Skip to content

Commit

Permalink
Change args to flags, support custom html template (#13)
Browse files Browse the repository at this point in the history
* Change args to flags, support custom HTML template
  • Loading branch information
kkoutsilis authored Apr 13, 2024
1 parent fa8701c commit f3963d0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 37 deletions.
48 changes: 28 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

# SSMG
# SSMG

![CI](https://github.com/kkoutsilis/SSMG/actions/workflows/ci.yml/badge.svg)

Expand All @@ -10,14 +9,14 @@ SSMG (Secret Santa Match Generator) is a simple CLI tool written in Go. It reads
Picture this: my friends and I, spread across the globe, were scheming up a festive get-together complete with a sprinkle of Secret Santa enchantment. Now, the catch? Our scattered geography made the classic match-making puzzle a bit tricky. Sure, there are online platforms for such scenarios, but most demanded user accounts—a hoop I'd rather not jump through. And then there's the whole sharing-emails-with-apps deal, not my cup of cocoa. So, cue my weekend project: a sleek CLI tool tailored to weave our Secret Santa magic, no strings attached. Here's to coding our way to holiday cheer! 🌐🎄✨

**Disclaimer**
I am not so familiar with GO so the code probably needs cleaning and improvements, but hey, it works!
I am not so familiar with GO so the code probably needs cleaning and improvements, but hey, it works!

### Usage

To use SSMG, you need to have go installed, clone the repo locally, provide a JSON file containing the participant data and some environment variables to be used to send the emails.

To use SSMG, you need to have go installed, clone the repo locally, provide a JSON file containing the participant data and some environmental variables to be used to send the emails.
#### Environment Variables

#### Environmental Variables
- `EMAIL_HOST`: The email host, for example `smtp.gmail.com` for gmail.

- `EMAIL_PORT`: The email port, tipically `583` for smtp.
Expand All @@ -28,40 +27,49 @@ To use SSMG, you need to have go installed, clone the repo locally, provide a JS

- `EMAIL_PASSWORD`: The passwowrd for the given email account.

**Note**: Gmail does not allow plain password login and instead uses app passwords, which is more secure as well. You can learn how to create and use app passwords [here](https://support.google.com/accounts/answer/185833).
**Note**: Gmail does not allow plain password login and instead uses app passwords, which is more secure as well. You can learn how to create and use app passwords [here](https://support.google.com/accounts/answer/185833).

#### Input File Format

```json
[
{
"name": "participant1",
"email": "[email protected]",
},
{
"name": "participant2",
"email": "[email protected]"
}
{
"name": "participant1",
"email": "[email protected]"
},
{
"name": "participant2",
"email": "[email protected]"
}
]

```

#### Run

```bash
go run main.go --file my_data.json
```
go run main.go my_data.json

or by using the binary

```bash
go build --ldflags "-s -w" ssmg
./ssmg --file my_data.json
```
**Note**: If no filename is provided, the program will default to data.json.

**Note**: If `file` flag is not provided, the program will default to data.json.

### Testing Emails

There is a docker compose file that spins up a MailHog service that can be used for testing the emails.
Simply `docker compose up` to use the testing MailHog service and set the environmentat variables to
There is a docker compose file that spins up a [MailHog](https://github.com/mailhog/MailHog) service that can be used for testing the emails.
Simply `docker compose up` to use the testing (MailHoghttps://github.com/mailhog/MailHog) service and set the environment variables to

```bash
export EMAIL_HOST=localhost
export EMAIL_PORT=1025
export [email protected]
export EMAIL_USER=””
export EMAIL_PASSWORD=””
```
You can access the MailHog UI at `localhost:8025`

You can access the MailHog UI at `localhost:8025`
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 environment variable is not set")
os.Exit(1)
}
_, ok = os.LookupEnv("EMAIL_PORT")
if !ok {
fmt.Printf("EMAIL_PORT environment variable is not set")
os.Exit(1)
}
_, ok = os.LookupEnv("EMAIL_USER")
if !ok {
fmt.Printf("EMAIL_USER environment variable is not set")
os.Exit(1)
}
_, ok = os.LookupEnv("EMAIL_PASSWORD")
if !ok {
fmt.Printf("EMAIL_PASSOWRD environment variable is not set")
os.Exit(1)
}

rootCmd.Flags().StringVarP(&fileFlag, "file", "f", "", "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 f3963d0

Please sign in to comment.