|
| 1 | +package orders |
| 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", |
| 16 | + Short: "Get mail order list", |
| 17 | + Long: "Retrieve a paginated list of mail orders associated with your account.\n\nUse this endpoint to monitor your mail services, including their status,\nplan, attached domain, and expiration details.", |
| 18 | + Run: func(cmd *cobra.Command, args []string) { |
| 19 | + utils.EnumCheck(cmd, "status", []string{"pending_setup", "active", "suspended"}) |
| 20 | + utils.EnumCheck(cmd, "sort", []string{"created_at", "-created_at", "expires_at", "-expires_at"}) |
| 21 | + r, err := api.Request().MailGetMailOrderListV1WithResponse(context.TODO(), listParams(cmd)) |
| 22 | + if err != nil { |
| 23 | + log.Fatal(err) |
| 24 | + } |
| 25 | + |
| 26 | + output.Format(cmd, r.Body, r.StatusCode()) |
| 27 | + }, |
| 28 | +} |
| 29 | + |
| 30 | +func init() { |
| 31 | + ListCmd.Flags().StringP("domain", "", "", "Filter orders by domain name (exact match)") |
| 32 | + ListCmd.Flags().StringP("status", "", "", "Filter orders by status (one of: pending_setup, active, suspended)") |
| 33 | + ListCmd.Flags().BoolP("is-trial", "", false, "Filter orders by trial state") |
| 34 | + ListCmd.Flags().StringP("sort", "", "-created_at", "Sort orders by field. Prefix with `-` for descending order. (one of: created_at, -created_at, expires_at, -expires_at)") |
| 35 | + ListCmd.Flags().IntP("page", "", 0, "Page number") |
| 36 | + ListCmd.Flags().IntP("per-page", "", 25, "Number of items per page") |
| 37 | +} |
| 38 | + |
| 39 | +func listParams(cmd *cobra.Command) *client.MailGetMailOrderListV1Params { |
| 40 | + params := &client.MailGetMailOrderListV1Params{} |
| 41 | + if cmd.Flags().Changed("domain") { |
| 42 | + v, _ := cmd.Flags().GetString("domain") |
| 43 | + params.Domain = &v |
| 44 | + } |
| 45 | + if cmd.Flags().Changed("status") { |
| 46 | + v, _ := cmd.Flags().GetString("status") |
| 47 | + e := client.MailGetMailOrderListV1ParamsStatus(v) |
| 48 | + params.Status = &e |
| 49 | + } |
| 50 | + if cmd.Flags().Changed("is-trial") { |
| 51 | + v, _ := cmd.Flags().GetBool("is-trial") |
| 52 | + params.IsTrial = &v |
| 53 | + } |
| 54 | + if cmd.Flags().Changed("sort") { |
| 55 | + v, _ := cmd.Flags().GetString("sort") |
| 56 | + e := client.MailGetMailOrderListV1ParamsSort(v) |
| 57 | + params.Sort = &e |
| 58 | + } |
| 59 | + if cmd.Flags().Changed("page") { |
| 60 | + v, _ := cmd.Flags().GetInt("page") |
| 61 | + params.Page = &v |
| 62 | + } |
| 63 | + if cmd.Flags().Changed("per-page") { |
| 64 | + v, _ := cmd.Flags().GetInt("per-page") |
| 65 | + params.PerPage = &v |
| 66 | + } |
| 67 | + return params |
| 68 | +} |
0 commit comments