|
| 1 | +package mailboxes |
| 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 MailboxCmd = &cobra.Command{ |
| 15 | + Use: "mailbox <order-id>", |
| 16 | + Short: "Get mailbox list", |
| 17 | + Long: "Retrieve a paginated list of mailboxes belonging to a mail order.\n\nUse this endpoint to monitor mailboxes of your mail service, including\ntheir status, enabled protocols, attached resource counts, and\nperiodically synced usage numbers (usage may lag behind live values).", |
| 18 | + Args: cobra.MatchAll(cobra.ExactArgs(1)), |
| 19 | + Run: func(cmd *cobra.Command, args []string) { |
| 20 | + utils.EnumCheck(cmd, "sort", []string{"address", "-address"}) |
| 21 | + r, err := api.Request().MailGetMailboxListV1WithResponse(context.TODO(), args[0], mailboxParams(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 | + MailboxCmd.Flags().StringP("search", "", "", "Filter mailboxes whose email address contains the given string") |
| 32 | + MailboxCmd.Flags().StringP("sort", "", "address", "Sort mailboxes by field. Prefix with `-` for descending order. (one of: address, -address)") |
| 33 | + MailboxCmd.Flags().IntP("page", "", 0, "Page number") |
| 34 | + MailboxCmd.Flags().IntP("per-page", "", 25, "Number of items per page") |
| 35 | +} |
| 36 | + |
| 37 | +func mailboxParams(cmd *cobra.Command) *client.MailGetMailboxListV1Params { |
| 38 | + params := &client.MailGetMailboxListV1Params{} |
| 39 | + if cmd.Flags().Changed("search") { |
| 40 | + v, _ := cmd.Flags().GetString("search") |
| 41 | + params.Search = &v |
| 42 | + } |
| 43 | + if cmd.Flags().Changed("sort") { |
| 44 | + v, _ := cmd.Flags().GetString("sort") |
| 45 | + e := client.MailGetMailboxListV1ParamsSort(v) |
| 46 | + params.Sort = &e |
| 47 | + } |
| 48 | + if cmd.Flags().Changed("page") { |
| 49 | + v, _ := cmd.Flags().GetInt("page") |
| 50 | + params.Page = &v |
| 51 | + } |
| 52 | + if cmd.Flags().Changed("per-page") { |
| 53 | + v, _ := cmd.Flags().GetInt("per-page") |
| 54 | + params.PerPage = &v |
| 55 | + } |
| 56 | + return params |
| 57 | +} |
0 commit comments