Skip to content

Commit 5569dd2

Browse files
author
Alexandre Catalan
committed
feat(registry): add replication-adapter list command
- Add 'stackit registry replication-adapter list' command to list available replication adapters - Support client-side --limit flag and JSON/YAML table formatting - Add unit tests covering argument parsing, request building, and output formatting - Generate markdown documentation for the new command Relates to STACKITCRE-382
1 parent e4d47e9 commit 5569dd2

7 files changed

Lines changed: 484 additions & 0 deletions

File tree

‎docs/stackit_registry.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ stackit registry [flags]
3131

3232
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
3333
* [stackit registry artifactory](./stackit_registry_artifactory.md) - Provides functionality for Container Registry artifactories
34+
* [stackit registry replication-adapter](./stackit_registry_replication-adapter.md) - Provides functionality for Container Registry replication adapters
3435

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## stackit registry replication-adapter
2+
3+
Provides functionality for Container Registry replication adapters
4+
5+
### Synopsis
6+
7+
Provides functionality for Container Registry replication adapters.
8+
9+
```
10+
stackit registry replication-adapter [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit registry replication-adapter"
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
-y, --assume-yes If set, skips all confirmation prompts
23+
--async If set, runs the command asynchronously
24+
-o, --output-format string Output format, (one of: [json, pretty, none, yaml])
25+
-p, --project-id string Project ID
26+
--region string Target region for region-specific requests
27+
--verbosity string Verbosity of the CLI, (one of: [debug, info, warning, error]) (default "info")
28+
```
29+
30+
### SEE ALSO
31+
32+
* [stackit registry](./stackit_registry.md) - Provides functionality for Container Registry
33+
* [stackit registry replication-adapter list](./stackit_registry_replication-adapter_list.md) - Lists all Container Registry replication adapters
34+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## stackit registry replication-adapter list
2+
3+
Lists all Container Registry replication adapters
4+
5+
### Synopsis
6+
7+
Lists all available adapters for replication to/from other registries.
8+
9+
```
10+
stackit registry replication-adapter list [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all replication adapters
17+
$ stackit registry replication-adapter list
18+
19+
List all replication adapters in JSON format
20+
$ stackit registry replication-adapter list --output-format json
21+
22+
List up to 10 replication adapters
23+
$ stackit registry replication-adapter list --limit 10
24+
```
25+
26+
### Options
27+
28+
```
29+
-h, --help Help for "stackit registry replication-adapter list"
30+
--limit int Maximum number of entries to list
31+
```
32+
33+
### Options inherited from parent commands
34+
35+
```
36+
-y, --assume-yes If set, skips all confirmation prompts
37+
--async If set, runs the command asynchronously
38+
-o, --output-format string Output format, (one of: [json, pretty, none, yaml])
39+
-p, --project-id string Project ID
40+
--region string Target region for region-specific requests
41+
--verbosity string Verbosity of the CLI, (one of: [debug, info, warning, error]) (default "info")
42+
```
43+
44+
### SEE ALSO
45+
46+
* [stackit registry replication-adapter](./stackit_registry_replication-adapter.md) - Provides functionality for Container Registry replication adapters
47+

‎internal/cmd/registry/registry.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/spf13/cobra"
55

66
"github.com/stackitcloud/stackit-cli/internal/cmd/registry/artifactory"
7+
replicationadapter "github.com/stackitcloud/stackit-cli/internal/cmd/registry/replication-adapter"
78
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
89
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -23,4 +24,5 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
2324

2425
func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
2526
cmd.AddCommand(artifactory.NewCmd(params))
27+
cmd.AddCommand(replicationadapter.NewCmd(params))
2628
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)