|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/goccy/go-yaml" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 19 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 20 | +) |
| 21 | + |
| 22 | +type inputModel struct { |
| 23 | + *globalflags.GlobalFlagModel |
| 24 | + ImageId string |
| 25 | +} |
| 26 | + |
| 27 | +const imageIdArg = "IMAGE_ID" |
| 28 | + |
| 29 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: fmt.Sprintf("describe %s", imageIdArg), |
| 32 | + Short: "Describes image", |
| 33 | + Long: "Describes an image by its internal ID.", |
| 34 | + Args: args.SingleArg(imageIdArg, utils.ValidateUUID), |
| 35 | + Example: examples.Build( |
| 36 | + examples.NewExample(`Describe image "xxx"`, `$ stackit beta image describe xxx`), |
| 37 | + ), |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + ctx := context.Background() |
| 40 | + model, err := parseInput(p, cmd, args) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + // Configure API client |
| 46 | + apiClient, err := client.ConfigureClient(p) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + // Call API |
| 52 | + request := buildRequest(ctx, model, apiClient) |
| 53 | + |
| 54 | + image, err := request.Execute() |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("get image: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + if err := outputResult(p, model, image); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + return cmd |
| 68 | +} |
| 69 | + |
| 70 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiGetImageRequest { |
| 71 | + request := apiClient.GetImage(ctx, model.ProjectId, model.ImageId) |
| 72 | + return request |
| 73 | +} |
| 74 | + |
| 75 | +func parseInput(p *print.Printer, cmd *cobra.Command, cliArgs []string) (*inputModel, error) { |
| 76 | + globalFlags := globalflags.Parse(p, cmd) |
| 77 | + if globalFlags.ProjectId == "" { |
| 78 | + return nil, &errors.ProjectIdError{} |
| 79 | + } |
| 80 | + |
| 81 | + model := inputModel{ |
| 82 | + GlobalFlagModel: globalFlags, |
| 83 | + ImageId: cliArgs[0], |
| 84 | + } |
| 85 | + |
| 86 | + if p.IsVerbosityDebug() { |
| 87 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 88 | + if err != nil { |
| 89 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 90 | + } else { |
| 91 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return &model, nil |
| 96 | +} |
| 97 | + |
| 98 | +func outputResult(p *print.Printer, model *inputModel, resp *iaas.Image) error { |
| 99 | + switch model.OutputFormat { |
| 100 | + case print.JSONOutputFormat: |
| 101 | + details, err := json.MarshalIndent(resp, "", " ") |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("marshal image: %w", err) |
| 104 | + } |
| 105 | + p.Outputln(string(details)) |
| 106 | + |
| 107 | + return nil |
| 108 | + case print.YAMLOutputFormat: |
| 109 | + details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true)) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("marshal image: %w", err) |
| 112 | + } |
| 113 | + p.Outputln(string(details)) |
| 114 | + |
| 115 | + return nil |
| 116 | + default: |
| 117 | + table := tables.NewTable() |
| 118 | + if id := resp.Id; id != nil { |
| 119 | + table.AddRow("ID", *id) |
| 120 | + } |
| 121 | + table.AddSeparator() |
| 122 | + |
| 123 | + if name := resp.Name; name != nil { |
| 124 | + table.AddRow("NAME", *name) |
| 125 | + table.AddSeparator() |
| 126 | + } |
| 127 | + if format := resp.DiskFormat; format != nil { |
| 128 | + table.AddRow("FORMAT", *format) |
| 129 | + table.AddSeparator() |
| 130 | + } |
| 131 | + if diskSize := resp.MinDiskSize; diskSize != nil { |
| 132 | + table.AddRow("DISK SIZE", *diskSize) |
| 133 | + table.AddSeparator() |
| 134 | + } |
| 135 | + if ramSize := resp.MinRam; ramSize != nil { |
| 136 | + table.AddRow("RAM SIZE", *ramSize) |
| 137 | + table.AddSeparator() |
| 138 | + } |
| 139 | + if config := resp.Config; config != nil { |
| 140 | + if os := config.OperatingSystem; os != nil { |
| 141 | + table.AddRow("OPERATING SYSTEM", *os) |
| 142 | + table.AddSeparator() |
| 143 | + } |
| 144 | + if distro := config.OperatingSystemDistro; distro != nil { |
| 145 | + table.AddRow("OPERATING SYSTEM DISTRIBUTION", *distro) |
| 146 | + table.AddSeparator() |
| 147 | + } |
| 148 | + if version := config.OperatingSystemVersion; version != nil { |
| 149 | + table.AddRow("OPERATING SYSTEM VERSION", *version) |
| 150 | + table.AddSeparator() |
| 151 | + } |
| 152 | + if uefi := config.Uefi; uefi != nil { |
| 153 | + table.AddRow("UEFI BOOT", *uefi) |
| 154 | + table.AddSeparator() |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if resp.Labels != nil && len(*resp.Labels) > 0 { |
| 159 | + labels := []string{} |
| 160 | + for key, value := range *resp.Labels { |
| 161 | + labels = append(labels, fmt.Sprintf("%s: %s", key, value)) |
| 162 | + } |
| 163 | + table.AddRow("LABELS", strings.Join(labels, "\n")) |
| 164 | + table.AddSeparator() |
| 165 | + } |
| 166 | + |
| 167 | + if err := table.Display(p); err != nil { |
| 168 | + return fmt.Errorf("render table: %w", err) |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | + } |
| 173 | +} |
0 commit comments