Skip to content

Commit e17c7ff

Browse files
authored
fix(runcommand): print valid JSON/YAML output for list cmds (#1378)
relates to STACKITCLI-385 and #893
1 parent 47f322d commit e17c7ff

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

internal/cmd/server/command/list/list.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,14 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7878
if err != nil {
7979
return fmt.Errorf("list server commands: %w", err)
8080
}
81-
if commands := resp.Items; commands == nil || len(*commands) == 0 {
82-
params.Printer.Info("No commands found for server %s\n", serverLabel)
83-
return nil
84-
}
85-
commands := *resp.Items
81+
82+
commands := resp.GetItems()
83+
8684
// Truncate output
8785
if model.Limit != nil && len(commands) > int(*model.Limit) {
8886
commands = commands[:*model.Limit]
8987
}
90-
return outputResult(params.Printer, model.OutputFormat, commands)
88+
return outputResult(params.Printer, model.OutputFormat, serverLabel, commands)
9189
},
9290
}
9391
configureFlags(cmd)
@@ -131,8 +129,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *runcommand.
131129
return req
132130
}
133131

134-
func outputResult(p *print.Printer, outputFormat string, commands []runcommand.Commands) error {
132+
func outputResult(p *print.Printer, outputFormat, serverLabel string, commands []runcommand.Commands) error {
135133
return p.OutputResult(outputFormat, commands, func() error {
134+
if len(commands) == 0 {
135+
p.Outputf("No commands found for server %s\n", serverLabel)
136+
return nil
137+
}
136138
table := tables.NewTable()
137139
table.SetHeader("ID", "TEMPLATE NAME", "TEMPLATE TITLE", "STATUS", "STARTED_AT", "FINISHED_AT")
138140
for i := range commands {

internal/cmd/server/command/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func TestBuildRequest(t *testing.T) {
157157
func TestOutputResult(t *testing.T) {
158158
type args struct {
159159
outputFormat string
160+
serverLabel string
160161
commands []runcommand.Commands
161162
}
162163
tests := []struct {
@@ -173,7 +174,7 @@ func TestOutputResult(t *testing.T) {
173174
params := testparams.NewTestParams()
174175
for _, tt := range tests {
175176
t.Run(tt.name, func(t *testing.T) {
176-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.commands); (err != nil) != tt.wantErr {
177+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.serverLabel, tt.args.commands); (err != nil) != tt.wantErr {
177178
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
178179
}
179180
})

internal/cmd/server/command/template/list/list.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6262
if err != nil {
6363
return fmt.Errorf("list server command templates: %w", err)
6464
}
65-
if templates := resp.Items; templates == nil || len(*templates) == 0 {
66-
params.Printer.Info("No commands templates found\n")
67-
return nil
68-
}
69-
templates := *resp.Items
65+
66+
templates := resp.GetItems()
7067

7168
// Truncate output
7269
if model.Limit != nil && len(templates) > int(*model.Limit) {
@@ -113,6 +110,10 @@ func buildRequest(ctx context.Context, _ *inputModel, apiClient *runcommand.APIC
113110

114111
func outputResult(p *print.Printer, outputFormat string, templates []runcommand.CommandTemplate) error {
115112
return p.OutputResult(outputFormat, templates, func() error {
113+
if len(templates) == 0 {
114+
p.Outputf("No commands templates found\n")
115+
return nil
116+
}
116117
table := tables.NewTable()
117118
table.SetHeader("NAME", "OS TYPE", "TITLE")
118119
for i := range templates {

0 commit comments

Comments
 (0)