Skip to content

Commit 0bc8501

Browse files
committed
adapted public ip list command to align to expectations
1 parent d19379d commit 0bc8501

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

internal/cmd/public-ip/list/list.go

Lines changed: 14 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 public IPs: %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 public IPs 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)
@@ -140,8 +137,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
140137
return req
141138
}
142139

143-
func outputResult(p *print.Printer, outputFormat string, publicIps []iaas.PublicIp) error {
140+
func outputResult(p *print.Printer, outputFormat, projectLabel string, publicIps []iaas.PublicIp) error {
144141
return p.OutputResult(outputFormat, publicIps, func() error {
142+
if len(publicIps) == 0 {
143+
p.Outputf("No public IPs found for project %q\n", projectLabel)
144+
return nil
145+
}
145146
table := tables.NewTable()
146147
table.SetHeader("ID", "IP ADDRESS", "USED BY")
147148

internal/cmd/public-ip/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
publicIps []iaas.PublicIp
180181
}
181182
tests := []struct {
@@ -193,7 +194,7 @@ func TestOutputResult(t *testing.T) {
193194
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
194195
for _, tt := range tests {
195196
t.Run(tt.name, func(t *testing.T) {
196-
if err := outputResult(p, tt.args.outputFormat, tt.args.publicIps); (err != nil) != tt.wantErr {
197+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.publicIps); (err != nil) != tt.wantErr {
197198
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
198199
}
199200
})

0 commit comments

Comments
 (0)