|
| 1 | +package bar |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 19 | + "gopkg.in/yaml.v2" |
| 20 | + // (...) |
| 21 | +) |
| 22 | + |
| 23 | +// Define consts for command flags |
| 24 | +const ( |
| 25 | + someArg = "MY_ARG" |
| 26 | + someFlag = "my-flag" |
| 27 | +) |
| 28 | + |
| 29 | +// Struct to model user input (arguments and/or flags) |
| 30 | +type inputModel struct { |
| 31 | + *globalflags.GlobalFlagModel |
| 32 | + MyArg string |
| 33 | + MyFlag *string |
| 34 | +} |
| 35 | + |
| 36 | +// "bar" command constructor |
| 37 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 38 | + cmd := &cobra.Command{ |
| 39 | + Use: "bar", |
| 40 | + Short: "Short description of the command (is shown in the help of parent command)", |
| 41 | + Long: "Long description of the command. Can contain some more information about the command usage. It is shown in the help of the current command.", |
| 42 | + Args: args.SingleArg(someArg, utils.ValidateUUID), // Validate argument, with an optional validation function |
| 43 | + Example: examples.Build( |
| 44 | + examples.NewExample( |
| 45 | + `Do something with command "bar"`, |
| 46 | + "$ stackit foo bar arg-value --my-flag flag-value"), |
| 47 | + //... |
| 48 | + ), |
| 49 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 50 | + ctx := context.Background() |
| 51 | + model, err := parseInput(params.Printer, cmd, args) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + // Configure API client |
| 57 | + apiClient, err := client.ConfigureClient(params.Printer, cmd) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + // Call API |
| 63 | + req := buildRequest(ctx, model, apiClient) |
| 64 | + resp, err := req.Execute() |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("(...): %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd) |
| 70 | + if err != nil { |
| 71 | + projectLabel = model.ProjectId |
| 72 | + } |
| 73 | + |
| 74 | + // Check API response "resp" and output accordingly |
| 75 | + if resp.Item == nil { |
| 76 | + params.Printer.Info("(...)", projectLabel) |
| 77 | + return nil |
| 78 | + } |
| 79 | + return outputResult(params.Printer, cmd, model.OutputFormat, instances) |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + configureFlags(cmd) |
| 84 | + return cmd |
| 85 | +} |
| 86 | + |
| 87 | +// Configure command flags (type, default value, and description) |
| 88 | +func configureFlags(cmd *cobra.Command) { |
| 89 | + cmd.Flags().StringP(myFlag, "defaultValue", "My flag description") |
| 90 | +} |
| 91 | + |
| 92 | +// Parse user input (arguments and/or flags) |
| 93 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 94 | + myArg := inputArgs[0] |
| 95 | + |
| 96 | + globalFlags := globalflags.Parse(cmd) |
| 97 | + if globalFlags.ProjectId == "" { |
| 98 | + return nil, &errors.ProjectIdError{} |
| 99 | + } |
| 100 | + |
| 101 | + model := inputModel{ |
| 102 | + GlobalFlagModel: globalFlags, |
| 103 | + MyArg: myArg, |
| 104 | + MyFlag: flags.FlagToStringPointer(cmd, myFlag), |
| 105 | + } |
| 106 | + |
| 107 | + // Write the input model to the debug logs |
| 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 | +// Build request to the API |
| 121 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *foo.APIClient) foo.ApiListInstancesRequest { |
| 122 | + req := apiClient.GetBar(ctx, model.ProjectId, model.MyArg, someParam) |
| 123 | + return req |
| 124 | +} |
| 125 | + |
| 126 | +// Output result based on the configured output format |
| 127 | +func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, resources []foo.Resource) error { |
| 128 | + switch outputFormat { |
| 129 | + case print.JSONOutputFormat: |
| 130 | + details, err := json.MarshalIndent(resources, "", " ") |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("marshal resource list: %w", err) |
| 133 | + } |
| 134 | + p.Outputln(string(details)) |
| 135 | + return nil |
| 136 | + case print.YAMLOutputFormat: |
| 137 | + details, err := yaml.Marshal(resources) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("marshal resource list: %w", err) |
| 140 | + } |
| 141 | + p.Outputln(string(details)) |
| 142 | + return nil |
| 143 | + default: |
| 144 | + table := tables.NewTable() |
| 145 | + table.SetHeader("ID", "NAME", "STATE") |
| 146 | + for i := range resources { |
| 147 | + resource := resources[i] |
| 148 | + table.AddRow(*resource.ResourceId, *resource.Name, *resource.State) |
| 149 | + } |
| 150 | + err := table.Display(cmd) |
| 151 | + if err != nil { |
| 152 | + return fmt.Errorf("render table: %w", err) |
| 153 | + } |
| 154 | + return nil |
| 155 | + } |
| 156 | +} |
0 commit comments