Skip to content

Commit f8ef44e

Browse files
committed
fix(mongodbflex): print valid JSON for list cmds
relates to STACKITCLI-244
1 parent 7b0c48d commit f8ef44e

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

internal/cmd/mongodbflex/backup/list/list.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7474
if err != nil {
7575
return fmt.Errorf("get backups for MongoDB Flex instance %q: %w", instanceLabel, err)
7676
}
77-
if resp.Items == nil || len(*resp.Items) == 0 {
78-
cmd.Printf("No backups found for instance %q\n", instanceLabel)
79-
return nil
80-
}
81-
backups := *resp.Items
77+
backups := utils.GetSliceFromPointer(resp.Items)
8278

8379
restoreJobs, err := apiClient.ListRestoreJobs(ctx, model.ProjectId, *model.InstanceId, model.Region).Execute()
8480
if err != nil {
@@ -90,7 +86,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
9086
backups = backups[:*model.Limit]
9187
}
9288

93-
return outputResult(params.Printer, model.OutputFormat, backups, restoreJobs)
89+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, backups, restoreJobs)
9490
},
9591
}
9692

@@ -135,12 +131,17 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex
135131
return req
136132
}
137133

138-
func outputResult(p *print.Printer, outputFormat string, backups []mongodbflex.Backup, restoreJobs *mongodbflex.ListRestoreJobsResponse) error {
134+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, backups []mongodbflex.Backup, restoreJobs *mongodbflex.ListRestoreJobsResponse) error {
139135
if restoreJobs == nil {
140136
return fmt.Errorf("restore jobs is empty")
141137
}
142138

143139
return p.OutputResult(outputFormat, backups, func() error {
140+
if len(backups) == 0 {
141+
p.Outputf("No backups found for instance %q\n", instanceLabel)
142+
return nil
143+
}
144+
144145
table := tables.NewTable()
145146
table.SetHeader("ID", "CREATED AT", "EXPIRES AT", "BACKUP SIZE", "RESTORE STATUS")
146147
for i := range backups {

internal/cmd/mongodbflex/instance/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6565
if err != nil {
6666
return fmt.Errorf("get MongoDB Flex instances: %w", err)
6767
}
68-
if resp.Items == nil || len(*resp.Items) == 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
68+
instances := utils.GetSliceFromPointer(resp.Items)
69+
70+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
71+
if err != nil {
72+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
73+
projectLabel = model.ProjectId
7674
}
77-
instances := *resp.Items
7875

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

84-
return outputResult(params.Printer, model.OutputFormat, instances)
81+
return outputResult(params.Printer, model.OutputFormat, projectLabel, instances)
8582
},
8683
}
8784

@@ -121,8 +118,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex
121118
return req
122119
}
123120

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

internal/cmd/mongodbflex/user/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6868
if err != nil {
6969
return fmt.Errorf("get MongoDB Flex users: %w", err)
7070
}
71-
if resp.Items == nil || len(*resp.Items) == 0 {
72-
instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, *model.InstanceId, model.Region)
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 users found for instance %q\n", instanceLabel)
78-
return nil
71+
users := utils.GetSliceFromPointer(resp.Items)
72+
73+
instanceLabel, err := mongodbflexUtils.GetInstanceName(ctx, apiClient, model.ProjectId, *model.InstanceId, model.Region)
74+
if err != nil {
75+
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
76+
instanceLabel = *model.InstanceId
7977
}
80-
users := *resp.Items
8178

8279
// Truncate output
8380
if model.Limit != nil && len(users) > int(*model.Limit) {
8481
users = users[:*model.Limit]
8582
}
8683

87-
return outputResult(params.Printer, model.OutputFormat, users)
84+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, users)
8885
},
8986
}
9087

@@ -129,8 +126,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex
129126
return req
130127
}
131128

132-
func outputResult(p *print.Printer, outputFormat string, users []mongodbflex.ListUser) error {
129+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, users []mongodbflex.ListUser) error {
133130
return p.OutputResult(outputFormat, users, func() error {
131+
if len(users) == 0 {
132+
p.Outputf("No users found for instance %q\n", instanceLabel)
133+
return nil
134+
}
135+
134136
table := tables.NewTable()
135137
table.SetHeader("ID", "USERNAME")
136138
for i := range users {

0 commit comments

Comments
 (0)