|
| 1 | +package catalog |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/hostinger/api-cli/api" |
| 6 | + "github.com/hostinger/api-cli/client" |
| 7 | + "github.com/hostinger/api-cli/output" |
| 8 | + "github.com/hostinger/api-cli/utils" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "log" |
| 11 | +) |
| 12 | + |
| 13 | +var ListCmd = &cobra.Command{ |
| 14 | + Use: "list", |
| 15 | + Short: "Get catalog item list", |
| 16 | + Long: `This endpoint retrieves a list of available catalog items that can be ordered. |
| 17 | +
|
| 18 | +Prices in the response are displayed in cents.`, |
| 19 | + Run: func(cmd *cobra.Command, args []string) { |
| 20 | + r, err := api.Request().BillingGetCatalogItemListV1WithResponse(context.TODO(), catalogListRequestParameters(cmd)) |
| 21 | + if err != nil { |
| 22 | + log.Fatal(err) |
| 23 | + } |
| 24 | + |
| 25 | + output.Format(cmd, r.Body, r.StatusCode()) |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +func init() { |
| 30 | + ListCmd.Flags().StringP("category", "", "", "Filter catalog items by category (one of: DOMAIN, VPS)") |
| 31 | + ListCmd.Flags().StringP("name", "", "", "Filter catalog items by name. Use * for wildcard search, e.g. .COM* to find .com domain") |
| 32 | +} |
| 33 | + |
| 34 | +func catalogListRequestParameters(cmd *cobra.Command) *client.BillingGetCatalogItemListV1Params { |
| 35 | + category, _ := cmd.Flags().GetString("category") |
| 36 | + name, _ := cmd.Flags().GetString("name") |
| 37 | + |
| 38 | + params := &client.BillingGetCatalogItemListV1Params{ |
| 39 | + Name: utils.StringPtrOrNil(name), |
| 40 | + } |
| 41 | + |
| 42 | + if category != "" { |
| 43 | + c := client.BillingGetCatalogItemListV1ParamsCategory(category) |
| 44 | + params.Category = &c |
| 45 | + } |
| 46 | + |
| 47 | + return params |
| 48 | +} |
0 commit comments