|
| 1 | +package campaigns |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hostinger/api-cli/api" |
| 8 | + "github.com/hostinger/api-cli/client" |
| 9 | + "github.com/hostinger/api-cli/output" |
| 10 | + "github.com/hostinger/api-cli/utils" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ListCmd = &cobra.Command{ |
| 15 | + Use: "list <profile-uuid>", |
| 16 | + Short: "List campaigns", |
| 17 | + Long: "Get a paginated list of the campaigns in a profile.\n\nEach campaign carries its headline engagement rates. Filter by status to find drafts,\nscheduled, sending or sent campaigns, keeping in mind that a fully sent campaign has the\nstatus `publish`. By default only regular campaigns are returned - pass `type` to get the\nemails sent by automations or the double opt-in confirmations instead.", |
| 18 | + Args: cobra.MatchAll(cobra.ExactArgs(1)), |
| 19 | + Run: func(cmd *cobra.Command, args []string) { |
| 20 | + utils.EnumCheck(cmd, "status", []string{"draft", "scheduled", "sending", "publish", "failed"}) |
| 21 | + utils.EnumCheck(cmd, "type", []string{"campaign", "automation", "double_opt_in"}) |
| 22 | + utils.EnumCheck(cmd, "sort-direction", []string{"asc", "desc"}) |
| 23 | + r, err := api.Request().ReachListCampaignsV1WithResponse(context.TODO(), args[0], listParams(cmd)) |
| 24 | + if err != nil { |
| 25 | + log.Fatal(err) |
| 26 | + } |
| 27 | + |
| 28 | + output.Format(cmd, r.Body, r.StatusCode()) |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +func init() { |
| 33 | + ListCmd.Flags().StringP("status", "", "", "Filter campaigns by status.\n\nA fully sent campaign has the status `publish`. There is no `sent` status, and campaigns can\nbe neither paused nor archived. (one of: draft, scheduled, sending, publish, failed)") |
| 34 | + ListCmd.Flags().StringP("type", "", "campaign", "Filter campaigns by type.\n\nDefaults to `campaign`, which leaves out the emails sent by automations and the double\nopt-in confirmations. (one of: campaign, automation, double_opt_in)") |
| 35 | + ListCmd.Flags().StringP("sort-direction", "", "", "Order campaigns by creation date. Newest first unless set to `asc`. (one of: asc, desc)") |
| 36 | + ListCmd.Flags().IntP("page", "", 0, "Page number") |
| 37 | + ListCmd.Flags().IntP("per-page", "", 25, "Number of items per page") |
| 38 | +} |
| 39 | + |
| 40 | +func listParams(cmd *cobra.Command) *client.ReachListCampaignsV1Params { |
| 41 | + params := &client.ReachListCampaignsV1Params{} |
| 42 | + if cmd.Flags().Changed("status") { |
| 43 | + v, _ := cmd.Flags().GetString("status") |
| 44 | + e := client.ReachListCampaignsV1ParamsStatus(v) |
| 45 | + params.Status = &e |
| 46 | + } |
| 47 | + if cmd.Flags().Changed("type") { |
| 48 | + v, _ := cmd.Flags().GetString("type") |
| 49 | + e := client.ReachListCampaignsV1ParamsType(v) |
| 50 | + params.Type = &e |
| 51 | + } |
| 52 | + if cmd.Flags().Changed("sort-direction") { |
| 53 | + v, _ := cmd.Flags().GetString("sort-direction") |
| 54 | + e := client.ReachListCampaignsV1ParamsSortDirection(v) |
| 55 | + params.SortDirection = &e |
| 56 | + } |
| 57 | + if cmd.Flags().Changed("page") { |
| 58 | + v, _ := cmd.Flags().GetInt("page") |
| 59 | + params.Page = &v |
| 60 | + } |
| 61 | + if cmd.Flags().Changed("per-page") { |
| 62 | + v, _ := cmd.Flags().GetInt("per-page") |
| 63 | + params.PerPage = &v |
| 64 | + } |
| 65 | + return params |
| 66 | +} |
0 commit comments