Skip to content

Commit

Permalink
Change args to flags, support custom html template
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoutsilis committed Apr 13, 2024
1 parent fa8701c commit 88b373c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
55 changes: 42 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"gopkg.in/gomail.v2"
)

var version string = "0.2.0"
var version string = "0.3.0"

type Data struct {
Name string `json:"name"`
Expand Down Expand Up @@ -134,13 +134,16 @@ func sendEmails(emailMessages ...*gomail.Message) error {

}

var fileFlag string
var templateFlag string

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,
SilenceUsage: true,
Use: "run",
Short: "A cli tool that generates secret santa matches and notifies the participants via email",
Example: "ssmg run --file data.json --template my_template.html",
Version: version,
RunE: func(cmd *cobra.Command, args []string) error {

defaultValues := struct {
FilePath string
TemplatePath string
Expand All @@ -150,12 +153,9 @@ var rootCmd = &cobra.Command{
}

var filePath string
if len(args) == 0 {
filePath = defaultValues.FilePath
if strings.Trim(fileFlag, " ") != "" {
filePath = fileFlag
} else {
filePath = args[0]
}
if filePath == "" {
filePath = defaultValues.FilePath
}

Expand Down Expand Up @@ -184,8 +184,14 @@ var rootCmd = &cobra.Command{
}

matches := generateSecretSantaMatches(payload)
// TODO: Give users the option to use their own template
templateFilePath := defaultValues.TemplatePath

var templateFilePath string
if strings.Trim(templateFlag, " ") != "" {
templateFilePath = templateFlag
} else {
templateFilePath = defaultValues.TemplatePath
}

tmpl, err := loadEmailTemplate(templateFilePath)
if err != nil {
return errors.Wrap(err, "error loading email template")
Expand All @@ -206,6 +212,29 @@ var rootCmd = &cobra.Command{
}

func Execute() {
_, ok := os.LookupEnv("EMAIL_HOST")
if !ok {
fmt.Printf("EMAIL_HOST environmental variable is not set")
os.Exit(1)
}
_, ok = os.LookupEnv("EMAIL_PORT")
if !ok {
fmt.Printf("EMAIL_PORT environmental variable is not set")
os.Exit(1)
}
_, ok = os.LookupEnv("EMAIL_USER")
if !ok {
fmt.Printf("EMAIL_USER environmental variable is not set")
os.Exit(1)
}
_, ok = os.LookupEnv("EMAIL_PASSWORD")
if !ok {
fmt.Printf("EMAIL_PASSOWRD environmental variable is not set")
os.Exit(1)
}

rootCmd.Flags().StringVarP(&fileFlag, "file", "p", "", "Path to input file")
rootCmd.Flags().StringVarP(&templateFlag, "template", "t", "", "Path to custom html email template file")
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module ssmg

go 1.21.4
go 1.22.0

require github.com/spf13/cobra v1.8.0
require (
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
Expand Down

0 comments on commit 88b373c

Please sign in to comment.