From 88b373cae54c7c5e22910089bde5a32b671dc416 Mon Sep 17 00:00:00 2001 From: Konstantinos Koutsilis Date: Sat, 13 Apr 2024 22:16:45 +0100 Subject: [PATCH] Change args to flags, support custom html template --- cmd/root.go | 55 ++++++++++++++++++++++++++++++++++++++++------------- go.mod | 10 ++++++---- go.sum | 1 + 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 2f0a213..f3a2ac5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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"` @@ -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 @@ -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 } @@ -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") @@ -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) diff --git a/go.mod b/go.mod index b400b98..c966731 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 1f3a3b9..7d8e67d 100644 --- a/go.sum +++ b/go.sum @@ -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=