Skip to content

Commit 07bb839

Browse files
committed
adapted network list command to align to expectations
1 parent 936bb00 commit 07bb839

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

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
@@ -174,6 +174,7 @@ func TestBuildRequest(t *testing.T) {
174174
func TestOutputResult(t *testing.T) {
175175
type args struct {
176176
outputFormat string
177+
projectLabel string
177178
networks []iaas.Network
178179
}
179180
tests := []struct {
@@ -199,7 +200,7 @@ func TestOutputResult(t *testing.T) {
199200
params := testparams.NewTestParams()
200201
for _, tt := range tests {
201202
t.Run(tt.name, func(t *testing.T) {
202-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.networks); (err != nil) != tt.wantErr {
203+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.networks); (err != nil) != tt.wantErr {
203204
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
204205
}
205206
})

0 commit comments

Comments
 (0)