Skip to content

Commit b301502

Browse files
committed
Corrected json and yaml on empty results
1 parent 7c4d440 commit b301502

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

internal/cmd/network-interface/list/list.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,18 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8787
return fmt.Errorf("list network interfaces: %w", err)
8888
}
8989

90-
if resp.Items == nil || len(*resp.Items) == 0 {
91-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
92-
if err != nil {
93-
projectLabel = model.ProjectId
94-
}
95-
params.Printer.Outputf("No network interfaces found for project %q\n", projectLabel)
96-
return nil
90+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
91+
if err != nil {
92+
projectLabel = model.ProjectId
9793
}
9894

9995
// Truncate output
100-
items := *resp.Items
96+
items := utils.GetSliceFromPointer(resp.Items)
10197
if model.Limit != nil && len(items) > int(*model.Limit) {
10298
items = items[:*model.Limit]
10399
}
104100

105-
return outputProjectResult(params.Printer, model.OutputFormat, items)
101+
return outputProjectResult(params.Printer, model.OutputFormat, items, projectLabel)
106102
}
107103

108104
// Call API to get NICs for one Network
@@ -113,25 +109,21 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
113109
return fmt.Errorf("list network interfaces: %w", err)
114110
}
115111

116-
if resp.Items == nil || len(*resp.Items) == 0 {
117-
networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, model.Region, *model.NetworkId)
118-
if err != nil {
119-
params.Printer.Debug(print.ErrorLevel, "get network name: %v", err)
120-
networkLabel = *model.NetworkId
121-
} else if networkLabel == "" {
122-
networkLabel = *model.NetworkId
123-
}
124-
params.Printer.Outputf("No network interfaces found for network %q\n", networkLabel)
125-
return nil
112+
networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, model.Region, *model.NetworkId)
113+
if err != nil {
114+
params.Printer.Debug(print.ErrorLevel, "get network name: %v", err)
115+
networkLabel = *model.NetworkId
116+
} else if networkLabel == "" {
117+
networkLabel = *model.NetworkId
126118
}
127119

128120
// Truncate output
129-
items := *resp.Items
121+
items := utils.GetSliceFromPointer(resp.Items)
130122
if model.Limit != nil && len(items) > int(*model.Limit) {
131123
items = items[:*model.Limit]
132124
}
133125

134-
return outputNetworkResult(params.Printer, model.OutputFormat, items)
126+
return outputNetworkResult(params.Printer, model.OutputFormat, items, networkLabel)
135127
},
136128
}
137129
configureFlags(cmd)
@@ -187,8 +179,13 @@ func buildNetworkRequest(ctx context.Context, model *inputModel, apiClient *iaas
187179
return req
188180
}
189181

190-
func outputProjectResult(p *print.Printer, outputFormat string, nics []iaas.NIC) error {
182+
func outputProjectResult(p *print.Printer, outputFormat string, nics []iaas.NIC, projectLabel string) error {
191183
return p.OutputResult(outputFormat, nics, func() error {
184+
if len(nics) == 0 {
185+
p.Outputf("No network interfaces found for project %q\n", projectLabel)
186+
return nil
187+
}
188+
192189
slices.SortFunc(nics, func(a, b iaas.NIC) int {
193190
return cmp.Compare(*a.NetworkId, *b.NetworkId)
194191
})
@@ -215,8 +212,13 @@ func outputProjectResult(p *print.Printer, outputFormat string, nics []iaas.NIC)
215212
})
216213
}
217214

218-
func outputNetworkResult(p *print.Printer, outputFormat string, nics []iaas.NIC) error {
215+
func outputNetworkResult(p *print.Printer, outputFormat string, nics []iaas.NIC, networkLabel string) error {
219216
return p.OutputResult(outputFormat, nics, func() error {
217+
if len(nics) == 0 {
218+
p.Outputf("No network interfaces found for network %q\n", networkLabel)
219+
return nil
220+
}
221+
220222
table := tables.NewTable()
221223
table.SetHeader("ID", "NAME", "NIC SECURITY", "DEVICE ID", "IPv4 ADDRESS", "STATUS", "TYPE")
222224

internal/cmd/network-interface/list/list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func TestOutputProjectResult(t *testing.T) {
241241
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
242242
for _, tt := range tests {
243243
t.Run(tt.name, func(t *testing.T) {
244-
if err := outputProjectResult(p, tt.args.outputFormat, tt.args.nics); (err != nil) != tt.wantErr {
244+
if err := outputProjectResult(p, tt.args.outputFormat, tt.args.nics, ""); (err != nil) != tt.wantErr {
245245
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
246246
}
247247
})
@@ -276,7 +276,7 @@ func TestOutputNetworkResult(t *testing.T) {
276276
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
277277
for _, tt := range tests {
278278
t.Run(tt.name, func(t *testing.T) {
279-
if err := outputNetworkResult(p, tt.args.outputFormat, tt.args.nics); (err != nil) != tt.wantErr {
279+
if err := outputNetworkResult(p, tt.args.outputFormat, tt.args.nics, ""); (err != nil) != tt.wantErr {
280280
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
281281
}
282282
})

0 commit comments

Comments
 (0)