|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api" |
| 9 | + |
| 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/print" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/registry/client" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 19 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + limitFlag = "limit" |
| 24 | +) |
| 25 | + |
| 26 | +type inputModel struct { |
| 27 | + *globalflags.GlobalFlagModel |
| 28 | + Limit *int64 |
| 29 | +} |
| 30 | + |
| 31 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "list", |
| 34 | + Short: "Lists all Container Registry replication adapters", |
| 35 | + Long: "Lists all available adapters for replication to/from other registries.", |
| 36 | + Args: args.NoArgs, |
| 37 | + Example: examples.Build( |
| 38 | + examples.NewExample( |
| 39 | + `List all replication adapters`, |
| 40 | + "$ stackit registry replication-adapter list", |
| 41 | + ), |
| 42 | + examples.NewExample( |
| 43 | + `List all replication adapters in JSON format`, |
| 44 | + "$ stackit registry replication-adapter list --output-format json", |
| 45 | + ), |
| 46 | + examples.NewExample( |
| 47 | + `List up to 10 replication adapters`, |
| 48 | + "$ stackit registry replication-adapter list --limit 10", |
| 49 | + ), |
| 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("list replication adapters: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + var adapters []registry.ReplicationAdapter |
| 72 | + if resp != nil { |
| 73 | + adapters = resp.ReplicationAdapters |
| 74 | + } |
| 75 | + |
| 76 | + if model.Limit != nil && len(adapters) > int(*model.Limit) { |
| 77 | + adapters = adapters[:*model.Limit] |
| 78 | + } |
| 79 | + |
| 80 | + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) |
| 81 | + if err != nil { |
| 82 | + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) |
| 83 | + projectLabel = model.ProjectId |
| 84 | + } |
| 85 | + |
| 86 | + return outputResult(params.Printer, model.OutputFormat, projectLabel, adapters) |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + configureFlags(cmd) |
| 91 | + return cmd |
| 92 | +} |
| 93 | + |
| 94 | +func configureFlags(cmd *cobra.Command) { |
| 95 | + cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list") |
| 96 | +} |
| 97 | + |
| 98 | +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { |
| 99 | + globalFlags := globalflags.Parse(p, cmd) |
| 100 | + if globalFlags.ProjectId == "" { |
| 101 | + return nil, &errors.ProjectIdError{} |
| 102 | + } |
| 103 | + |
| 104 | + limit := flags.FlagToInt64Pointer(p, cmd, limitFlag) |
| 105 | + if limit != nil && *limit < 1 { |
| 106 | + return nil, &errors.FlagValidationError{ |
| 107 | + Flag: limitFlag, |
| 108 | + Details: "must be greater than 0", |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + model := inputModel{ |
| 113 | + GlobalFlagModel: globalFlags, |
| 114 | + Limit: limit, |
| 115 | + } |
| 116 | + |
| 117 | + p.DebugInputModel(model) |
| 118 | + return &model, nil |
| 119 | +} |
| 120 | + |
| 121 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *registry.APIClient) registry.ApiListReplicationAdaptersRequest { |
| 122 | + return apiClient.DefaultAPI.ListReplicationAdapters(ctx, model.ProjectId, model.Region) |
| 123 | +} |
| 124 | + |
| 125 | +func outputResult(p *print.Printer, outputFormat, projectLabel string, adapters []registry.ReplicationAdapter) error { |
| 126 | + return p.OutputResult(outputFormat, adapters, func() error { |
| 127 | + if len(adapters) == 0 { |
| 128 | + p.Outputf("No replication adapters found for project %q\n", projectLabel) |
| 129 | + return nil |
| 130 | + } |
| 131 | + |
| 132 | + table := tables.NewTable() |
| 133 | + table.SetHeader("REGISTRY TYPE", "DISPLAY NAME", "CREDENTIAL TYPE") |
| 134 | + |
| 135 | + for _, adapter := range adapters { |
| 136 | + table.AddRow( |
| 137 | + adapter.RegistryType, |
| 138 | + adapter.DisplayName, |
| 139 | + adapter.CredentialType, |
| 140 | + ) |
| 141 | + } |
| 142 | + |
| 143 | + p.Outputln(table.Render()) |
| 144 | + return nil |
| 145 | + }) |
| 146 | +} |
0 commit comments