Skip to content

Commit d6772b0

Browse files
authored
fix(logme): print valid JSON/YAML output for list cmds (#1038)
relates to STACKITCLI-264 / #893
1 parent af803a3 commit d6772b0

File tree

6 files changed

+63
-60
lines changed

6 files changed

+63
-60
lines changed

internal/cmd/logme/credentials/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,19 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6767
if err != nil {
6868
return fmt.Errorf("list LogMe credentials: %w", err)
6969
}
70-
credentials := *resp.CredentialsList
71-
if len(credentials) == 0 {
72-
instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
73-
if err != nil {
74-
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
75-
instanceLabel = model.InstanceId
76-
}
77-
params.Printer.Info("No credentials found for instance %q\n", instanceLabel)
78-
return nil
70+
credentials := resp.GetCredentialsList()
71+
72+
instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
73+
if err != nil {
74+
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
75+
instanceLabel = model.InstanceId
7976
}
8077

8178
// Truncate output
8279
if model.Limit != nil && len(credentials) > int(*model.Limit) {
8380
credentials = credentials[:*model.Limit]
8481
}
85-
return outputResult(params.Printer, model.OutputFormat, credentials)
82+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, credentials)
8683
},
8784
}
8885
configureFlags(cmd)
@@ -126,8 +123,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl
126123
return req
127124
}
128125

129-
func outputResult(p *print.Printer, outputFormat string, credentials []logme.CredentialsListItem) error {
126+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, credentials []logme.CredentialsListItem) error {
130127
return p.OutputResult(outputFormat, credentials, func() error {
128+
if len(credentials) == 0 {
129+
p.Outputf("No credentials found for instance %q\n", instanceLabel)
130+
return nil
131+
}
132+
131133
table := tables.NewTable()
132134
table.SetHeader("ID")
133135
for i := range credentials {

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/stackitcloud/stackit-sdk-go/services/logme"
1717
)
1818

19-
var projectIdFlag = globalflags.ProjectIdFlag
20-
2119
type testCtxKey struct{}
2220

2321
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
@@ -27,9 +25,9 @@ var testInstanceId = uuid.NewString()
2725

2826
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2927
flagValues := map[string]string{
30-
projectIdFlag: testProjectId,
31-
instanceIdFlag: testInstanceId,
32-
limitFlag: "10",
28+
globalflags.ProjectIdFlag: testProjectId,
29+
instanceIdFlag: testInstanceId,
30+
limitFlag: "10",
3331
}
3432
for _, mod := range mods {
3533
mod(flagValues)
@@ -82,21 +80,21 @@ func TestParseInput(t *testing.T) {
8280
{
8381
description: "project id missing",
8482
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
85-
delete(flagValues, projectIdFlag)
83+
delete(flagValues, globalflags.ProjectIdFlag)
8684
}),
8785
isValid: false,
8886
},
8987
{
9088
description: "project id invalid 1",
9189
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
92-
flagValues[projectIdFlag] = ""
90+
flagValues[globalflags.ProjectIdFlag] = ""
9391
}),
9492
isValid: false,
9593
},
9694
{
9795
description: "project id invalid 2",
9896
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
99-
flagValues[projectIdFlag] = "invalid-uuid"
97+
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
10098
}),
10199
isValid: false,
102100
},
@@ -174,8 +172,9 @@ func TestBuildRequest(t *testing.T) {
174172

175173
func TestOutputResult(t *testing.T) {
176174
type args struct {
177-
outputFormat string
178-
credentials []logme.CredentialsListItem
175+
outputFormat string
176+
instanceLabel string
177+
credentials []logme.CredentialsListItem
179178
}
180179
tests := []struct {
181180
name string
@@ -206,7 +205,7 @@ func TestOutputResult(t *testing.T) {
206205
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
207206
for _, tt := range tests {
208207
t.Run(tt.name, func(t *testing.T) {
209-
if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
208+
if err := outputResult(p, tt.args.outputFormat, tt.args.instanceLabel, tt.args.credentials); (err != nil) != tt.wantErr {
210209
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
211210
}
212211
})

internal/cmd/logme/instance/list/list.go

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

7875
// Truncate output
7976
if model.Limit != nil && len(instances) > int(*model.Limit) {
8077
instances = instances[:*model.Limit]
8178
}
8279

83-
return outputResult(params.Printer, model.OutputFormat, instances)
80+
return outputResult(params.Printer, model.OutputFormat, projectLabel, instances)
8481
},
8582
}
8683

@@ -120,8 +117,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl
120117
return req
121118
}
122119

123-
func outputResult(p *print.Printer, outputFormat string, instances []logme.Instance) error {
120+
func outputResult(p *print.Printer, outputFormat, projectLabel string, instances []logme.Instance) error {
124121
return p.OutputResult(outputFormat, instances, func() error {
122+
if len(instances) == 0 {
123+
p.Outputf("No instances found for project %q\n", projectLabel)
124+
return nil
125+
}
126+
125127
table := tables.NewTable()
126128
table.SetHeader("ID", "NAME", "LAST OPERATION TYPE", "LAST OPERATION STATE")
127129
for i := range instances {

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/stackitcloud/stackit-sdk-go/services/logme"
1717
)
1818

19-
var projectIdFlag = globalflags.ProjectIdFlag
20-
2119
type testCtxKey struct{}
2220

2321
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
@@ -26,8 +24,8 @@ var testProjectId = uuid.NewString()
2624

2725
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2826
flagValues := map[string]string{
29-
projectIdFlag: testProjectId,
30-
limitFlag: "10",
27+
globalflags.ProjectIdFlag: testProjectId,
28+
limitFlag: "10",
3129
}
3230
for _, mod := range mods {
3331
mod(flagValues)
@@ -79,21 +77,21 @@ func TestParseInput(t *testing.T) {
7977
{
8078
description: "project id missing",
8179
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
82-
delete(flagValues, projectIdFlag)
80+
delete(flagValues, globalflags.ProjectIdFlag)
8381
}),
8482
isValid: false,
8583
},
8684
{
8785
description: "project id invalid 1",
8886
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
89-
flagValues[projectIdFlag] = ""
87+
flagValues[globalflags.ProjectIdFlag] = ""
9088
}),
9189
isValid: false,
9290
},
9391
{
9492
description: "project id invalid 2",
9593
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
96-
flagValues[projectIdFlag] = "invalid-uuid"
94+
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
9795
}),
9896
isValid: false,
9997
},
@@ -151,6 +149,7 @@ func TestBuildRequest(t *testing.T) {
151149
func TestOutputResult(t *testing.T) {
152150
type args struct {
153151
outputFormat string
152+
projectLabel string
154153
instances []logme.Instance
155154
}
156155
tests := []struct {
@@ -182,7 +181,7 @@ func TestOutputResult(t *testing.T) {
182181
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
183182
for _, tt := range tests {
184183
t.Run(tt.name, func(t *testing.T) {
185-
if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr {
184+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.instances); (err != nil) != tt.wantErr {
186185
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
187186
}
188187
})

internal/cmd/logme/plans/plans.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6464
if err != nil {
6565
return fmt.Errorf("get LogMe service plans: %w", err)
6666
}
67-
plans := *resp.Offerings
68-
if len(plans) == 0 {
69-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
70-
if err != nil {
71-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
72-
projectLabel = model.ProjectId
73-
}
74-
params.Printer.Info("No plans found for project %q\n", projectLabel)
75-
return nil
67+
plans := resp.GetOfferings()
68+
69+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
70+
if err != nil {
71+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
72+
projectLabel = model.ProjectId
7673
}
7774

7875
// Truncate output
7976
if model.Limit != nil && len(plans) > int(*model.Limit) {
8077
plans = plans[:*model.Limit]
8178
}
8279

83-
return outputResult(params.Printer, model.OutputFormat, plans)
80+
return outputResult(params.Printer, model.OutputFormat, projectLabel, plans)
8481
},
8582
}
8683

@@ -120,8 +117,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl
120117
return req
121118
}
122119

123-
func outputResult(p *print.Printer, outputFormat string, plans []logme.Offering) error {
120+
func outputResult(p *print.Printer, outputFormat, projectLabel string, plans []logme.Offering) error {
124121
return p.OutputResult(outputFormat, plans, func() error {
122+
if len(plans) == 0 {
123+
p.Outputf("No plans found for project %q\n", projectLabel)
124+
return nil
125+
}
126+
125127
table := tables.NewTable()
126128
table.SetHeader("OFFERING NAME", "VERSION", "ID", "NAME", "DESCRIPTION")
127129
for i := range plans {

internal/cmd/logme/plans/plans_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/stackitcloud/stackit-sdk-go/services/logme"
1717
)
1818

19-
var projectIdFlag = globalflags.ProjectIdFlag
20-
2119
type testCtxKey struct{}
2220

2321
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
@@ -26,8 +24,8 @@ var testProjectId = uuid.NewString()
2624

2725
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2826
flagValues := map[string]string{
29-
projectIdFlag: testProjectId,
30-
limitFlag: "10",
27+
globalflags.ProjectIdFlag: testProjectId,
28+
limitFlag: "10",
3129
}
3230
for _, mod := range mods {
3331
mod(flagValues)
@@ -79,21 +77,21 @@ func TestParseInput(t *testing.T) {
7977
{
8078
description: "project id missing",
8179
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
82-
delete(flagValues, projectIdFlag)
80+
delete(flagValues, globalflags.ProjectIdFlag)
8381
}),
8482
isValid: false,
8583
},
8684
{
8785
description: "project id invalid 1",
8886
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
89-
flagValues[projectIdFlag] = ""
87+
flagValues[globalflags.ProjectIdFlag] = ""
9088
}),
9189
isValid: false,
9290
},
9391
{
9492
description: "project id invalid 2",
9593
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
96-
flagValues[projectIdFlag] = "invalid-uuid"
94+
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
9795
}),
9896
isValid: false,
9997
},
@@ -151,6 +149,7 @@ func TestBuildRequest(t *testing.T) {
151149
func TestOutputResult(t *testing.T) {
152150
type args struct {
153151
outputFormat string
152+
projectLabel string
154153
plans []logme.Offering
155154
}
156155
tests := []struct {
@@ -182,7 +181,7 @@ func TestOutputResult(t *testing.T) {
182181
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
183182
for _, tt := range tests {
184183
t.Run(tt.name, func(t *testing.T) {
185-
if err := outputResult(p, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr {
184+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.plans); (err != nil) != tt.wantErr {
186185
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
187186
}
188187
})

0 commit comments

Comments
 (0)