Skip to content

Commit 8b6bb19

Browse files
authored
fix(observability): print valid JSON/YAML output for list cmds (#1393)
relates to STACKITCLI-268 and #893
1 parent a557c95 commit 8b6bb19

8 files changed

Lines changed: 64 additions & 52 deletions

File tree

internal/cmd/observability/credentials/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,20 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6868
if err != nil {
6969
return fmt.Errorf("list Observability credentials: %w", err)
7070
}
71-
credentials := *resp.Credentials
72-
if len(credentials) == 0 {
73-
instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
74-
if err != nil {
75-
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
76-
instanceLabel = model.InstanceId
77-
}
78-
params.Printer.Info("No credentials found for instance %q\n", instanceLabel)
79-
return nil
71+
72+
credentials := resp.GetCredentials()
73+
74+
instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
75+
if err != nil {
76+
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
77+
instanceLabel = model.InstanceId
8078
}
8179

8280
// Truncate output
8381
if model.Limit != nil && len(credentials) > int(*model.Limit) {
8482
credentials = credentials[:*model.Limit]
8583
}
86-
return outputResult(params.Printer, model.OutputFormat, credentials)
84+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, credentials)
8785
},
8886
}
8987
configureFlags(cmd)
@@ -124,8 +122,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *observabili
124122
return req
125123
}
126124

127-
func outputResult(p *print.Printer, outputFormat string, credentials []observability.ServiceKeysList) error {
125+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, credentials []observability.ServiceKeysList) error {
128126
return p.OutputResult(outputFormat, credentials, func() error {
127+
if len(credentials) == 0 {
128+
p.Outputf("No credentials found for instance %q\n", instanceLabel)
129+
return nil
130+
}
129131
table := tables.NewTable()
130132
table.SetHeader("USERNAME")
131133
for i := range credentials {

internal/cmd/observability/credentials/list/list_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ func TestBuildRequest(t *testing.T) {
173173

174174
func TestOutputResult(t *testing.T) {
175175
type args struct {
176-
outputFormat string
177-
credentials []observability.ServiceKeysList
176+
outputFormat string
177+
instanceLabel string
178+
credentials []observability.ServiceKeysList
178179
}
179180
tests := []struct {
180181
name string
@@ -204,7 +205,7 @@ func TestOutputResult(t *testing.T) {
204205
params := testparams.NewTestParams()
205206
for _, tt := range tests {
206207
t.Run(tt.name, func(t *testing.T) {
207-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
208+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.instanceLabel, tt.args.credentials); (err != nil) != tt.wantErr {
208209
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
209210
}
210211
})

internal/cmd/observability/instance/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,21 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6666
if err != nil {
6767
return fmt.Errorf("get Observability instances: %w", err)
6868
}
69-
instances := *resp.Instances
70-
if len(instances) == 0 {
71-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
72-
if err != nil {
73-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
74-
projectLabel = model.ProjectId
75-
}
76-
params.Printer.Info("No instances found for project %q\n", projectLabel)
77-
return nil
69+
70+
instances := resp.GetInstances()
71+
72+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
73+
if err != nil {
74+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
75+
projectLabel = model.ProjectId
7876
}
7977

8078
// Truncate output
8179
if model.Limit != nil && len(instances) > int(*model.Limit) {
8280
instances = instances[:*model.Limit]
8381
}
8482

85-
return outputResult(params.Printer, model.OutputFormat, instances)
83+
return outputResult(params.Printer, model.OutputFormat, projectLabel, instances)
8684
},
8785
}
8886

@@ -122,8 +120,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *observabili
122120
return req
123121
}
124122

125-
func outputResult(p *print.Printer, outputFormat string, instances []observability.ProjectInstanceFull) error {
123+
func outputResult(p *print.Printer, outputFormat, projectLabel string, instances []observability.ProjectInstanceFull) error {
126124
return p.OutputResult(outputFormat, instances, func() error {
125+
if len(instances) == 0 {
126+
p.Outputf("No instances found for project %q\n", projectLabel)
127+
return nil
128+
}
127129
table := tables.NewTable()
128130
table.SetHeader("ID", "NAME", "PLAN", "STATUS")
129131
for i := range instances {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func TestBuildRequest(t *testing.T) {
150150
func TestOutputResult(t *testing.T) {
151151
type args struct {
152152
outputFormat string
153+
projectLabel string
153154
instances []observability.ProjectInstanceFull
154155
}
155156
tests := []struct {
@@ -180,7 +181,7 @@ func TestOutputResult(t *testing.T) {
180181
params := testparams.NewTestParams()
181182
for _, tt := range tests {
182183
t.Run(tt.name, func(t *testing.T) {
183-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr {
184+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.instances); (err != nil) != tt.wantErr {
184185
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
185186
}
186187
})

internal/cmd/observability/plans/plans.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,21 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6666
if err != nil {
6767
return fmt.Errorf("get Observability service plans: %w", err)
6868
}
69-
plans := *resp.Plans
70-
if len(plans) == 0 {
71-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
72-
if err != nil {
73-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
74-
projectLabel = model.ProjectId
75-
}
76-
params.Printer.Info("No plans found for project %q\n", projectLabel)
77-
return nil
69+
70+
plans := resp.GetPlans()
71+
72+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
73+
if err != nil {
74+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
75+
projectLabel = model.ProjectId
7876
}
7977

8078
// Truncate output
8179
if model.Limit != nil && len(plans) > int(*model.Limit) {
8280
plans = plans[:*model.Limit]
8381
}
8482

85-
return outputResult(params.Printer, model.OutputFormat, plans)
83+
return outputResult(params.Printer, model.OutputFormat, projectLabel, plans)
8684
},
8785
}
8886

@@ -122,8 +120,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *observabili
122120
return req
123121
}
124122

125-
func outputResult(p *print.Printer, outputFormat string, plans []observability.Plan) error {
123+
func outputResult(p *print.Printer, outputFormat, projectLabel string, plans []observability.Plan) error {
126124
return p.OutputResult(outputFormat, plans, func() error {
125+
if len(plans) == 0 {
126+
p.Outputf("No plans found for project %q\n", projectLabel)
127+
return nil
128+
}
127129
table := tables.NewTable()
128130
table.SetHeader("ID", "PLAN NAME", "DESCRIPTION")
129131
for i := range plans {

internal/cmd/observability/plans/plans_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func TestBuildRequest(t *testing.T) {
150150
func TestOutputResult(t *testing.T) {
151151
type args struct {
152152
outputFormat string
153+
projectLabel string
153154
plans []observability.Plan
154155
}
155156
tests := []struct {
@@ -180,7 +181,7 @@ func TestOutputResult(t *testing.T) {
180181
params := testparams.NewTestParams()
181182
for _, tt := range tests {
182183
t.Run(tt.name, func(t *testing.T) {
183-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr {
184+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.plans); (err != nil) != tt.wantErr {
184185
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
185186
}
186187
})

internal/cmd/observability/scrape-config/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,21 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6969
if err != nil {
7070
return fmt.Errorf("get scrape configurations: %w", err)
7171
}
72-
configs := *resp.Data
73-
if len(configs) == 0 {
74-
instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
75-
if err != nil {
76-
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
77-
instanceLabel = model.InstanceId
78-
}
79-
params.Printer.Info("No scrape configurations found for instance %q\n", instanceLabel)
80-
return nil
72+
73+
configs := resp.GetData()
74+
75+
instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
76+
if err != nil {
77+
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
78+
instanceLabel = model.InstanceId
8179
}
8280

8381
// Truncate output
8482
if model.Limit != nil && len(configs) > int(*model.Limit) {
8583
configs = configs[:*model.Limit]
8684
}
8785

88-
return outputResult(params.Printer, model.OutputFormat, configs)
86+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, configs)
8987
},
9088
}
9189

@@ -127,8 +125,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *observabili
127125
return req
128126
}
129127

130-
func outputResult(p *print.Printer, outputFormat string, configs []observability.Job) error {
128+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, configs []observability.Job) error {
131129
return p.OutputResult(outputFormat, configs, func() error {
130+
if len(configs) == 0 {
131+
p.Outputf("No scrape configurations found for instance %q\n", instanceLabel)
132+
return nil
133+
}
132134
table := tables.NewTable()
133135
table.SetHeader("NAME", "TARGETS", "SCRAPE INTERVAL")
134136
for i := range configs {

internal/cmd/observability/scrape-config/list/list_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ func TestBuildRequest(t *testing.T) {
174174

175175
func TestOutputResult(t *testing.T) {
176176
type args struct {
177-
outputFormat string
178-
configs []observability.Job
177+
outputFormat string
178+
instanceLabel string
179+
configs []observability.Job
179180
}
180181
tests := []struct {
181182
name string
@@ -205,7 +206,7 @@ func TestOutputResult(t *testing.T) {
205206
params := testparams.NewTestParams()
206207
for _, tt := range tests {
207208
t.Run(tt.name, func(t *testing.T) {
208-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.configs); (err != nil) != tt.wantErr {
209+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.instanceLabel, tt.args.configs); (err != nil) != tt.wantErr {
209210
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
210211
}
211212
})

0 commit comments

Comments
 (0)