From 5fe19e0d167a1e88603b00ed57c2620493f7f0ed Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:28:52 -0500 Subject: [PATCH 01/20] Update .gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 6f6f5e6..e0f7ecc 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,8 @@ # Go workspace file go.work go.work.sum + +# IDE directories and misc. files +.idea +.vscode +**/.DS_Store From 4e05cbf8a9f3c67f4d8e6d8fb99208338f5a3bf5 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:29:59 -0500 Subject: [PATCH 02/20] Create Makefile --- Makefile | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1c050c8 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +default: lint + +.PHONY: init +init: + go mod download + +.PHONY: lint +lint: + go mod tidy + go fmt ./... + go vet ./... + staticcheck ./... From 3775a17e7181d13615a3d6be057e337ebf19c09b Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:30:03 -0500 Subject: [PATCH 03/20] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 42eda70..467608b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# golang-scripting +# Various Scripts in the Key of Golang Various scripts written in Golang. From 5fa04ef2047ade72496378ff167c4678a93c26ee Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:33:21 -0500 Subject: [PATCH 04/20] Create audit.yml --- .github/workflows/audit.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/audit.yml diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml new file mode 100644 index 0000000..f2fffad --- /dev/null +++ b/.github/workflows/audit.yml @@ -0,0 +1,34 @@ +name: Audit + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + + audit: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.22 + + - name: Verify dependencies + run: go mod verify + + - name: Build + run: go build -v ./... + + - name: Run go vet + run: go vet ./... + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Run staticcheck + run: staticcheck ./... From 6941ce9f0361328ffc8f65e94474bace07ad5742 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:47:52 -0500 Subject: [PATCH 05/20] Create go.mod --- go.mod | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a7f0401 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module slack_message_parser.go + +go 1.22.4 + +require github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 From 7dc0cf1990b514736ca34a44d3b2fc1e3a969959 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:47:56 -0500 Subject: [PATCH 06/20] Create go.sum --- go.sum | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 go.sum diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e47b2a7 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 h1:FWNFq4fM1wPfcK40yHE5UO3RUdSNPaBC+j3PokzA6OQ= +github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI= From 777263b1f3e8165b9eeb8679b032542f0b22d7c8 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:47:58 -0500 Subject: [PATCH 07/20] Update README.md --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 467608b..223d684 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ # Various Scripts in the Key of Golang Various scripts written in Golang. + +## Scripts + +### Slack Message Parser + + +#### Instructions +```console +cp [YOUR_ARCHIVE_PATH_HERE]/*.json ./SlackMessages/ +go run slack_message_parser.go +``` From 2339804b9d955cdb3eb7f94079845d9822c96ba3 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:48:01 -0500 Subject: [PATCH 08/20] Create slack_message_parser.go --- slack_message_parser.go | 157 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 slack_message_parser.go diff --git a/slack_message_parser.go b/slack_message_parser.go new file mode 100644 index 0000000..54faf7b --- /dev/null +++ b/slack_message_parser.go @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2024 Michael Plunkett (https://github.com/michplunkett) + * All rights reserved. + * Used to parse Slack messages. + */ + +package main + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "strconv" + "strings" + "time" + + "github.com/gocarina/gocsv" +) + +type Message struct { + TimeStamp string + UserID string `json:"user"` + TS string `json:"ts"` + Type string `json:"type"` + ClientMessageID string `json:"client_msg_id"` + Text string `json:"text,omitempty"` + UserProfile *UserProfile `json:"user_profile,omitempty"` + Attachments *[]*Attachment `json:"attachments,omitempty"` + Files *[]File `json:"files,omitempty"` + IsUpload bool `json:"upload,omitempty"` +} + +type UserProfile struct { + ProfileImage string `json:"image_72,omitempty"` + FirstName string `json:"first_name,omitempty"` + RealName string `json:"real_name,omitempty"` + DisplayName string `json:"display_name,omitempty"` + Name string `json:"name,omitempty"` +} + +type Attachment struct { + Text string `json:"text"` + Fallback string `json:"fallback"` + FromURL string `json:"from_url"` + ServiceName string `json:"service_name"` + ID int `json:"id"` + OriginalURL string `json:"original_url"` +} + +type File struct { + ID string `json:"id"` + Name string `json:"name"` + Title string `json:"title"` + MimeType string `json:"mimetype"` + FileType string `json:"pretty_type"` + IsExternal bool `json:"is_external"` + IsPublic bool `json:"is_public"` + DownloadLink string `json:"url_private_download"` + Height int `json:"original_w"` + Width int `json:"original_h"` +} + +type CSVRecord struct { + TimeStamp string + UserID string + UserName string + RealName string + MessageType string + Text string + Attachments []string + Files []string +} + +func main() { + files, err := filepath.Glob("./SlackMessages/*.json") + if err != nil { + fmt.Println(err) + } + + messages := make([]*Message, 0) + + for _, file := range files { + bytes, err := os.ReadFile(file) + if err != nil { + fmt.Printf("Error occured while opening %s: %+v\n", file, err) + continue + } + + fileMessages := make([]*Message, 0) + err = json.Unmarshal(bytes, &fileMessages) + if err != nil { + fmt.Printf("Error occured while parsing JSON from %s: %+v\n", file, err) + continue + } + + messages = append(messages, fileMessages...) + } + + csvRecords := make([]*CSVRecord, 0) + for _, msg := range messages { + record := &CSVRecord{ + UserID: msg.UserID, + UserName: "", + RealName: "", + MessageType: msg.Type, + Attachments: make([]string, 0), + Files: make([]string, 0), + } + + timeStampSplit := strings.Split(msg.TS, ".") + seconds, err := strconv.ParseInt(timeStampSplit[0], 10, 64) + if err != nil { + fmt.Printf("Error occured while parsing seconds: %+v\n", err) + continue + } + + nanoseconds, err := strconv.ParseInt(timeStampSplit[1], 10, 64) + if err != nil { + fmt.Printf("Error occured while parsing nanoseconds: %+v\n", err) + continue + } + + record.TimeStamp = time.Unix(seconds, nanoseconds).Format(time.RFC3339) + + if msg.UserProfile != nil { + record.UserName = msg.UserProfile.Name + record.RealName = msg.UserProfile.RealName + } + + if msg.Text != "" { + record.Text = msg.Text + } + + if msg.Attachments != nil { + for _, attachment := range *msg.Attachments { + record.Attachments = append(record.Attachments, attachment.OriginalURL) + } + } + + if msg.Files != nil { + for _, file := range *msg.Files { + record.Files = append(record.Files, file.DownloadLink) + } + } + + csvRecords = append(csvRecords, record) + } + + csvFile, err := os.Create("slack_records.csv") + if err != nil { + fmt.Println(err) + } + defer csvFile.Close() + + _ = gocsv.MarshalFile(csvRecords, csvFile) +} From 31b98969ea02a1b80b37205e37ec7644001747a6 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:50:05 -0500 Subject: [PATCH 09/20] Create pull_request_template.md --- .github/pull_request_template.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/pull_request_template.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..9775100 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,19 @@ + +## Describe your changes + + +## Non-obvious technical information + + +## Checklist before requesting a review +- [ ] The code runs successfully. + +```commandline +HERE IS SOME COMMAND LINE OUTPUT +``` From 36bc529e06b061b6c08df1d8b726f4fe67a4ebb3 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:52:13 -0500 Subject: [PATCH 10/20] Update slack_message_parser.go --- slack_message_parser.go => cmd/slack_message_parser.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename slack_message_parser.go => cmd/slack_message_parser.go (97%) diff --git a/slack_message_parser.go b/cmd/slack_message_parser.go similarity index 97% rename from slack_message_parser.go rename to cmd/slack_message_parser.go index 54faf7b..2c3c5ef 100644 --- a/slack_message_parser.go +++ b/cmd/slack_message_parser.go @@ -73,7 +73,7 @@ type CSVRecord struct { } func main() { - files, err := filepath.Glob("./SlackMessages/*.json") + files, err := filepath.Glob("../SlackMessages/*.json") if err != nil { fmt.Println(err) } @@ -147,7 +147,7 @@ func main() { csvRecords = append(csvRecords, record) } - csvFile, err := os.Create("slack_records.csv") + csvFile, err := os.Create("../slack_records.csv") if err != nil { fmt.Println(err) } From 3f26b3083773cfcd6261c09a43afd4fa706ee37a Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:55:08 -0500 Subject: [PATCH 11/20] Update go files --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a7f0401..3bf8e10 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module slack_message_parser.go +module cmd go 1.22.4 From 4e941bb153d3718c53bf69530893855c147d6ea2 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:07:34 -0500 Subject: [PATCH 12/20] Update slack_message_parser.go --- {cmd => scripts}/slack_message_parser.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {cmd => scripts}/slack_message_parser.go (100%) diff --git a/cmd/slack_message_parser.go b/scripts/slack_message_parser.go similarity index 100% rename from cmd/slack_message_parser.go rename to scripts/slack_message_parser.go From 87d64913add7de9932564c949c9a87546992058c Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:14:30 -0500 Subject: [PATCH 13/20] Update slack_message_parser.go --- scripts/slack_message_parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/slack_message_parser.go b/scripts/slack_message_parser.go index 2c3c5ef..30057e4 100644 --- a/scripts/slack_message_parser.go +++ b/scripts/slack_message_parser.go @@ -4,7 +4,7 @@ * Used to parse Slack messages. */ -package main +package scripts import ( "encoding/json" From 6a24115e1d50c380e25f6e32a8c9bd04ca7ce8ca Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:16:14 -0500 Subject: [PATCH 14/20] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3bf8e10..3966179 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module cmd +module scripts go 1.22.4 From 78367ae4ac60e17d01181a9c76efa71f4f915561 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:16:18 -0500 Subject: [PATCH 15/20] Update slack_message_parser.go --- scripts/slack_message_parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/slack_message_parser.go b/scripts/slack_message_parser.go index 30057e4..2c3c5ef 100644 --- a/scripts/slack_message_parser.go +++ b/scripts/slack_message_parser.go @@ -4,7 +4,7 @@ * Used to parse Slack messages. */ -package scripts +package main import ( "encoding/json" From cb2b9a67c48ea158cb47ce6aa698cdfe2c7106ec Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:23:16 -0500 Subject: [PATCH 16/20] Update go.mod --- scripts/slack_message_parser.go | 157 -------------------------------- 1 file changed, 157 deletions(-) delete mode 100644 scripts/slack_message_parser.go diff --git a/scripts/slack_message_parser.go b/scripts/slack_message_parser.go deleted file mode 100644 index 2c3c5ef..0000000 --- a/scripts/slack_message_parser.go +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright (c) 2024 Michael Plunkett (https://github.com/michplunkett) - * All rights reserved. - * Used to parse Slack messages. - */ - -package main - -import ( - "encoding/json" - "fmt" - "os" - "path/filepath" - "strconv" - "strings" - "time" - - "github.com/gocarina/gocsv" -) - -type Message struct { - TimeStamp string - UserID string `json:"user"` - TS string `json:"ts"` - Type string `json:"type"` - ClientMessageID string `json:"client_msg_id"` - Text string `json:"text,omitempty"` - UserProfile *UserProfile `json:"user_profile,omitempty"` - Attachments *[]*Attachment `json:"attachments,omitempty"` - Files *[]File `json:"files,omitempty"` - IsUpload bool `json:"upload,omitempty"` -} - -type UserProfile struct { - ProfileImage string `json:"image_72,omitempty"` - FirstName string `json:"first_name,omitempty"` - RealName string `json:"real_name,omitempty"` - DisplayName string `json:"display_name,omitempty"` - Name string `json:"name,omitempty"` -} - -type Attachment struct { - Text string `json:"text"` - Fallback string `json:"fallback"` - FromURL string `json:"from_url"` - ServiceName string `json:"service_name"` - ID int `json:"id"` - OriginalURL string `json:"original_url"` -} - -type File struct { - ID string `json:"id"` - Name string `json:"name"` - Title string `json:"title"` - MimeType string `json:"mimetype"` - FileType string `json:"pretty_type"` - IsExternal bool `json:"is_external"` - IsPublic bool `json:"is_public"` - DownloadLink string `json:"url_private_download"` - Height int `json:"original_w"` - Width int `json:"original_h"` -} - -type CSVRecord struct { - TimeStamp string - UserID string - UserName string - RealName string - MessageType string - Text string - Attachments []string - Files []string -} - -func main() { - files, err := filepath.Glob("../SlackMessages/*.json") - if err != nil { - fmt.Println(err) - } - - messages := make([]*Message, 0) - - for _, file := range files { - bytes, err := os.ReadFile(file) - if err != nil { - fmt.Printf("Error occured while opening %s: %+v\n", file, err) - continue - } - - fileMessages := make([]*Message, 0) - err = json.Unmarshal(bytes, &fileMessages) - if err != nil { - fmt.Printf("Error occured while parsing JSON from %s: %+v\n", file, err) - continue - } - - messages = append(messages, fileMessages...) - } - - csvRecords := make([]*CSVRecord, 0) - for _, msg := range messages { - record := &CSVRecord{ - UserID: msg.UserID, - UserName: "", - RealName: "", - MessageType: msg.Type, - Attachments: make([]string, 0), - Files: make([]string, 0), - } - - timeStampSplit := strings.Split(msg.TS, ".") - seconds, err := strconv.ParseInt(timeStampSplit[0], 10, 64) - if err != nil { - fmt.Printf("Error occured while parsing seconds: %+v\n", err) - continue - } - - nanoseconds, err := strconv.ParseInt(timeStampSplit[1], 10, 64) - if err != nil { - fmt.Printf("Error occured while parsing nanoseconds: %+v\n", err) - continue - } - - record.TimeStamp = time.Unix(seconds, nanoseconds).Format(time.RFC3339) - - if msg.UserProfile != nil { - record.UserName = msg.UserProfile.Name - record.RealName = msg.UserProfile.RealName - } - - if msg.Text != "" { - record.Text = msg.Text - } - - if msg.Attachments != nil { - for _, attachment := range *msg.Attachments { - record.Attachments = append(record.Attachments, attachment.OriginalURL) - } - } - - if msg.Files != nil { - for _, file := range *msg.Files { - record.Files = append(record.Files, file.DownloadLink) - } - } - - csvRecords = append(csvRecords, record) - } - - csvFile, err := os.Create("../slack_records.csv") - if err != nil { - fmt.Println(err) - } - defer csvFile.Close() - - _ = gocsv.MarshalFile(csvRecords, csvFile) -} From 4de4114f0ba04f37255716bc1e93a5b0aef5690f Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:23:19 -0500 Subject: [PATCH 17/20] Create main.go --- cmd/slackMessageParser/main.go | 157 +++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 cmd/slackMessageParser/main.go diff --git a/cmd/slackMessageParser/main.go b/cmd/slackMessageParser/main.go new file mode 100644 index 0000000..4248a23 --- /dev/null +++ b/cmd/slackMessageParser/main.go @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2024 Michael Plunkett (https://github.com/michplunkett) + * All rights reserved. + * Used to parse Slack messages. + */ + +package main + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "strconv" + "strings" + "time" + + "github.com/gocarina/gocsv" +) + +type Message struct { + TimeStamp string + UserID string `json:"user"` + TS string `json:"ts"` + Type string `json:"type"` + ClientMessageID string `json:"client_msg_id"` + Text string `json:"text,omitempty"` + UserProfile *UserProfile `json:"user_profile,omitempty"` + Attachments *[]*Attachment `json:"attachments,omitempty"` + Files *[]File `json:"files,omitempty"` + IsUpload bool `json:"upload,omitempty"` +} + +type UserProfile struct { + ProfileImage string `json:"image_72,omitempty"` + FirstName string `json:"first_name,omitempty"` + RealName string `json:"real_name,omitempty"` + DisplayName string `json:"display_name,omitempty"` + Name string `json:"name,omitempty"` +} + +type Attachment struct { + Text string `json:"text"` + Fallback string `json:"fallback"` + FromURL string `json:"from_url"` + ServiceName string `json:"service_name"` + ID int `json:"id"` + OriginalURL string `json:"original_url"` +} + +type File struct { + ID string `json:"id"` + Name string `json:"name"` + Title string `json:"title"` + MimeType string `json:"mimetype"` + FileType string `json:"pretty_type"` + IsExternal bool `json:"is_external"` + IsPublic bool `json:"is_public"` + DownloadLink string `json:"url_private_download"` + Height int `json:"original_w"` + Width int `json:"original_h"` +} + +type CSVRecord struct { + TimeStamp string + UserID string + UserName string + RealName string + MessageType string + Text string + Attachments []string + Files []string +} + +func main() { + files, err := filepath.Glob("../../SlackMessages/*.json") + if err != nil { + fmt.Println(err) + } + + messages := make([]*Message, 0) + + for _, file := range files { + bytes, err := os.ReadFile(file) + if err != nil { + fmt.Printf("Error occured while opening %s: %+v\n", file, err) + continue + } + + fileMessages := make([]*Message, 0) + err = json.Unmarshal(bytes, &fileMessages) + if err != nil { + fmt.Printf("Error occured while parsing JSON from %s: %+v\n", file, err) + continue + } + + messages = append(messages, fileMessages...) + } + + csvRecords := make([]*CSVRecord, 0) + for _, msg := range messages { + record := &CSVRecord{ + UserID: msg.UserID, + UserName: "", + RealName: "", + MessageType: msg.Type, + Attachments: make([]string, 0), + Files: make([]string, 0), + } + + timeStampSplit := strings.Split(msg.TS, ".") + seconds, err := strconv.ParseInt(timeStampSplit[0], 10, 64) + if err != nil { + fmt.Printf("Error occured while parsing seconds: %+v\n", err) + continue + } + + nanoseconds, err := strconv.ParseInt(timeStampSplit[1], 10, 64) + if err != nil { + fmt.Printf("Error occured while parsing nanoseconds: %+v\n", err) + continue + } + + record.TimeStamp = time.Unix(seconds, nanoseconds).Format(time.RFC3339) + + if msg.UserProfile != nil { + record.UserName = msg.UserProfile.Name + record.RealName = msg.UserProfile.RealName + } + + if msg.Text != "" { + record.Text = msg.Text + } + + if msg.Attachments != nil { + for _, attachment := range *msg.Attachments { + record.Attachments = append(record.Attachments, attachment.OriginalURL) + } + } + + if msg.Files != nil { + for _, file := range *msg.Files { + record.Files = append(record.Files, file.DownloadLink) + } + } + + csvRecords = append(csvRecords, record) + } + + csvFile, err := os.Create("../../slack_records.csv") + if err != nil { + fmt.Println(err) + } + defer csvFile.Close() + + _ = gocsv.MarshalFile(csvRecords, csvFile) +} From dc53d78f0fbc73e534222c95814b3242c0d750d0 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:23:24 -0500 Subject: [PATCH 18/20] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3966179..37af3fe 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module scripts +module github.com/michplunkett/golang-scripting go 1.22.4 From 10f09ea0701fd279d62cec889ad0b6fa0c9593c9 Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:32:31 -0500 Subject: [PATCH 19/20] Update main.go --- cmd/slackMessageParser/main.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/slackMessageParser/main.go b/cmd/slackMessageParser/main.go index 4248a23..311d8de 100644 --- a/cmd/slackMessageParser/main.go +++ b/cmd/slackMessageParser/main.go @@ -9,6 +9,7 @@ package main import ( "encoding/json" "fmt" + "log" "os" "path/filepath" "strconv" @@ -75,7 +76,11 @@ type CSVRecord struct { func main() { files, err := filepath.Glob("../../SlackMessages/*.json") if err != nil { - fmt.Println(err) + log.Fatal(err) + } + + if len(files) == 0 { + log.Fatal("Could not find *.json files in the ./SlackMessages directory") } messages := make([]*Message, 0) @@ -149,7 +154,7 @@ func main() { csvFile, err := os.Create("../../slack_records.csv") if err != nil { - fmt.Println(err) + log.Fatal(err) } defer csvFile.Close() From 9b527683c87503eecab23edf0a1844ec8eb7817a Mon Sep 17 00:00:00 2001 From: michplunkett <5885605+michplunkett@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:38:33 -0500 Subject: [PATCH 20/20] Update main.go --- cmd/slackMessageParser/main.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/slackMessageParser/main.go b/cmd/slackMessageParser/main.go index 311d8de..aea22af 100644 --- a/cmd/slackMessageParser/main.go +++ b/cmd/slackMessageParser/main.go @@ -74,13 +74,17 @@ type CSVRecord struct { } func main() { + if _, err := os.Stat("../../SlackMessages"); os.IsNotExist(err) { + log.Fatal("the ./SlackMessages directory does not exist") + } + files, err := filepath.Glob("../../SlackMessages/*.json") if err != nil { log.Fatal(err) } if len(files) == 0 { - log.Fatal("Could not find *.json files in the ./SlackMessages directory") + log.Fatal("could not find *.json files in the ./SlackMessages directory") } messages := make([]*Message, 0) @@ -88,14 +92,14 @@ func main() { for _, file := range files { bytes, err := os.ReadFile(file) if err != nil { - fmt.Printf("Error occured while opening %s: %+v\n", file, err) + fmt.Printf("error occured while opening %s: %+v\n", file, err) continue } fileMessages := make([]*Message, 0) err = json.Unmarshal(bytes, &fileMessages) if err != nil { - fmt.Printf("Error occured while parsing JSON from %s: %+v\n", file, err) + fmt.Printf("error occured while parsing JSON from %s: %+v\n", file, err) continue } @@ -116,13 +120,13 @@ func main() { timeStampSplit := strings.Split(msg.TS, ".") seconds, err := strconv.ParseInt(timeStampSplit[0], 10, 64) if err != nil { - fmt.Printf("Error occured while parsing seconds: %+v\n", err) + fmt.Printf("error occured while parsing seconds: %+v\n", err) continue } nanoseconds, err := strconv.ParseInt(timeStampSplit[1], 10, 64) if err != nil { - fmt.Printf("Error occured while parsing nanoseconds: %+v\n", err) + fmt.Printf("error occured while parsing nanoseconds: %+v\n", err) continue }