Skip to content

Commit

Permalink
Delegate error handling to caller
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoutsilis committed Jan 11, 2024
1 parent b7ca680 commit f1f066a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ export EMAIL_FROM=test@example.org
export EMAIL_USER=””
export EMAIL_PASSWORD=””
```
You can access the MailHog UI at `localhost:8025`
28 changes: 16 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"log"
"math/rand"
Expand All @@ -15,7 +16,7 @@ import (
"gopkg.in/gomail.v2"
)

var version string = "0.0.1"
var version string = "0.1.0"

type Data struct {
Name string `json:"name"`
Expand Down Expand Up @@ -109,11 +110,12 @@ func sendEmails(matches []MatchPair) {
}

var rootCmd = &cobra.Command{
Use: "run [path]",
Short: "A cli tool that generates secret santa matches and notifies the participants by email",
ArgAliases: []string{"path"},
Version: version,
Run: func(cmd *cobra.Command, args []string) {
Use: "run [path]",
Short: "A cli tool that generates secret santa matches and notifies the participants by email",
ArgAliases: []string{"path"},
Version: version,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
var filePath string
if len(args) == 0 {
filePath = "data.json"
Expand All @@ -125,31 +127,32 @@ var rootCmd = &cobra.Command{
}

if !checkFileExists(filePath) {
log.Fatal("File ", filePath, " does not exist")
return fmt.Errorf("file %s does not exist", filePath)
}

if !checkIsJson(filePath) {
log.Fatal("File ", filePath, " is not a json file")
return fmt.Errorf("file %s is not a json file", filePath)
}

file, err := os.ReadFile(filePath)
if err != nil {
log.Fatal("Error when opening file: ", err)
return fmt.Errorf("error when opening file: %w", err)
}

var payload []Data
err = json.Unmarshal(file, &payload)
if err != nil {
log.Fatal("Error reading file content: ", err)
return fmt.Errorf("error reading file content: %w", err)
}

if len(payload) == 0 {
log.Fatal("File is empty")
return errors.New("file is empty")
}

matches := generateSecretSantaMatches(payload)

sendEmails(matches)

return nil
},
}

Expand All @@ -158,4 +161,5 @@ func Execute() {
if err != nil {
os.Exit(1)
}
os.Exit(0)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

0 comments on commit f1f066a

Please sign in to comment.