|
| 1 | +package plans |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 16 | + |
| 17 | + "github.com/spf13/cobra" |
| 18 | + "github.com/stackitcloud/stackit-sdk-go/services/argus" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + limitFlag = "limit" |
| 23 | +) |
| 24 | + |
| 25 | +type inputModel struct { |
| 26 | + *globalflags.GlobalFlagModel |
| 27 | + Limit *int64 |
| 28 | +} |
| 29 | + |
| 30 | +func NewCmd() *cobra.Command { |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "plans", |
| 33 | + Short: "Lists all Argus service plans", |
| 34 | + Long: "Lists all Argus service plans.", |
| 35 | + Args: args.NoArgs, |
| 36 | + Example: examples.Build( |
| 37 | + examples.NewExample( |
| 38 | + `List all Argus service plans`, |
| 39 | + "$ stackit argus plans"), |
| 40 | + examples.NewExample( |
| 41 | + `List all Argus service plans in JSON format`, |
| 42 | + "$ stackit argus plans --output-format json"), |
| 43 | + examples.NewExample( |
| 44 | + `List up to 10 Argus service plans`, |
| 45 | + "$ stackit argus plans --limit 10"), |
| 46 | + ), |
| 47 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 48 | + ctx := context.Background() |
| 49 | + model, err := parseInput(cmd) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Configure API client |
| 55 | + apiClient, err := client.ConfigureClient(cmd) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // Call API |
| 61 | + req := buildRequest(ctx, model, apiClient) |
| 62 | + resp, err := req.Execute() |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("get Argus service plans: %w", err) |
| 65 | + } |
| 66 | + plans := *resp.Plans |
| 67 | + if len(plans) == 0 { |
| 68 | + projectLabel, err := projectname.GetProjectName(ctx, cmd) |
| 69 | + if err != nil { |
| 70 | + projectLabel = model.ProjectId |
| 71 | + } |
| 72 | + cmd.Printf("No plans found for project %q\n", projectLabel) |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + // Truncate output |
| 77 | + if model.Limit != nil && len(plans) > int(*model.Limit) { |
| 78 | + plans = plans[:*model.Limit] |
| 79 | + } |
| 80 | + |
| 81 | + return outputResult(cmd, model.OutputFormat, plans) |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + configureFlags(cmd) |
| 86 | + return cmd |
| 87 | +} |
| 88 | + |
| 89 | +func configureFlags(cmd *cobra.Command) { |
| 90 | + cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list") |
| 91 | +} |
| 92 | + |
| 93 | +func parseInput(cmd *cobra.Command) (*inputModel, error) { |
| 94 | + globalFlags := globalflags.Parse(cmd) |
| 95 | + if globalFlags.ProjectId == "" { |
| 96 | + return nil, &errors.ProjectIdError{} |
| 97 | + } |
| 98 | + |
| 99 | + limit := flags.FlagToInt64Pointer(cmd, limitFlag) |
| 100 | + if limit != nil && *limit < 1 { |
| 101 | + return nil, &errors.FlagValidationError{ |
| 102 | + Flag: limitFlag, |
| 103 | + Details: "must be greater than 0", |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return &inputModel{ |
| 108 | + GlobalFlagModel: globalFlags, |
| 109 | + Limit: limit, |
| 110 | + }, nil |
| 111 | +} |
| 112 | + |
| 113 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiListPlansRequest { |
| 114 | + req := apiClient.ListPlans(ctx, model.ProjectId) |
| 115 | + return req |
| 116 | +} |
| 117 | + |
| 118 | +func outputResult(cmd *cobra.Command, outputFormat string, plans []argus.Plan) error { |
| 119 | + switch outputFormat { |
| 120 | + case globalflags.JSONOutputFormat: |
| 121 | + details, err := json.MarshalIndent(plans, "", " ") |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("marshal Argus plans: %w", err) |
| 124 | + } |
| 125 | + cmd.Println(string(details)) |
| 126 | + |
| 127 | + return nil |
| 128 | + default: |
| 129 | + table := tables.NewTable() |
| 130 | + table.SetHeader("ID", "PLAN NAME", "DESCRIPTION") |
| 131 | + for i := range plans { |
| 132 | + o := plans[i] |
| 133 | + table.AddRow(*o.Id, *o.Name, *o.Description) |
| 134 | + table.AddSeparator() |
| 135 | + } |
| 136 | + table.EnableAutoMergeOnColumns(1) |
| 137 | + err := table.Display(cmd) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("render table: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | + } |
| 144 | +} |
0 commit comments