Skip to content

Commit f4c54a9

Browse files
committed
- add nil pointer checks
- add tests for the outputResult functions within the public-ip commands
1 parent 6213bb5 commit f4c54a9

File tree

9 files changed

+103
-6
lines changed

9 files changed

+103
-6
lines changed

internal/cmd/beta/public-ip/associate/associate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
6060
p.Debug(print.ErrorLevel, "get public IP: %v", err)
6161
publicIpLabel = model.PublicIpId
6262
}
63+
if publicIpLabel == "" {
64+
publicIpLabel = model.PublicIpId
65+
}
6366

6467
if !model.AssumeYes {
6568
prompt := fmt.Sprintf("Are you sure you want to associate public IP %q with resource %v?", publicIpLabel, *model.AssociatedResourceId)

internal/cmd/beta/public-ip/create/create.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
6868
p.Debug(print.ErrorLevel, "get project name: %v", err)
6969
projectLabel = model.ProjectId
7070
}
71+
if projectLabel == "" {
72+
projectLabel = model.ProjectId
73+
}
7174

7275
if !model.AssumeYes {
7376
prompt := fmt.Sprintf("Are you sure you want to create a public IP for project %q?", projectLabel)
@@ -84,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8487
return fmt.Errorf("create public IP: %w", err)
8588
}
8689

87-
return outputResult(p, model, projectLabel, resp)
90+
return outputResult(p, model.OutputFormat, projectLabel, *resp)
8891
},
8992
}
9093
configureFlags(cmd)
@@ -140,8 +143,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
140143
return req.CreatePublicIPPayload(payload)
141144
}
142145

143-
func outputResult(p *print.Printer, model *inputModel, projectLabel string, publicIp *iaas.PublicIp) error {
144-
switch model.OutputFormat {
146+
func outputResult(p *print.Printer, outputFormat, projectLabel string, publicIp iaas.PublicIp) error {
147+
switch outputFormat {
145148
case print.JSONOutputFormat:
146149
details, err := json.MarshalIndent(publicIp, "", " ")
147150
if err != nil {

internal/cmd/beta/public-ip/create/create_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,31 @@ func TestBuildRequest(t *testing.T) {
213213
})
214214
}
215215
}
216+
217+
func Test_outputResult(t *testing.T) {
218+
type args struct {
219+
outputFormat string
220+
projectLabel string
221+
publicIp iaas.PublicIp
222+
}
223+
tests := []struct {
224+
name string
225+
args args
226+
wantErr bool
227+
}{
228+
{
229+
name: "empty",
230+
args: args{},
231+
wantErr: false,
232+
},
233+
}
234+
p := print.NewPrinter()
235+
p.Cmd = NewCmd(p)
236+
for _, tt := range tests {
237+
t.Run(tt.name, func(t *testing.T) {
238+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.publicIp); (err != nil) != tt.wantErr {
239+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
240+
}
241+
})
242+
}
243+
}

internal/cmd/beta/public-ip/delete/delete.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
5858
p.Debug(print.ErrorLevel, "get public IP: %v", err)
5959
publicIpLabel = model.PublicIpId
6060
}
61+
if publicIpLabel == "" {
62+
publicIpLabel = model.PublicIpId
63+
}
6164

6265
if !model.AssumeYes {
6366
prompt := fmt.Sprintf("Are you sure you want to delete public IP %q? (This cannot be undone)", publicIpLabel)
@@ -74,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7477
return fmt.Errorf("delete public IP: %w", err)
7578
}
7679

77-
p.Info("Deleted public IP %q\n", model.PublicIpId)
80+
p.Info("Deleted public IP %q\n", publicIpLabel)
7881
return nil
7982
},
8083
}

internal/cmd/beta/public-ip/describe/describe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6666
return fmt.Errorf("read public IP: %w", err)
6767
}
6868

69-
return outputResult(p, model.OutputFormat, resp)
69+
return outputResult(p, model.OutputFormat, *resp)
7070
},
7171
}
7272
return cmd
@@ -101,7 +101,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
101101
return apiClient.GetPublicIP(ctx, model.ProjectId, model.PublicIpId)
102102
}
103103

104-
func outputResult(p *print.Printer, outputFormat string, publicIp *iaas.PublicIp) error {
104+
func outputResult(p *print.Printer, outputFormat string, publicIp iaas.PublicIp) error {
105105
switch outputFormat {
106106
case print.JSONOutputFormat:
107107
details, err := json.MarshalIndent(publicIp, "", " ")

internal/cmd/beta/public-ip/describe/describe_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,30 @@ func TestBuildRequest(t *testing.T) {
216216
})
217217
}
218218
}
219+
220+
func TestOutputResult(t *testing.T) {
221+
type args struct {
222+
outputFormat string
223+
publicIp iaas.PublicIp
224+
}
225+
tests := []struct {
226+
name string
227+
args args
228+
wantErr bool
229+
}{
230+
{
231+
name: "empty",
232+
args: args{},
233+
wantErr: false,
234+
},
235+
}
236+
p := print.NewPrinter()
237+
p.Cmd = NewCmd(p)
238+
for _, tt := range tests {
239+
t.Run(tt.name, func(t *testing.T) {
240+
if err := outputResult(p, tt.args.outputFormat, tt.args.publicIp); (err != nil) != tt.wantErr {
241+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
242+
}
243+
})
244+
}
245+
}

internal/cmd/beta/public-ip/disassociate/disassociate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
5656
p.Debug(print.ErrorLevel, "get public IP: %v", err)
5757
publicIpLabel = model.PublicIpId
5858
}
59+
if publicIpLabel == "" {
60+
publicIpLabel = model.PublicIpId
61+
}
5962

6063
if !model.AssumeYes {
6164
prompt := fmt.Sprintf("Are you sure you want to disassociate public IP %q from the associated resource %q?", publicIpLabel, associatedResourceId)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
8282
p.Debug(print.ErrorLevel, "get project name: %v", err)
8383
projectLabel = model.ProjectId
8484
}
85+
if projectLabel == "" {
86+
projectLabel = model.ProjectId
87+
}
8588
p.Info("No public IPs found for project %q\n", projectLabel)
8689
return nil
8790
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,30 @@ func TestBuildRequest(t *testing.T) {
202202
})
203203
}
204204
}
205+
206+
func TestOutputResult(t *testing.T) {
207+
type args struct {
208+
outputFormat string
209+
publicIps []iaas.PublicIp
210+
}
211+
tests := []struct {
212+
name string
213+
args args
214+
wantErr bool
215+
}{
216+
{
217+
name: "empty",
218+
args: args{},
219+
wantErr: false,
220+
},
221+
}
222+
p := print.NewPrinter()
223+
p.Cmd = NewCmd(p)
224+
for _, tt := range tests {
225+
t.Run(tt.name, func(t *testing.T) {
226+
if err := outputResult(p, tt.args.outputFormat, tt.args.publicIps); (err != nil) != tt.wantErr {
227+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
228+
}
229+
})
230+
}
231+
}

0 commit comments

Comments
 (0)