diff --git a/internal/cmd/mariadb/credentials/create/create.go b/internal/cmd/mariadb/credentials/create/create.go index 8fd77e4c1..880444860 100644 --- a/internal/cmd/mariadb/credentials/create/create.go +++ b/internal/cmd/mariadb/credentials/create/create.go @@ -79,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create MariaDB credentials: %w", err) } - return outputResult(p, model, instanceLabel, resp) + return outputResult(p, model.OutputFormat, model.ShowPassword, instanceLabel, resp) }, } configureFlags(cmd) @@ -123,11 +123,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API return req } -func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *mariadb.CredentialsResponse) error { - if !model.ShowPassword { +func outputResult(p *print.Printer, outputFormat string, showPassword bool, instanceLabel string, resp *mariadb.CredentialsResponse) error { + if resp == nil { + return fmt.Errorf("response is nil") + } + + if !showPassword && resp.HasRaw() && resp.Raw.Credentials != nil { resp.Raw.Credentials.Password = utils.Ptr("hidden") } - switch model.OutputFormat { + switch outputFormat { case print.JSONOutputFormat: details, err := json.MarshalIndent(resp, "", " ") if err != nil { @@ -151,7 +155,7 @@ func outputResult(p *print.Printer, model *inputModel, instanceLabel string, res if username := resp.Raw.Credentials.Username; username != nil && *username != "" { p.Outputf("Username: %s\n", *username) } - if !model.ShowPassword { + if !showPassword { p.Outputf("Password: \n") } else { p.Outputf("Password: %s\n", utils.PtrString(resp.Raw.Credentials.Password)) diff --git a/internal/cmd/mariadb/credentials/create/create_test.go b/internal/cmd/mariadb/credentials/create/create_test.go index 9d012577b..d3202e596 100644 --- a/internal/cmd/mariadb/credentials/create/create_test.go +++ b/internal/cmd/mariadb/credentials/create/create_test.go @@ -200,3 +200,40 @@ func TestBuildRequest(t *testing.T) { }) } } + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + showPassword bool + instanceLabel string + credentials *mariadb.CredentialsResponse + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: true, + }, + { + name: "set empty credentials", + args: args{ + credentials: &mariadb.CredentialsResponse{}, + }, + 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.showPassword, tt.args.instanceLabel, tt.args.credentials); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/mariadb/credentials/describe/describe.go b/internal/cmd/mariadb/credentials/describe/describe.go index 8739c7d59..8e3e3f869 100644 --- a/internal/cmd/mariadb/credentials/describe/describe.go +++ b/internal/cmd/mariadb/credentials/describe/describe.go @@ -112,6 +112,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API } func outputResult(p *print.Printer, outputFormat string, credentials *mariadb.CredentialsResponse) error { + if credentials == nil { + return fmt.Errorf("credentials is nil") + } + switch outputFormat { case print.JSONOutputFormat: details, err := json.MarshalIndent(credentials, "", " ") @@ -131,7 +135,7 @@ func outputResult(p *print.Printer, outputFormat string, credentials *mariadb.Cr return nil default: table := tables.NewTable() - table.AddRow("ID", *credentials.Id) + table.AddRow("ID", utils.PtrString(credentials.Id)) table.AddSeparator() // The username field cannot be set by the user so we only display it if it's not returned empty if credentials.HasRaw() && credentials.Raw.Credentials != nil { diff --git a/internal/cmd/mariadb/credentials/describe/describe_test.go b/internal/cmd/mariadb/credentials/describe/describe_test.go index 27e96b383..f822d88f4 100644 --- a/internal/cmd/mariadb/credentials/describe/describe_test.go +++ b/internal/cmd/mariadb/credentials/describe/describe_test.go @@ -243,3 +243,38 @@ func TestBuildRequest(t *testing.T) { }) } } + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + credentials *mariadb.CredentialsResponse + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: true, + }, + { + name: "set empty credentials", + args: args{ + credentials: &mariadb.CredentialsResponse{}, + }, + 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.credentials); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/mariadb/credentials/list/list_test.go b/internal/cmd/mariadb/credentials/list/list_test.go index ab667fbb4..978210a9f 100644 --- a/internal/cmd/mariadb/credentials/list/list_test.go +++ b/internal/cmd/mariadb/credentials/list/list_test.go @@ -207,3 +207,45 @@ func TestBuildRequest(t *testing.T) { }) } } + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + credentials []mariadb.CredentialsListItem + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: false, + }, + { + name: "set empty credentials slice", + args: args{ + credentials: []mariadb.CredentialsListItem{}, + }, + wantErr: false, + }, + { + name: "set empty credential in credentials slice", + args: args{ + credentials: []mariadb.CredentialsListItem{{}}, + }, + 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.credentials); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/mariadb/instance/create/create.go b/internal/cmd/mariadb/instance/create/create.go index 81732c1a6..3a2a4e4fb 100644 --- a/internal/cmd/mariadb/instance/create/create.go +++ b/internal/cmd/mariadb/instance/create/create.go @@ -125,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - return outputResult(p, model, projectLabel, resp) + return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp) }, } configureFlags(cmd) @@ -251,8 +251,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient mariaDBClien return req, nil } -func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *mariadb.CreateInstanceResponse) error { - switch model.OutputFormat { +func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, resp *mariadb.CreateInstanceResponse) error { + if resp == nil { + return fmt.Errorf("response is nil") + } + + switch outputFormat { case print.JSONOutputFormat: details, err := json.MarshalIndent(resp, "", " ") if err != nil { @@ -271,7 +275,7 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp return nil default: operationState := "Created" - if model.Async { + if async { operationState = "Triggered creation of" } p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, utils.PtrString(resp.InstanceId)) diff --git a/internal/cmd/mariadb/instance/create/create_test.go b/internal/cmd/mariadb/instance/create/create_test.go index 554b7db9a..0e797ff1e 100644 --- a/internal/cmd/mariadb/instance/create/create_test.go +++ b/internal/cmd/mariadb/instance/create/create_test.go @@ -463,3 +463,40 @@ func TestBuildRequest(t *testing.T) { }) } } + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + async bool + projectLabel string + resp *mariadb.CreateInstanceResponse + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: true, + }, + { + name: "set empty response", + args: args{ + resp: &mariadb.CreateInstanceResponse{}, + }, + 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.async, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/mariadb/instance/describe/describe.go b/internal/cmd/mariadb/instance/describe/describe.go index 952754762..b3b8013a6 100644 --- a/internal/cmd/mariadb/instance/describe/describe.go +++ b/internal/cmd/mariadb/instance/describe/describe.go @@ -100,6 +100,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API } func outputResult(p *print.Printer, outputFormat string, instance *mariadb.Instance) error { + if instance == nil { + return fmt.Errorf("instance is nil") + } + switch outputFormat { case print.JSONOutputFormat: details, err := json.MarshalIndent(instance, "", " ") @@ -123,18 +127,22 @@ func outputResult(p *print.Printer, outputFormat string, instance *mariadb.Insta table.AddSeparator() table.AddRow("NAME", utils.PtrString(instance.Name)) table.AddSeparator() - table.AddRow("LAST OPERATION TYPE", utils.PtrString(instance.LastOperation.Type)) - table.AddSeparator() - table.AddRow("LAST OPERATION STATE", utils.PtrString(instance.LastOperation.State)) - table.AddSeparator() + if instance.LastOperation != nil { + table.AddRow("LAST OPERATION TYPE", utils.PtrString(instance.LastOperation.Type)) + table.AddSeparator() + table.AddRow("LAST OPERATION STATE", utils.PtrString(instance.LastOperation.State)) + table.AddSeparator() + } table.AddRow("PLAN ID", utils.PtrString(instance.PlanId)) // Only show ACL if it's present and not empty - acl := (*instance.Parameters)[aclParameterKey] - aclStr, ok := acl.(string) - if ok { - if aclStr != "" { - table.AddSeparator() - table.AddRow("ACL", aclStr) + if instance.Parameters != nil { + acl := (*instance.Parameters)[aclParameterKey] + aclStr, ok := acl.(string) + if ok { + if aclStr != "" { + table.AddSeparator() + table.AddRow("ACL", aclStr) + } } } err := table.Display(p) diff --git a/internal/cmd/mariadb/instance/describe/describe_test.go b/internal/cmd/mariadb/instance/describe/describe_test.go index f7bb4cfca..555f7bcbf 100644 --- a/internal/cmd/mariadb/instance/describe/describe_test.go +++ b/internal/cmd/mariadb/instance/describe/describe_test.go @@ -216,3 +216,38 @@ func TestBuildRequest(t *testing.T) { }) } } + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + instance *mariadb.Instance + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: true, + }, + { + name: "set empty instance", + args: args{ + instance: &mariadb.Instance{}, + }, + 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.instance); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/mariadb/instance/list/list.go b/internal/cmd/mariadb/instance/list/list.go index 37c5afff4..2b2dbe70c 100644 --- a/internal/cmd/mariadb/instance/list/list.go +++ b/internal/cmd/mariadb/instance/list/list.go @@ -152,11 +152,18 @@ func outputResult(p *print.Printer, outputFormat string, instances []mariadb.Ins table.SetHeader("ID", "NAME", "LAST OPERATION TYPE", "LAST OPERATION STATE") for i := range instances { instance := instances[i] + + lastOperationType, lastOperationState := "", "" + if instance.LastOperation != nil { + lastOperationType = utils.PtrString(instance.LastOperation.Type) + lastOperationState = utils.PtrString(instance.LastOperation.State) + } + table.AddRow( utils.PtrString(instance.InstanceId), utils.PtrString(instance.Name), - utils.PtrString(instance.LastOperation.Type), - utils.PtrString(instance.LastOperation.State), + lastOperationType, + lastOperationState, ) } err := table.Display(p) diff --git a/internal/cmd/mariadb/instance/list/list_test.go b/internal/cmd/mariadb/instance/list/list_test.go index 82ed3955f..a7b29714f 100644 --- a/internal/cmd/mariadb/instance/list/list_test.go +++ b/internal/cmd/mariadb/instance/list/list_test.go @@ -186,3 +186,45 @@ func TestBuildRequest(t *testing.T) { }) } } + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + instances []mariadb.Instance + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: false, + }, + { + name: "set empty instances slice", + args: args{ + instances: []mariadb.Instance{}, + }, + wantErr: false, + }, + { + name: "set empty instance in instances slice", + args: args{ + instances: []mariadb.Instance{{}}, + }, + 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.instances); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/mariadb/plans/plans.go b/internal/cmd/mariadb/plans/plans.go index bd3d31957..e4d3bf021 100644 --- a/internal/cmd/mariadb/plans/plans.go +++ b/internal/cmd/mariadb/plans/plans.go @@ -152,17 +152,19 @@ func outputResult(p *print.Printer, outputFormat string, plans []mariadb.Offerin table.SetHeader("OFFERING NAME", "VERSION", "ID", "NAME", "DESCRIPTION") for i := range plans { o := plans[i] - for j := range *o.Plans { - plan := (*o.Plans)[j] - table.AddRow( - utils.PtrString(o.Name), - utils.PtrString(o.Version), - utils.PtrString(plan.Id), - utils.PtrString(plan.Name), - utils.PtrString(plan.Description), - ) + if o.Plans != nil { + for j := range *o.Plans { + plan := (*o.Plans)[j] + table.AddRow( + utils.PtrString(o.Name), + utils.PtrString(o.Version), + utils.PtrString(plan.Id), + utils.PtrString(plan.Name), + utils.PtrString(plan.Description), + ) + } + table.AddSeparator() } - table.AddSeparator() } table.EnableAutoMergeOnColumns(1, 2) err := table.Display(p) diff --git a/internal/cmd/mariadb/plans/plans_test.go b/internal/cmd/mariadb/plans/plans_test.go index 7c6e4b7d7..3d8f56a28 100644 --- a/internal/cmd/mariadb/plans/plans_test.go +++ b/internal/cmd/mariadb/plans/plans_test.go @@ -186,3 +186,45 @@ func TestBuildRequest(t *testing.T) { }) } } + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + plans []mariadb.Offering + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: false, + }, + { + name: "set empty plans slice", + args: args{ + plans: []mariadb.Offering{}, + }, + wantErr: false, + }, + { + name: "set empty plan in plans slice", + args: args{ + plans: []mariadb.Offering{{}}, + }, + 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.plans); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}