From 1dbbe156e2d78f7c8106097b51999818233ff70e Mon Sep 17 00:00:00 2001 From: Ruben Hoenle Date: Tue, 21 Oct 2025 15:37:36 +0200 Subject: [PATCH 1/3] fix(git): print valid JSON/YAML output for list cmds relates to STACKITCLI-265 / #893 --- internal/cmd/git/flavor/list/list.go | 30 ++++++++++++--------- internal/cmd/git/flavor/list/list_test.go | 3 ++- internal/cmd/git/instance/list/list.go | 30 ++++++++++++--------- internal/cmd/git/instance/list/list_test.go | 3 ++- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/internal/cmd/git/flavor/list/list.go b/internal/cmd/git/flavor/list/list.go index 68b674830..c8c8fe583 100644 --- a/internal/cmd/git/flavor/list/list.go +++ b/internal/cmd/git/flavor/list/list.go @@ -60,19 +60,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command { if err != nil { return fmt.Errorf("get STACKIT Git flavors: %w", err) } - flavors := *resp.Flavors - if len(flavors) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) - if err != nil { - params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) - projectLabel = model.ProjectId - } - params.Printer.Info("No flavors found for project %q\n", projectLabel) - return nil - } else if model.Limit != nil && len(flavors) > int(*model.Limit) { + flavors := resp.GetFlavors() + + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) + projectLabel = model.ProjectId + } + + // Truncate output + if model.Limit != nil && len(flavors) > int(*model.Limit) { flavors = (flavors)[:*model.Limit] } - return outputResult(params.Printer, model.OutputFormat, flavors) + + return outputResult(params.Printer, model.OutputFormat, projectLabel, flavors) }, } configureFlags(cmd) @@ -110,8 +111,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClie return apiClient.ListFlavors(ctx, model.ProjectId) } -func outputResult(p *print.Printer, outputFormat string, flavors []git.Flavor) error { +func outputResult(p *print.Printer, outputFormat, projectLabel string, flavors []git.Flavor) error { return p.OutputResult(outputFormat, flavors, func() error { + if len(flavors) == 0 { + p.Outputf("No flavors found for project %q\n", projectLabel) + return nil + } + table := tables.NewTable() table.SetHeader("ID", "DESCRIPTION", "DISPLAY_NAME", "AVAILABLE", "SKU") for i := range flavors { diff --git a/internal/cmd/git/flavor/list/list_test.go b/internal/cmd/git/flavor/list/list_test.go index d5a6e2f54..8c2aca50a 100644 --- a/internal/cmd/git/flavor/list/list_test.go +++ b/internal/cmd/git/flavor/list/list_test.go @@ -161,6 +161,7 @@ func TestBuildRequest(t *testing.T) { func TestOutputResult(t *testing.T) { type args struct { outputFormat string + projectLabel string flavors []git.Flavor } tests := []struct { @@ -192,7 +193,7 @@ func TestOutputResult(t *testing.T) { p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := outputResult(p, tt.args.outputFormat, tt.args.flavors); (err != nil) != tt.wantErr { + if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.flavors); (err != nil) != tt.wantErr { t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/internal/cmd/git/instance/list/list.go b/internal/cmd/git/instance/list/list.go index 1499ecfdf..161af2df4 100644 --- a/internal/cmd/git/instance/list/list.go +++ b/internal/cmd/git/instance/list/list.go @@ -61,19 +61,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command { if err != nil { return fmt.Errorf("get STACKIT Git instances: %w", err) } - instances := *resp.Instances - if len(instances) == 0 { - projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) - if err != nil { - params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) - projectLabel = model.ProjectId - } - params.Printer.Info("No instances found for project %q\n", projectLabel) - return nil - } else if model.Limit != nil && len(instances) > int(*model.Limit) { + instances := resp.GetInstances() + + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) + projectLabel = model.ProjectId + } + + // Truncate output + if model.Limit != nil && len(instances) > int(*model.Limit) { instances = (instances)[:*model.Limit] } - return outputResult(params.Printer, model.OutputFormat, instances) + + return outputResult(params.Printer, model.OutputFormat, projectLabel, instances) }, } configureFlags(cmd) @@ -111,8 +112,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClie return apiClient.ListInstances(ctx, model.ProjectId) } -func outputResult(p *print.Printer, outputFormat string, instances []git.Instance) error { +func outputResult(p *print.Printer, outputFormat, projectLabel string, instances []git.Instance) error { return p.OutputResult(outputFormat, instances, func() error { + if len(instances) == 0 { + p.Outputf("No instances found for project %q\n", projectLabel) + return nil + } + table := tables.NewTable() table.SetHeader("ID", "NAME", "URL", "VERSION", "STATE", "CREATED") for i := range instances { diff --git a/internal/cmd/git/instance/list/list_test.go b/internal/cmd/git/instance/list/list_test.go index 8d6ac21a6..36fa36f10 100644 --- a/internal/cmd/git/instance/list/list_test.go +++ b/internal/cmd/git/instance/list/list_test.go @@ -161,6 +161,7 @@ func TestBuildRequest(t *testing.T) { func TestOutputResult(t *testing.T) { type args struct { outputFormat string + projectLabel string instances []git.Instance } tests := []struct { @@ -192,7 +193,7 @@ func TestOutputResult(t *testing.T) { p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr { + if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.instances); (err != nil) != tt.wantErr { t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) } }) From 277bf25ae9fedbc86934b9026bdf6ed37299ddea Mon Sep 17 00:00:00 2001 From: Ruben Hoenle Date: Tue, 28 Oct 2025 16:32:36 +0100 Subject: [PATCH 2/3] fix typo in git instance create cmd examples --- internal/cmd/git/instance/create/create.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cmd/git/instance/create/create.go b/internal/cmd/git/instance/create/create.go index 296a44598..8c6c56ac4 100644 --- a/internal/cmd/git/instance/create/create.go +++ b/internal/cmd/git/instance/create/create.go @@ -46,11 +46,11 @@ func NewCmd(params *params.CmdParams) *cobra.Command { ), examples.NewExample( `Create a instance with name 'my-new-instance' and flavor`, - `$ stackit git instance create --name my-new-instance --flavor git-100'`, + `$ stackit git instance create --name my-new-instance --flavor git-100`, ), examples.NewExample( `Create a instance with name 'my-new-instance' and acl`, - `$ stackit git instance create --name my-new-instance --acl 1.1.1.1/1'`, + `$ stackit git instance create --name my-new-instance --acl 1.1.1.1/1`, ), ), RunE: func(cmd *cobra.Command, args []string) (err error) { From 0b2e6d7766215e2b9453c43ed32d660f5157a60a Mon Sep 17 00:00:00 2001 From: Ruben Hoenle Date: Tue, 28 Oct 2025 16:34:40 +0100 Subject: [PATCH 3/3] update docs --- docs/stackit_git_instance_create.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/stackit_git_instance_create.md b/docs/stackit_git_instance_create.md index e5d8bba93..a82c92ec4 100644 --- a/docs/stackit_git_instance_create.md +++ b/docs/stackit_git_instance_create.md @@ -17,10 +17,10 @@ stackit git instance create [flags] $ stackit git instance create --name my-new-instance Create a instance with name 'my-new-instance' and flavor - $ stackit git instance create --name my-new-instance --flavor git-100' + $ stackit git instance create --name my-new-instance --flavor git-100 Create a instance with name 'my-new-instance' and acl - $ stackit git instance create --name my-new-instance --acl 1.1.1.1/1' + $ stackit git instance create --name my-new-instance --acl 1.1.1.1/1 ``` ### Options