Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sendgrid CLI #251

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
121 changes: 121 additions & 0 deletions cmd/sendgrid/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"

"github.com/joho/godotenv"
"github.com/rotationalio/ensign/pkg"
"github.com/rotationalio/ensign/pkg/utils/sendgrid"
"github.com/urfave/cli/v2"
)

func main() {
// If a dotenv file exists load it for configuration
godotenv.Load()

// Creates a multi-command CLI application
app := cli.NewApp()
app.Name = "sendgrid"
app.Version = pkg.Version()
app.Usage = "interact with the sendgrid API"
app.Flags = []cli.Flag{}
app.Commands = []*cli.Command{
{
Name: "lists",
Usage: "fetch marketing lists from sendgrid",
Category: "api",
Action: fetchLists,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "apikey",
Aliases: []string{"k"},
Usage: "sendgrid API key",
},
},
},
{
Name: "defs",
Usage: "fetch field definitions from sendgrid",
Category: "api",
Action: fetchDefs,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "apikey",
Aliases: []string{"k"},
Usage: "sendgrid API key",
},
},
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}

//===========================================================================
// SendGrid API Commands
//===========================================================================

func fetchLists(c *cli.Context) (err error) {
key := apiKey(c)
if key == "" {
return cli.Exit("specify sendgrid api key in command line or environment", 1)
}

// Fetch a page of sendgrid marketing lists
// Note: Default page size is 100 so we should get everything in one request
var rep string
if rep, err = sendgrid.MarketingLists(key, ""); err != nil {
return cli.Exit(err, 1)
}

// Print the response
printJSON(rep)
return nil
}

func fetchDefs(c *cli.Context) (err error) {
key := apiKey(c)
if key == "" {
return cli.Exit("specify sendgrid api key in command line or environment", 1)
}

// Fetch the field definitions
var rep string
if rep, err = sendgrid.FieldDefinitions(key); err != nil {
return cli.Exit(err, 1)
}

// Print the response
printJSON(rep)
return nil
}

//===========================================================================
// Helpers
//===========================================================================

// Get the API key from the command line or environment
func apiKey(c *cli.Context) string {
key := c.String("apikey")
if key == "" {
key = os.Getenv("SENDGRID_API_KEY")
}

return key
}

// Print a JSON string response to stdout and add indents for readability
func printJSON(rep string) {
out := &bytes.Buffer{}
if err := json.Indent(out, []byte(rep), "", " "); err != nil {
cli.Exit(err, 1)
}

fmt.Println(out.String())
}