Skip to content

Commit f4ad28e

Browse files
committed
adapted server machine-types list command to align to expectations
1 parent b8ffc82 commit f4ad28e

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

internal/cmd/server/machine-type/list/list.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,18 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8080
return fmt.Errorf("read machine-types: %w", err)
8181
}
8282

83-
if resp.Items == nil || len(*resp.Items) == 0 {
84-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
85-
if err != nil {
86-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
87-
projectLabel = model.ProjectId
88-
}
89-
params.Printer.Info("No machine-types found for project %q\n", projectLabel)
90-
return nil
83+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
84+
if err != nil {
85+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
86+
projectLabel = model.ProjectId
9187
}
9288

9389
// limit output
9490
if model.Limit != nil && len(*resp.Items) > int(*model.Limit) {
9591
*resp.Items = (*resp.Items)[:*model.Limit]
9692
}
9793

98-
return outputResult(params.Printer, model.OutputFormat, *resp)
94+
return outputResult(params.Printer, model.OutputFormat, projectLabel, *resp)
9995
},
10096
}
10197

@@ -140,8 +136,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
140136
return req
141137
}
142138

143-
func outputResult(p *print.Printer, outputFormat string, machineTypes iaas.MachineTypeListResponse) error {
139+
func outputResult(p *print.Printer, outputFormat, projectLabel string, machineTypes iaas.MachineTypeListResponse) error {
144140
return p.OutputResult(outputFormat, machineTypes, func() error {
141+
if machineTypes.Items == nil || len(*machineTypes.Items) == 0 {
142+
p.Outputf("No machine-types found for project %q\n", projectLabel)
143+
return nil
144+
}
145145
table := tables.NewTable()
146146
table.SetTitle("Machine-Types")
147147
table.SetHeader("NAME", "VCPUS", "RAM (GB)", "DESCRIPTION", "EXTRA SPECS")

internal/cmd/server/machine-type/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func TestBuildRequest(t *testing.T) {
181181
func TestOutputResult(t *testing.T) {
182182
type args struct {
183183
outputFormat string
184+
projectLabel string
184185
machineTypes iaas.MachineTypeListResponse
185186
}
186187
tests := []struct {
@@ -225,7 +226,7 @@ func TestOutputResult(t *testing.T) {
225226
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
226227
for _, tt := range tests {
227228
t.Run(tt.name, func(t *testing.T) {
228-
if err := outputResult(p, tt.args.outputFormat, tt.args.machineTypes); (err != nil) != tt.wantErr {
229+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.machineTypes); (err != nil) != tt.wantErr {
229230
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
230231
}
231232
})

0 commit comments

Comments
 (0)