Skip to content

Commit 7ca2dce

Browse files
committed
adapted network list command to align to expectations
1 parent 4542beb commit 7ca2dce

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

internal/cmd/network/list/list.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,22 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7777
return fmt.Errorf("list networks: %w", err)
7878
}
7979

80-
if resp.Items == nil || len(*resp.Items) == 0 {
81-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
82-
if err != nil {
83-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
84-
projectLabel = model.ProjectId
85-
} else if projectLabel == "" {
86-
projectLabel = model.ProjectId
87-
}
88-
params.Printer.Info("No networks found for project %q\n", projectLabel)
89-
return nil
80+
items := resp.GetItems()
81+
82+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
83+
if err != nil {
84+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
85+
projectLabel = model.ProjectId
86+
} else if projectLabel == "" {
87+
projectLabel = model.ProjectId
9088
}
9189

9290
// Truncate output
93-
items := *resp.Items
9491
if model.Limit != nil && len(items) > int(*model.Limit) {
9592
items = items[:*model.Limit]
9693
}
9794

98-
return outputResult(params.Printer, model.OutputFormat, items)
95+
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
9996
},
10097
}
10198
configureFlags(cmd)
@@ -139,8 +136,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
139136
return req
140137
}
141138

142-
func outputResult(p *print.Printer, outputFormat string, networks []iaas.Network) error {
139+
func outputResult(p *print.Printer, outputFormat, projectLabel string, networks []iaas.Network) error {
143140
return p.OutputResult(outputFormat, networks, func() error {
141+
if len(networks) == 0 {
142+
p.Outputf("No networks found for project %q\n", projectLabel)
143+
return nil
144+
}
145+
144146
table := tables.NewTable()
145147
table.SetHeader("ID", "NAME", "STATUS", "PUBLIC IP", "PREFIXES", "ROUTED", "ROUTING TABLE ID")
146148

internal/cmd/network/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func TestBuildRequest(t *testing.T) {
176176
func TestOutputResult(t *testing.T) {
177177
type args struct {
178178
outputFormat string
179+
projectLabel string
179180
networks []iaas.Network
180181
}
181182
tests := []struct {
@@ -202,7 +203,7 @@ func TestOutputResult(t *testing.T) {
202203
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
203204
for _, tt := range tests {
204205
t.Run(tt.name, func(t *testing.T) {
205-
if err := outputResult(p, tt.args.outputFormat, tt.args.networks); (err != nil) != tt.wantErr {
206+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.networks); (err != nil) != tt.wantErr {
206207
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
207208
}
208209
})

0 commit comments

Comments
 (0)