Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions cmd/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import (
"golang.org/x/term"
)

var loginName string
var loginSkipVerify bool

var loginCmd = &cobra.Command{
Use: "login",
Use: "login <name>",
Short: "Authenticate with your Loops API key",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if loginName == "" {
return errors.New("use --name to give this key a name (e.g. loops auth login --name my-team)")
}
name := args[0]

fmt.Fprint(os.Stderr, "Enter your API key: ")
raw, err := term.ReadPassword(int(os.Stdin.Fd()))
Expand All @@ -35,30 +33,30 @@ var loginCmd = &cobra.Command{
return fmt.Errorf("API key cannot be empty")
}

result, err := runAuthLogin(apiKey, loginName, loginSkipVerify)
result, err := runAuthLogin(apiKey, name, loginSkipVerify)
if err != nil {
return err
}

if isJSONOutput() {
msg := fmt.Sprintf("API key saved as %q", loginName)
msg := fmt.Sprintf("API key saved as %q", name)
if result != nil {
msg = fmt.Sprintf("Authenticated as team: %s", result.TeamName)
}
return printJSON(cmd.OutOrStdout(), Result{Success: true, Message: msg})
}
if result != nil {
fmt.Fprintf(cmd.OutOrStdout(), "API key saved as %q. Authenticated as team: %s\n", loginName, result.TeamName)
fmt.Fprintf(cmd.OutOrStdout(), "API key saved as %q. Authenticated as team: %s\n", name, result.TeamName)
} else {
fmt.Fprintf(cmd.OutOrStdout(), "API key saved as %q.\n", loginName)
fmt.Fprintf(cmd.OutOrStdout(), "API key saved as %q.\n", name)
}
return nil
},
}

func runAuthLogin(apiKey, name string, skipVerify bool) (*api.APIKeyResponse, error) {
if name == "" {
return nil, errors.New("use --name to give this key a name")
return nil, errors.New("a key name is required")
}
if skipVerify {
if err := config.Save(apiKey, name); err != nil {
Expand All @@ -77,7 +75,6 @@ func runAuthLogin(apiKey, name string, skipVerify bool) (*api.APIKeyResponse, er
}

func init() {
loginCmd.Flags().StringVarP(&loginName, "name", "n", "", "Name for this API key (e.g. my-team)")
loginCmd.Flags().BoolVar(&loginSkipVerify, "skip-verify", false, "Save the API key without verifying it")
authCmd.AddCommand(loginCmd)
}
17 changes: 8 additions & 9 deletions cmd/auth_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,30 @@ import (
"github.com/spf13/cobra"
)

var logoutName string

var logoutCmd = &cobra.Command{
Use: "logout",
Use: "logout <name>",
Short: "Remove stored Loops credentials",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if logoutName == "" {
return errors.New("use --name to specify which key to remove (e.g. loops auth logout --name loops-prod)")
}
if err := runAuthLogout(logoutName); err != nil {
name := args[0]
if err := runAuthLogout(name); err != nil {
return err
}
if isJSONOutput() {
return printJSON(cmd.OutOrStdout(), Result{Success: true})
}
fmt.Fprintf(cmd.OutOrStdout(), "Logged out of %q.\n", logoutName)
fmt.Fprintf(cmd.OutOrStdout(), "Logged out of %q.\n", name)
return nil
},
}

func runAuthLogout(name string) error {
if name == "" {
return errors.New("a key name is required")
}
return config.Delete(name)
}

func init() {
logoutCmd.Flags().StringVarP(&logoutName, "name", "n", "", "Name of the API key to remove")
authCmd.AddCommand(logoutCmd)
}
7 changes: 3 additions & 4 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ var eventsCmd = &cobra.Command{
}

var eventsSendCmd = &cobra.Command{
Use: "send",
Use: "send <event>",
Short: "Send an event",
Args: cobra.ExactArgs(1),
RunE: eventsSendRunE,
}

Expand All @@ -61,7 +62,7 @@ func eventsSendRunE(cmd *cobra.Command, args []string) error {
return fmt.Errorf("at least one of --email or --user-id is required")
}

eventName, _ := cmd.Flags().GetString("event")
eventName := args[0]
idempotencyKey, _ := cmd.Flags().GetString("idempotency-key")

req := api.SendEventRequest{
Expand Down Expand Up @@ -112,14 +113,12 @@ func eventsSendRunE(cmd *cobra.Command, args []string) error {
}

func addEventsSendFlags(cmd *cobra.Command) {
cmd.Flags().String("event", "", "Event name")
cmd.Flags().String("email", "", "Contact email address")
cmd.Flags().String("user-id", "", "Contact user ID")
cmd.Flags().String("props", "", "Path to a JSON file of event properties")
cmd.Flags().String("contact-props", "", "Path to a JSON file of contact properties")
cmd.Flags().StringArray("list", nil, "Mailing list subscription as id=true|false (repeatable)")
cmd.Flags().String("idempotency-key", "", "Idempotency key to prevent duplicate sends")
cmd.MarkFlagRequired("event")
}

func init() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import "github.com/spf13/cobra"

var sendCmd = &cobra.Command{
Use: "send",
Use: "send <id>",
Short: "Send a transactional email",
Args: cobra.ExactArgs(1),
RunE: transactionalSendRunE,
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/transactional.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func transactionalSendRunE(cmd *cobra.Command, args []string) error {
return err
}

id := args[0]
email, _ := cmd.Flags().GetString("email")
id, _ := cmd.Flags().GetString("id")
idempotencyKey, _ := cmd.Flags().GetString("idempotency-key")

req := api.SendTransactionalRequest{
Expand Down Expand Up @@ -164,19 +164,18 @@ func transactionalSendRunE(cmd *cobra.Command, args []string) error {

func addTransactionalSendFlags(cmd *cobra.Command) {
cmd.Flags().String("email", "", "Recipient email address")
cmd.Flags().String("id", "", "Transactional email ID")
cmd.Flags().BoolP("add-to-audience", "a", false, "Create a contact if one doesn't exist")
cmd.Flags().StringArrayP("var", "v", nil, "Data variable as KEY=value (repeatable)")
cmd.Flags().StringP("json-vars", "j", "", "Path to a JSON file of data variables")
cmd.Flags().StringArrayP("attachment", "A", nil, "Path to a file to attach (repeatable)")
cmd.Flags().String("idempotency-key", "", "Idempotency key to prevent duplicate sends")
cmd.MarkFlagRequired("email")
cmd.MarkFlagRequired("id")
}

var transactionalSendCmd = &cobra.Command{
Use: "send",
Use: "send <id>",
Short: "Send a transactional email",
Args: cobra.ExactArgs(1),
RunE: transactionalSendRunE,
}

Expand Down