Skip to content

Commit 71b3950

Browse files
authored
fix(git): print valid JSON/YAML output for list cmds (#1039)
relates to STACKITCLI-265 / #893
1 parent 611d185 commit 71b3950

File tree

6 files changed

+44
-30
lines changed

6 files changed

+44
-30
lines changed

docs/stackit_git_instance_create.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ stackit git instance create [flags]
1717
$ stackit git instance create --name my-new-instance
1818
1919
Create a instance with name 'my-new-instance' and flavor
20-
$ stackit git instance create --name my-new-instance --flavor git-100'
20+
$ stackit git instance create --name my-new-instance --flavor git-100
2121
2222
Create a instance with name 'my-new-instance' and acl
23-
$ stackit git instance create --name my-new-instance --acl 1.1.1.1/1'
23+
$ stackit git instance create --name my-new-instance --acl 1.1.1.1/1
2424
```
2525

2626
### Options

internal/cmd/git/flavor/list/list.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6060
if err != nil {
6161
return fmt.Errorf("get STACKIT Git flavors: %w", err)
6262
}
63-
flavors := *resp.Flavors
64-
if len(flavors) == 0 {
65-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
66-
if err != nil {
67-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
68-
projectLabel = model.ProjectId
69-
}
70-
params.Printer.Info("No flavors found for project %q\n", projectLabel)
71-
return nil
72-
} else if model.Limit != nil && len(flavors) > int(*model.Limit) {
63+
flavors := resp.GetFlavors()
64+
65+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
66+
if err != nil {
67+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
68+
projectLabel = model.ProjectId
69+
}
70+
71+
// Truncate output
72+
if model.Limit != nil && len(flavors) > int(*model.Limit) {
7373
flavors = (flavors)[:*model.Limit]
7474
}
75-
return outputResult(params.Printer, model.OutputFormat, flavors)
75+
76+
return outputResult(params.Printer, model.OutputFormat, projectLabel, flavors)
7677
},
7778
}
7879
configureFlags(cmd)
@@ -110,8 +111,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClie
110111
return apiClient.ListFlavors(ctx, model.ProjectId)
111112
}
112113

113-
func outputResult(p *print.Printer, outputFormat string, flavors []git.Flavor) error {
114+
func outputResult(p *print.Printer, outputFormat, projectLabel string, flavors []git.Flavor) error {
114115
return p.OutputResult(outputFormat, flavors, func() error {
116+
if len(flavors) == 0 {
117+
p.Outputf("No flavors found for project %q\n", projectLabel)
118+
return nil
119+
}
120+
115121
table := tables.NewTable()
116122
table.SetHeader("ID", "DESCRIPTION", "DISPLAY_NAME", "AVAILABLE", "SKU")
117123
for i := range flavors {

internal/cmd/git/flavor/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func TestBuildRequest(t *testing.T) {
161161
func TestOutputResult(t *testing.T) {
162162
type args struct {
163163
outputFormat string
164+
projectLabel string
164165
flavors []git.Flavor
165166
}
166167
tests := []struct {
@@ -192,7 +193,7 @@ func TestOutputResult(t *testing.T) {
192193
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
193194
for _, tt := range tests {
194195
t.Run(tt.name, func(t *testing.T) {
195-
if err := outputResult(p, tt.args.outputFormat, tt.args.flavors); (err != nil) != tt.wantErr {
196+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.flavors); (err != nil) != tt.wantErr {
196197
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
197198
}
198199
})

internal/cmd/git/instance/create/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4646
),
4747
examples.NewExample(
4848
`Create a instance with name 'my-new-instance' and flavor`,
49-
`$ stackit git instance create --name my-new-instance --flavor git-100'`,
49+
`$ stackit git instance create --name my-new-instance --flavor git-100`,
5050
),
5151
examples.NewExample(
5252
`Create a instance with name 'my-new-instance' and acl`,
53-
`$ stackit git instance create --name my-new-instance --acl 1.1.1.1/1'`,
53+
`$ stackit git instance create --name my-new-instance --acl 1.1.1.1/1`,
5454
),
5555
),
5656
RunE: func(cmd *cobra.Command, args []string) (err error) {

internal/cmd/git/instance/list/list.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6161
if err != nil {
6262
return fmt.Errorf("get STACKIT Git instances: %w", err)
6363
}
64-
instances := *resp.Instances
65-
if len(instances) == 0 {
66-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
67-
if err != nil {
68-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
69-
projectLabel = model.ProjectId
70-
}
71-
params.Printer.Info("No instances found for project %q\n", projectLabel)
72-
return nil
73-
} else if model.Limit != nil && len(instances) > int(*model.Limit) {
64+
instances := resp.GetInstances()
65+
66+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
67+
if err != nil {
68+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
69+
projectLabel = model.ProjectId
70+
}
71+
72+
// Truncate output
73+
if model.Limit != nil && len(instances) > int(*model.Limit) {
7474
instances = (instances)[:*model.Limit]
7575
}
76-
return outputResult(params.Printer, model.OutputFormat, instances)
76+
77+
return outputResult(params.Printer, model.OutputFormat, projectLabel, instances)
7778
},
7879
}
7980
configureFlags(cmd)
@@ -111,8 +112,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClie
111112
return apiClient.ListInstances(ctx, model.ProjectId)
112113
}
113114

114-
func outputResult(p *print.Printer, outputFormat string, instances []git.Instance) error {
115+
func outputResult(p *print.Printer, outputFormat, projectLabel string, instances []git.Instance) error {
115116
return p.OutputResult(outputFormat, instances, func() error {
117+
if len(instances) == 0 {
118+
p.Outputf("No instances found for project %q\n", projectLabel)
119+
return nil
120+
}
121+
116122
table := tables.NewTable()
117123
table.SetHeader("ID", "NAME", "URL", "VERSION", "STATE", "CREATED")
118124
for i := range instances {

internal/cmd/git/instance/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func TestBuildRequest(t *testing.T) {
161161
func TestOutputResult(t *testing.T) {
162162
type args struct {
163163
outputFormat string
164+
projectLabel string
164165
instances []git.Instance
165166
}
166167
tests := []struct {
@@ -192,7 +193,7 @@ func TestOutputResult(t *testing.T) {
192193
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
193194
for _, tt := range tests {
194195
t.Run(tt.name, func(t *testing.T) {
195-
if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr {
196+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.instances); (err != nil) != tt.wantErr {
196197
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
197198
}
198199
})

0 commit comments

Comments
 (0)