|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "text/tabwriter" |
| 12 | + |
| 13 | + "github.com/loops-so/cli/internal/api" |
| 14 | + "github.com/loops-so/cli/internal/config" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +func attachmentFromPath(path string) (api.Attachment, error) { |
| 19 | + info, err := os.Stat(path) |
| 20 | + if err != nil { |
| 21 | + return api.Attachment{}, fmt.Errorf("attachment %q: %w", path, err) |
| 22 | + } |
| 23 | + if info.IsDir() { |
| 24 | + return api.Attachment{}, fmt.Errorf("attachment %q: is a directory", path) |
| 25 | + } |
| 26 | + |
| 27 | + data, err := os.ReadFile(path) |
| 28 | + if err != nil { |
| 29 | + return api.Attachment{}, fmt.Errorf("attachment %q: %w", path, err) |
| 30 | + } |
| 31 | + |
| 32 | + sniff := data |
| 33 | + if len(sniff) > 512 { |
| 34 | + sniff = sniff[:512] |
| 35 | + } |
| 36 | + contentType := http.DetectContentType(sniff) |
| 37 | + |
| 38 | + return api.Attachment{ |
| 39 | + Filename: filepath.Base(path), |
| 40 | + ContentType: contentType, |
| 41 | + Data: base64.StdEncoding.EncodeToString(data), |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +var transactionalCmd = &cobra.Command{ |
| 46 | + Use: "transactional", |
| 47 | + Short: "Manage transactional emails", |
| 48 | +} |
| 49 | + |
| 50 | +var transactionalListCmd = &cobra.Command{ |
| 51 | + Use: "list", |
| 52 | + Short: "List published transactional emails", |
| 53 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 54 | + cfg, err := config.Load() |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + params := paginationParams(cmd) |
| 60 | + client := api.NewClient(cfg.EndpointURL, cfg.APIKey) |
| 61 | + |
| 62 | + var emails []api.TransactionalEmail |
| 63 | + if params.Cursor != "" { |
| 64 | + emails, _, err = client.ListTransactional(params) |
| 65 | + } else { |
| 66 | + emails, err = api.Paginate(func(cursor string) ([]api.TransactionalEmail, *api.Pagination, error) { |
| 67 | + return client.ListTransactional(api.PaginationParams{ |
| 68 | + PerPage: params.PerPage, |
| 69 | + Cursor: cursor, |
| 70 | + }) |
| 71 | + }) |
| 72 | + } |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if len(emails) == 0 { |
| 78 | + fmt.Println("No transactional emails found.") |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 83 | + fmt.Fprintln(w, "ID\tNAME\tLAST UPDATED\tVARIABLES") |
| 84 | + for _, e := range emails { |
| 85 | + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", e.ID, e.Name, e.LastUpdated, strings.Join(e.DataVariables, ", ")) |
| 86 | + } |
| 87 | + w.Flush() |
| 88 | + |
| 89 | + return nil |
| 90 | + }, |
| 91 | +} |
| 92 | + |
| 93 | +var transactionalSendCmd = &cobra.Command{ |
| 94 | + Use: "send", |
| 95 | + Short: "Send a transactional email", |
| 96 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 97 | + cfg, err := config.Load() |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + email, _ := cmd.Flags().GetString("email") |
| 103 | + id, _ := cmd.Flags().GetString("id") |
| 104 | + dataRaw, _ := cmd.Flags().GetString("data") |
| 105 | + |
| 106 | + req := api.SendTransactionalRequest{ |
| 107 | + Email: email, |
| 108 | + TransactionalID: id, |
| 109 | + } |
| 110 | + |
| 111 | + if cmd.Flags().Changed("add-to-audience") { |
| 112 | + v, _ := cmd.Flags().GetBool("add-to-audience") |
| 113 | + req.AddToAudience = &v |
| 114 | + } |
| 115 | + |
| 116 | + if dataRaw != "" { |
| 117 | + if err := json.Unmarshal([]byte(dataRaw), &req.DataVariables); err != nil { |
| 118 | + return fmt.Errorf("--data must be a valid JSON object: %w", err) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + paths, _ := cmd.Flags().GetStringArray("attachment") |
| 123 | + for _, path := range paths { |
| 124 | + a, err := attachmentFromPath(path) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + req.Attachments = append(req.Attachments, a) |
| 129 | + } |
| 130 | + |
| 131 | + client := api.NewClient(cfg.EndpointURL, cfg.APIKey) |
| 132 | + if err := client.SendTransactional(req); err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + fmt.Println("Sent.") |
| 137 | + return nil |
| 138 | + }, |
| 139 | +} |
| 140 | + |
| 141 | +func init() { |
| 142 | + addPaginationFlags(transactionalListCmd) |
| 143 | + transactionalCmd.AddCommand(transactionalListCmd) |
| 144 | + |
| 145 | + transactionalSendCmd.Flags().String("email", "", "Recipient email address") |
| 146 | + transactionalSendCmd.Flags().String("id", "", "Transactional email ID") |
| 147 | + transactionalSendCmd.Flags().BoolP("add-to-audience", "a", false, "Create a contact if one doesn't exist") |
| 148 | + transactionalSendCmd.Flags().String("data", "", "Data variables as a JSON object") |
| 149 | + transactionalSendCmd.Flags().StringArrayP("attachment", "A", nil, "Path to a file to attach (repeatable)") |
| 150 | + transactionalSendCmd.MarkFlagRequired("email") |
| 151 | + transactionalSendCmd.MarkFlagRequired("id") |
| 152 | + transactionalCmd.AddCommand(transactionalSendCmd) |
| 153 | + |
| 154 | + rootCmd.AddCommand(transactionalCmd) |
| 155 | +} |
0 commit comments