diff --git a/internal/cmd/beta/network-interface/create/create.go b/internal/cmd/beta/network-interface/create/create.go index 99fa2dc7f..eb6bb301b 100644 --- a/internal/cmd/beta/network-interface/create/create.go +++ b/internal/cmd/beta/network-interface/create/create.go @@ -82,6 +82,8 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { p.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId + } else if projectLabel == "" { + projectLabel = model.ProjectId } if !model.AssumeYes { @@ -99,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create network interface: %w", err) } - return outputResult(p, model, resp) + return outputResult(p, model.OutputFormat, model.ProjectId, resp) }, } configureFlags(cmd) @@ -226,8 +228,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli return req.CreateNicPayload(payload) } -func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error { - switch model.OutputFormat { +func outputResult(p *print.Printer, outputFormat, projectId string, nic *iaas.NIC) error { + if nic == nil { + return fmt.Errorf("nic is empty") + } + switch outputFormat { case print.JSONOutputFormat: details, err := json.MarshalIndent(nic, "", " ") if err != nil { @@ -245,7 +250,7 @@ func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error { return nil default: - p.Outputf("Created network interface for project %q.\nNIC ID: %s\n", model.ProjectId, utils.PtrString(nic.Id)) + p.Outputf("Created network interface for project %q.\nNIC ID: %s\n", projectId, utils.PtrString(nic.Id)) return nil } } diff --git a/internal/cmd/beta/network-interface/create/create_test.go b/internal/cmd/beta/network-interface/create/create_test.go index 2173cf1ea..843b15f69 100644 --- a/internal/cmd/beta/network-interface/create/create_test.go +++ b/internal/cmd/beta/network-interface/create/create_test.go @@ -260,3 +260,38 @@ func TestBuildRequest(t *testing.T) { }) } } + +func Test_outputResult(t *testing.T) { + type args struct { + outputFormat string + projectId string + nic *iaas.NIC + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: true, + }, + { + name: "set empty nic", + args: args{ + nic: &iaas.NIC{}, + }, + wantErr: false, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(p) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.projectId, tt.args.nic); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/beta/network-interface/describe/describe.go b/internal/cmd/beta/network-interface/describe/describe.go index c83e79897..a1d741bdf 100644 --- a/internal/cmd/beta/network-interface/describe/describe.go +++ b/internal/cmd/beta/network-interface/describe/describe.go @@ -117,6 +117,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli } func outputResult(p *print.Printer, outputFormat string, nic *iaas.NIC) error { + if nic == nil { + return fmt.Errorf("nic is empty") + } switch outputFormat { case print.JSONOutputFormat: details, err := json.MarshalIndent(nic, "", " ") @@ -136,27 +139,27 @@ func outputResult(p *print.Printer, outputFormat string, nic *iaas.NIC) error { return nil default: table := tables.NewTable() - table.AddRow("ID", *nic.Id) + table.AddRow("ID", utils.PtrString(nic.Id)) table.AddSeparator() - table.AddRow("NETWORK ID", *nic.NetworkId) + table.AddRow("NETWORK ID", utils.PtrString(nic.NetworkId)) table.AddSeparator() if nic.Name != nil { - table.AddRow("NAME", *nic.Name) + table.AddRow("NAME", utils.PtrString(nic.Name)) table.AddSeparator() } if nic.Ipv4 != nil { - table.AddRow("IPV4", *nic.Ipv4) + table.AddRow("IPV4", utils.PtrString(nic.Ipv4)) table.AddSeparator() } if nic.Ipv6 != nil { - table.AddRow("IPV6", *nic.Ipv6) + table.AddRow("IPV6", utils.PtrString(nic.Ipv6)) table.AddSeparator() } table.AddRow("MAC", utils.PtrString(nic.Mac)) table.AddSeparator() table.AddRow("NIC SECURITY", utils.PtrString(nic.NicSecurity)) if nic.AllowedAddresses != nil && len(*nic.AllowedAddresses) > 0 { - allowedAddresses := []string{} + var allowedAddresses []string for _, value := range *nic.AllowedAddresses { allowedAddresses = append(allowedAddresses, *value.String) } @@ -164,7 +167,7 @@ func outputResult(p *print.Printer, outputFormat string, nic *iaas.NIC) error { table.AddRow("ALLOWED ADDRESSES", strings.Join(allowedAddresses, "\n")) } if nic.Labels != nil && len(*nic.Labels) > 0 { - labels := []string{} + var labels []string for key, value := range *nic.Labels { labels = append(labels, fmt.Sprintf("%s: %s", key, value)) } diff --git a/internal/cmd/beta/network-interface/describe/describe_test.go b/internal/cmd/beta/network-interface/describe/describe_test.go index 145a12cc8..967057b02 100644 --- a/internal/cmd/beta/network-interface/describe/describe_test.go +++ b/internal/cmd/beta/network-interface/describe/describe_test.go @@ -200,3 +200,37 @@ func TestBuildRequest(t *testing.T) { }) } } + +func Test_outputResult(t *testing.T) { + type args struct { + outputFormat string + nic *iaas.NIC + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: true, + }, + { + name: "set empty nic", + args: args{ + nic: &iaas.NIC{}, + }, + wantErr: false, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(p) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.nic); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/beta/network-interface/list/list.go b/internal/cmd/beta/network-interface/list/list.go index 4b8d6b314..b4925b7a5 100644 --- a/internal/cmd/beta/network-interface/list/list.go +++ b/internal/cmd/beta/network-interface/list/list.go @@ -82,6 +82,8 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { p.Debug(print.ErrorLevel, "get network name: %v", err) networkLabel = *model.NetworkId + } else if networkLabel == "" { + networkLabel = *model.NetworkId } p.Info("No network interfaces found for network %q\n", networkLabel) return nil diff --git a/internal/cmd/beta/network-interface/list/list_test.go b/internal/cmd/beta/network-interface/list/list_test.go index 3058072b0..97610156a 100644 --- a/internal/cmd/beta/network-interface/list/list_test.go +++ b/internal/cmd/beta/network-interface/list/list_test.go @@ -205,3 +205,30 @@ func TestBuildRequest(t *testing.T) { }) } } + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + nics []iaas.NIC + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: false, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(p) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.nics); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/beta/network-interface/update/update.go b/internal/cmd/beta/network-interface/update/update.go index 2b74557ac..9e7c12022 100644 --- a/internal/cmd/beta/network-interface/update/update.go +++ b/internal/cmd/beta/network-interface/update/update.go @@ -95,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update network interface: %w", err) } - return outputResult(p, model, resp) + return outputResult(p, model.OutputFormat, model.ProjectId, resp) }, } configureFlags(cmd) @@ -218,8 +218,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli return req.UpdateNicPayload(payload) } -func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error { - switch model.OutputFormat { +func outputResult(p *print.Printer, outputFormat, projectId string, nic *iaas.NIC) error { + if nic == nil { + return fmt.Errorf("nic is empty") + } + switch outputFormat { case print.JSONOutputFormat: details, err := json.MarshalIndent(nic, "", " ") if err != nil { @@ -237,7 +240,7 @@ func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error { return nil default: - p.Outputf("Updated network interface for project %q.\n", model.ProjectId) + p.Outputf("Updated network interface for project %q.\n", projectId) return nil } } diff --git a/internal/cmd/beta/network-interface/update/update_test.go b/internal/cmd/beta/network-interface/update/update_test.go index 00eeda71f..98987b829 100644 --- a/internal/cmd/beta/network-interface/update/update_test.go +++ b/internal/cmd/beta/network-interface/update/update_test.go @@ -291,3 +291,38 @@ func TestBuildRequest(t *testing.T) { }) } } + +func Test_outputResult(t *testing.T) { + type args struct { + outputFormat string + projectId string + nic *iaas.NIC + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: true, + }, + { + name: "set empty nic", + args: args{ + nic: &iaas.NIC{}, + }, + wantErr: false, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(p) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.projectId, tt.args.nic); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}