|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/goccy/go-yaml" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 19 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/git/client" |
| 20 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 21 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 22 | + "github.com/stackitcloud/stackit-sdk-go/services/git" |
| 23 | +) |
| 24 | + |
| 25 | +type inputModel struct { |
| 26 | + *globalflags.GlobalFlagModel |
| 27 | + Limit *int64 |
| 28 | +} |
| 29 | + |
| 30 | +const limitFlag = "limit" |
| 31 | + |
| 32 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "list", |
| 35 | + Short: "Lists instances flavors of STACKIT Git.", |
| 36 | + Long: "Lists instances flavors of STACKIT Git for the current project.", |
| 37 | + Args: args.NoArgs, |
| 38 | + Example: examples.Build( |
| 39 | + examples.NewExample( |
| 40 | + `List STACKIT Git flavors`, |
| 41 | + "$ stackit git flavor list"), |
| 42 | + examples.NewExample( |
| 43 | + "Lists up to 10 STACKIT Git flavors", |
| 44 | + "$ stackit git flavor list --limit=10", |
| 45 | + ), |
| 46 | + ), |
| 47 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 48 | + ctx := context.Background() |
| 49 | + model, err := parseInput(params.Printer, cmd) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Configure API client |
| 55 | + apiClient, err := client.ConfigureClient(params.Printer) |
| 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 STACKIT Git flavors: %w", err) |
| 65 | + } |
| 66 | + flavors := *resp.Flavors |
| 67 | + if len(flavors) == 0 { |
| 68 | + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) |
| 69 | + if err != nil { |
| 70 | + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) |
| 71 | + projectLabel = model.ProjectId |
| 72 | + } |
| 73 | + params.Printer.Info("No flavors found for project %q\n", projectLabel) |
| 74 | + return nil |
| 75 | + } else if model.Limit != nil && len(flavors) > int(*model.Limit) { |
| 76 | + flavors = (flavors)[:*model.Limit] |
| 77 | + } |
| 78 | + return outputResult(params.Printer, model.OutputFormat, flavors) |
| 79 | + }, |
| 80 | + } |
| 81 | + configureFlags(cmd) |
| 82 | + return cmd |
| 83 | +} |
| 84 | + |
| 85 | +func configureFlags(cmd *cobra.Command) { |
| 86 | + cmd.Flags().Int64(limitFlag, 0, "Limit the output to the first n elements") |
| 87 | +} |
| 88 | + |
| 89 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 90 | + globalFlags := globalflags.Parse(p, cmd) |
| 91 | + if globalFlags.ProjectId == "" { |
| 92 | + return nil, &errors.ProjectIdError{} |
| 93 | + } |
| 94 | + |
| 95 | + limit := flags.FlagToInt64Pointer(p, cmd, limitFlag) |
| 96 | + if limit != nil && *limit < 1 { |
| 97 | + return nil, &errors.FlagValidationError{ |
| 98 | + Flag: limitFlag, |
| 99 | + Details: "must be greater than 0", |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + model := inputModel{ |
| 104 | + GlobalFlagModel: globalFlags, |
| 105 | + Limit: limit, |
| 106 | + } |
| 107 | + |
| 108 | + if p.IsVerbosityDebug() { |
| 109 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 110 | + if err != nil { |
| 111 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 112 | + } else { |
| 113 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return &model, nil |
| 118 | +} |
| 119 | + |
| 120 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClient) git.ApiListFlavorsRequest { |
| 121 | + return apiClient.ListFlavors(ctx, model.ProjectId) |
| 122 | +} |
| 123 | + |
| 124 | +func outputResult(p *print.Printer, outputFormat string, flavors []git.Flavor) error { |
| 125 | + switch outputFormat { |
| 126 | + case print.JSONOutputFormat: |
| 127 | + details, err := json.MarshalIndent(flavors, "", " ") |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("marshal Observability flavor list: %w", err) |
| 130 | + } |
| 131 | + p.Outputln(string(details)) |
| 132 | + |
| 133 | + return nil |
| 134 | + case print.YAMLOutputFormat: |
| 135 | + details, err := yaml.MarshalWithOptions(flavors, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf("marshal Observability flavor list: %w", err) |
| 138 | + } |
| 139 | + p.Outputln(string(details)) |
| 140 | + |
| 141 | + return nil |
| 142 | + default: |
| 143 | + table := tables.NewTable() |
| 144 | + table.SetHeader("ID", "DESCRIPTION", "DISPLAY_NAME", "AVAILABLE", "SKU") |
| 145 | + for i := range flavors { |
| 146 | + flavor := (flavors)[i] |
| 147 | + table.AddRow( |
| 148 | + utils.PtrString(flavor.Id), |
| 149 | + utils.PtrString(flavor.Description), |
| 150 | + utils.PtrString(flavor.DisplayName), |
| 151 | + utils.PtrString(flavor.Availability), |
| 152 | + utils.PtrString(flavor.Sku), |
| 153 | + ) |
| 154 | + } |
| 155 | + err := table.Display(p) |
| 156 | + if err != nil { |
| 157 | + return fmt.Errorf("render table: %w", err) |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | + } |
| 162 | +} |
0 commit comments