|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 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/print" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/resourcemanager/client" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 17 | + |
| 18 | + "github.com/spf13/cobra" |
| 19 | + resourcemanager "github.com/stackitcloud/stackit-sdk-go/services/resourcemanager/v0api" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + includeParentsFlag = "include-parents" |
| 24 | + |
| 25 | + containerIdArg = "CONTAINER_ID" |
| 26 | +) |
| 27 | + |
| 28 | +type inputModel struct { |
| 29 | + *globalflags.GlobalFlagModel |
| 30 | + ArgContainerId string |
| 31 | + IncludeParents bool |
| 32 | +} |
| 33 | + |
| 34 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 35 | + cmd := &cobra.Command{ |
| 36 | + Use: "describe", |
| 37 | + Short: "Shows details of a STACKIT folder", |
| 38 | + Long: "Shows details of a STACKIT folder.", |
| 39 | + Args: args.SingleOptionalArg(containerIdArg, utils.ValidateUUID), |
| 40 | + Example: examples.Build( |
| 41 | + examples.NewExample( |
| 42 | + `Get the details of the configured STACKIT folder`, |
| 43 | + "$ stackit folder describe"), |
| 44 | + examples.NewExample( |
| 45 | + `Get the details of a STACKIT folder by explicitly providing the container ID`, |
| 46 | + "$ stackit folder describe xxx"), |
| 47 | + examples.NewExample( |
| 48 | + `Get the details of the configured STACKIT folder, including details of the parent resources`, |
| 49 | + "$ stackit folder describe --include-parents"), |
| 50 | + ), |
| 51 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 52 | + ctx := context.Background() |
| 53 | + model, err := parseInput(params.Printer, cmd, args) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + // Configure API client |
| 59 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + // Call API |
| 65 | + req := buildRequest(ctx, model, apiClient) |
| 66 | + resp, err := req.Execute() |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("read folder details: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + return outputResult(params.Printer, model.OutputFormat, resp) |
| 72 | + }, |
| 73 | + } |
| 74 | + configureFlags(cmd) |
| 75 | + return cmd |
| 76 | +} |
| 77 | + |
| 78 | +func configureFlags(cmd *cobra.Command) { |
| 79 | + cmd.Flags().Bool(includeParentsFlag, false, "When true, the details of the parent resources will be included in the output") |
| 80 | +} |
| 81 | + |
| 82 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 83 | + var containerId string |
| 84 | + if len(inputArgs) > 0 { |
| 85 | + containerId = inputArgs[0] |
| 86 | + } |
| 87 | + |
| 88 | + globalFlags := globalflags.Parse(p, cmd) |
| 89 | + if globalFlags.ProjectId == "" && containerId == "" { |
| 90 | + return nil, fmt.Errorf("container ID needs to be provided either as an argument or as a flag") |
| 91 | + } |
| 92 | + |
| 93 | + if containerId == "" { |
| 94 | + containerId = globalFlags.ProjectId |
| 95 | + } |
| 96 | + |
| 97 | + model := inputModel{ |
| 98 | + GlobalFlagModel: globalFlags, |
| 99 | + ArgContainerId: containerId, |
| 100 | + IncludeParents: flags.FlagToBoolValue(p, cmd, includeParentsFlag), |
| 101 | + } |
| 102 | + |
| 103 | + p.DebugInputModel(model) |
| 104 | + return &model, nil |
| 105 | +} |
| 106 | + |
| 107 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *resourcemanager.APIClient) resourcemanager.ApiGetFolderDetailsRequest { |
| 108 | + req := apiClient.DefaultAPI.GetFolderDetails(ctx, model.ArgContainerId) |
| 109 | + req.IncludeParents(model.IncludeParents) |
| 110 | + return req |
| 111 | +} |
| 112 | + |
| 113 | +func outputResult(p *print.Printer, outputFormat string, folder *resourcemanager.GetFolderDetailsResponse) error { |
| 114 | + if folder == nil { |
| 115 | + return fmt.Errorf("response not set") |
| 116 | + } |
| 117 | + |
| 118 | + return p.OutputResult(outputFormat, folder, func() error { |
| 119 | + table := tables.NewTable() |
| 120 | + table.AddRow("ID", folder.FolderId) |
| 121 | + table.AddSeparator() |
| 122 | + table.AddRow("NAME", folder.Name) |
| 123 | + table.AddSeparator() |
| 124 | + table.AddRow("CREATION", folder.CreationTime) |
| 125 | + table.AddSeparator() |
| 126 | + table.AddRow("CONTAINER ID", folder.ContainerId) |
| 127 | + table.AddSeparator() |
| 128 | + table.AddRow("PARENT ID", folder.Parent.Id) |
| 129 | + |
| 130 | + var labels string |
| 131 | + if folder.Labels != nil { |
| 132 | + labels = utils.JoinStringMap(*folder.Labels, "=", ", ") |
| 133 | + } |
| 134 | + table.AddSeparator() |
| 135 | + table.AddRow("LABELS", labels) |
| 136 | + |
| 137 | + err := table.Display(p) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("render table: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | + }) |
| 144 | +} |
0 commit comments