Skip to content

Commit 566ed85

Browse files
authored
fix: stackit beta public-ip commands - add nil pointer checks and tests for the outputResult functions (#609)
* add nil pointer checks * add tests for the outputResult functions within the public-ip commands
1 parent a90d86e commit 566ed85

File tree

9 files changed

+98
-6
lines changed

9 files changed

+98
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
5959
if err != nil {
6060
p.Debug(print.ErrorLevel, "get public IP: %v", err)
6161
publicIpLabel = model.PublicIpId
62+
} else if publicIpLabel == "" {
63+
publicIpLabel = model.PublicIpId
6264
}
6365

6466
if !model.AssumeYes {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
6767
if err != nil {
6868
p.Debug(print.ErrorLevel, "get project name: %v", err)
6969
projectLabel = model.ProjectId
70+
} else if projectLabel == "" {
71+
projectLabel = model.ProjectId
7072
}
7173

7274
if !model.AssumeYes {
@@ -84,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8486
return fmt.Errorf("create public IP: %w", err)
8587
}
8688

87-
return outputResult(p, model, projectLabel, resp)
89+
return outputResult(p, model.OutputFormat, projectLabel, *resp)
8890
},
8991
}
9092
configureFlags(cmd)
@@ -140,8 +142,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
140142
return req.CreatePublicIPPayload(payload)
141143
}
142144

143-
func outputResult(p *print.Printer, model *inputModel, projectLabel string, publicIp *iaas.PublicIp) error {
144-
switch model.OutputFormat {
145+
func outputResult(p *print.Printer, outputFormat, projectLabel string, publicIp iaas.PublicIp) error {
146+
switch outputFormat {
145147
case print.JSONOutputFormat:
146148
details, err := json.MarshalIndent(publicIp, "", " ")
147149
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
5757
if err != nil {
5858
p.Debug(print.ErrorLevel, "get public IP: %v", err)
5959
publicIpLabel = model.PublicIpId
60+
} else if publicIpLabel == "" {
61+
publicIpLabel = model.PublicIpId
6062
}
6163

6264
if !model.AssumeYes {
@@ -74,7 +76,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7476
return fmt.Errorf("delete public IP: %w", err)
7577
}
7678

77-
p.Info("Deleted public IP %q\n", model.PublicIpId)
79+
p.Info("Deleted public IP %q\n", publicIpLabel)
7880
return nil
7981
},
8082
}

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
5555
if err != nil {
5656
p.Debug(print.ErrorLevel, "get public IP: %v", err)
5757
publicIpLabel = model.PublicIpId
58+
} else if publicIpLabel == "" {
59+
publicIpLabel = model.PublicIpId
5860
}
5961

6062
if !model.AssumeYes {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
8181
if err != nil {
8282
p.Debug(print.ErrorLevel, "get project name: %v", err)
8383
projectLabel = model.ProjectId
84+
} else if projectLabel == "" {
85+
projectLabel = model.ProjectId
8486
}
8587
p.Info("No public IPs found for project %q\n", projectLabel)
8688
return nil

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)