From f98d8ddd8d9289c1e99c119e43f7d11f1c15cdc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Mon, 22 Apr 2024 17:31:32 +0200 Subject: [PATCH 1/9] fix typo in error message --- internal/cmd/curl/curl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/curl/curl.go b/internal/cmd/curl/curl.go index 694261d4e..292effffc 100644 --- a/internal/cmd/curl/curl.go +++ b/internal/cmd/curl/curl.go @@ -212,7 +212,7 @@ func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) er } respBody, err := io.ReadAll(resp.Body) if err != nil { - return fmt.Errorf("read respose body: %w", err) + return fmt.Errorf("read response body: %w", err) } output = append(output, respBody...) From e72e8f69f8373ccb9a0750fbfd3b077de90c1c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Mon, 22 Apr 2024 18:06:28 +0200 Subject: [PATCH 2/9] remove unused flag --- internal/cmd/mariadb/instance/update/update.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/cmd/mariadb/instance/update/update.go b/internal/cmd/mariadb/instance/update/update.go index f22ae7863..d2c857d4a 100644 --- a/internal/cmd/mariadb/instance/update/update.go +++ b/internal/cmd/mariadb/instance/update/update.go @@ -25,7 +25,6 @@ import ( const ( instanceIdArg = "INSTANCE_ID" - instanceNameFlag = "name" enableMonitoringFlag = "enable-monitoring" graphiteFlag = "graphite" metricsFrequencyFlag = "metrics-frequency" From a89f23c0994b271e600f5879f642535810e4d082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 23 Apr 2024 09:32:21 +0200 Subject: [PATCH 3/9] add json output format for the responses --- internal/cmd/argus/instance/create/create.go | 26 +++- internal/cmd/config/list/list.go | 113 ++++++++++++------ internal/cmd/dns/record-set/create/create.go | 26 +++- internal/cmd/dns/zone/create/create.go | 26 +++- .../cmd/logme/credentials/create/create.go | 44 ++++--- internal/cmd/logme/instance/create/create.go | 26 +++- .../cmd/mariadb/credentials/create/create.go | 44 ++++--- .../cmd/mariadb/instance/create/create.go | 26 +++- .../cmd/mongodbflex/instance/create/create.go | 26 +++- .../cmd/mongodbflex/instance/update/update.go | 26 +++- .../cmd/mongodbflex/user/create/create.go | 39 ++++-- .../user/reset-password/reset_password.go | 27 ++++- .../object-storage/bucket/create/create.go | 28 ++++- .../credentials-group/create/create.go | 22 +++- .../credentials/create/create.go | 34 ++++-- .../opensearch/credentials/create/create.go | 42 +++++-- .../cmd/opensearch/instance/create/create.go | 26 +++- .../cmd/postgresflex/instance/clone/clone.go | 27 ++++- .../postgresflex/instance/create/create.go | 26 +++- .../postgresflex/instance/update/update.go | 26 +++- .../cmd/postgresflex/user/create/create.go | 36 ++++-- .../user/reset-password/reset_password.go | 26 +++- internal/cmd/project/create/create.go | 20 +++- .../cmd/rabbitmq/credentials/create/create.go | 44 ++++--- .../cmd/rabbitmq/instance/create/create.go | 26 +++- .../cmd/redis/credentials/create/create.go | 44 ++++--- internal/cmd/redis/instance/create/create.go | 26 +++- .../secrets-manager/instance/create/create.go | 20 +++- .../cmd/secrets-manager/user/create/create.go | 30 +++-- internal/cmd/service-account/create/create.go | 20 +++- .../service-account/token/create/create.go | 24 +++- internal/cmd/ske/cluster/create/create.go | 25 +++- internal/cmd/ske/cluster/update/update.go | 25 +++- internal/cmd/ske/kubeconfig/create/create.go | 20 +++- 34 files changed, 815 insertions(+), 251 deletions(-) diff --git a/internal/cmd/argus/instance/create/create.go b/internal/cmd/argus/instance/create/create.go index 245097b49..901f47bfa 100644 --- a/internal/cmd/argus/instance/create/create.go +++ b/internal/cmd/argus/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "errors" "fmt" @@ -102,12 +103,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -190,3 +196,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient argusClient) }) return req, nil } + +func outputResult(p *print.Printer, resp *argus.CreateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Argus instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/config/list/list.go b/internal/cmd/config/list/list.go index 0f6974703..52c8cdda3 100644 --- a/internal/cmd/config/list/list.go +++ b/internal/cmd/config/list/list.go @@ -1,7 +1,9 @@ package list import ( + "encoding/json" "fmt" + "slices" "sort" "strconv" @@ -10,6 +12,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/config" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/tables" @@ -17,6 +20,10 @@ import ( "github.com/spf13/viper" ) +type inputModel struct { + *globalflags.GlobalFlagModel +} + func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "list", @@ -35,56 +42,88 @@ func NewCmd(p *print.Printer) *cobra.Command { examples.NewExample( `List your active configuration`, "$ stackit config list"), + examples.NewExample( + `List your active configuration in a json format`, + "$ stackit config list --output-format json"), ), RunE: func(cmd *cobra.Command, args []string) error { configData := viper.AllSettings() - // Sort the config options by key - configKeys := make([]string, 0, len(configData)) - for k := range configData { - configKeys = append(configKeys, k) + model, err := parseInput(p, cmd) + if err != nil { + return err } - sort.Strings(configKeys) + return outputResult(p, model.OutputFormat, configData) - table := tables.NewTable() - table.SetHeader("NAME", "VALUE") - for _, key := range configKeys { - value := configData[key] + }, + } + return cmd +} - // Convert value to string - // (Assuming value is either string or bool) - valueString, ok := value.(string) - if !ok { - valueBool, ok := value.(bool) - if !ok { - continue - } - valueString = strconv.FormatBool(valueBool) - } +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { + globalFlags := globalflags.Parse(p, cmd) - // Don't show unset values - if valueString == "" { - continue - } + return &inputModel{ + GlobalFlagModel: globalFlags, + }, nil +} - // Don't show unsupported (deprecated or user-inputted) configuration options - // that might be present in the config file - if !slices.Contains(config.ConfigKeys, key) { +func outputResult(p *print.Printer, outputFormat string, configData map[string]any) error { + switch outputFormat { + case globalflags.JSONOutputFormat: + details, err := json.MarshalIndent(configData, "", " ") + if err != nil { + return fmt.Errorf("marshal config list: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + // Sort the config options by key + configKeys := make([]string, 0, len(configData)) + for k := range configData { + configKeys = append(configKeys, k) + } + sort.Strings(configKeys) + + table := tables.NewTable() + table.SetHeader("NAME", "VALUE") + for _, key := range configKeys { + value := configData[key] + + // Convert value to string + // (Assuming value is either string or bool) + valueString, ok := value.(string) + if !ok { + valueBool, ok := value.(bool) + if !ok { continue } + valueString = strconv.FormatBool(valueBool) + } - // Replace "_" with "-" to match the flags - key = strings.ReplaceAll(key, "_", "-") - - table.AddRow(key, valueString) - table.AddSeparator() + // Don't show unset values + if valueString == "" { + continue } - err := table.Display(p) - if err != nil { - return fmt.Errorf("render table: %w", err) + + // Don't show unsupported (deprecated or user-inputted) configuration options + // that might be present in the config file + if !slices.Contains(config.ConfigKeys, key) { + continue } - return nil - }, + + // Replace "_" with "-" to match the flags + key = strings.ReplaceAll(key, "_", "-") + + table.AddRow(key, valueString) + table.AddSeparator() + } + err := table.Display(p) + if err != nil { + return fmt.Errorf("render table: %w", err) + } + return nil } - return cmd + } diff --git a/internal/cmd/dns/record-set/create/create.go b/internal/cmd/dns/record-set/create/create.go index 7d3fbbd4e..722e03045 100644 --- a/internal/cmd/dns/record-set/create/create.go +++ b/internal/cmd/dns/record-set/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -98,12 +99,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s record set for zone %s. Record set ID: %s\n", operationState, zoneLabel, recordSetId) + return nil } - p.Outputf("%s record set for zone %s. Record set ID: %s\n", operationState, zoneLabel, recordSetId) - return nil }, } configureFlags(cmd) @@ -157,3 +163,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie }) return req } + +func outputResult(p *print.Printer, resp *dns.RecordSetResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal DNS record-set: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/dns/zone/create/create.go b/internal/cmd/dns/zone/create/create.go index abe905bee..5db759980 100644 --- a/internal/cmd/dns/zone/create/create.go +++ b/internal/cmd/dns/zone/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -112,12 +113,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s zone for project %q. Zone ID: %s\n", operationState, projectLabel, zoneId) + return nil } - p.Outputf("%s zone for project %q. Zone ID: %s\n", operationState, projectLabel, zoneId) - return nil }, } configureFlags(cmd) @@ -186,3 +192,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie }) return req } + +func outputResult(p *print.Printer, resp *dns.ZoneResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal DNS zone: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/logme/credentials/create/create.go b/internal/cmd/logme/credentials/create/create.go index 7120d721f..885dfa94a 100644 --- a/internal/cmd/logme/credentials/create/create.go +++ b/internal/cmd/logme/credentials/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -76,21 +77,26 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create LogMe credentials: %w", err) } - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) - // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil }, } configureFlags(cmd) @@ -122,3 +128,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl req := apiClient.CreateCredentials(ctx, model.ProjectId, model.InstanceId) return req } + +func outputResult(p *print.Printer, resp *logme.CredentialsResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal LogMe credentials: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/logme/instance/create/create.go b/internal/cmd/logme/instance/create/create.go index 472eb4f0e..149b5a727 100644 --- a/internal/cmd/logme/instance/create/create.go +++ b/internal/cmd/logme/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "errors" "fmt" "strings" @@ -123,12 +124,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -242,3 +248,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient logMeClient) }) return req, nil } + +func outputResult(p *print.Printer, resp *logme.CreateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal LogMe instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/mariadb/credentials/create/create.go b/internal/cmd/mariadb/credentials/create/create.go index 122bef421..eead95973 100644 --- a/internal/cmd/mariadb/credentials/create/create.go +++ b/internal/cmd/mariadb/credentials/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -76,21 +77,26 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create MariaDB credentials: %w", err) } - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) - // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil }, } configureFlags(cmd) @@ -122,3 +128,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API req := apiClient.CreateCredentials(ctx, model.ProjectId, model.InstanceId) return req } + +func outputResult(p *print.Printer, resp *mariadb.CredentialsResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal MariaDB credentials: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/mariadb/instance/create/create.go b/internal/cmd/mariadb/instance/create/create.go index 2b0b4e3ef..315384f10 100644 --- a/internal/cmd/mariadb/instance/create/create.go +++ b/internal/cmd/mariadb/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "errors" "fmt" "strings" @@ -123,12 +124,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -242,3 +248,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient mariaDBClien }) return req, nil } + +func outputResult(p *print.Printer, resp *mariadb.CreateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal MariaDB instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/mongodbflex/instance/create/create.go b/internal/cmd/mongodbflex/instance/create/create.go index 91b2178f8..2ead97102 100644 --- a/internal/cmd/mongodbflex/instance/create/create.go +++ b/internal/cmd/mongodbflex/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "errors" "fmt" @@ -131,12 +132,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -264,3 +270,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient MongoDBFlexC }) return req, nil } + +func outputResult(p *print.Printer, resp *mongodbflex.CreateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal MongoDBFlex instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/mongodbflex/instance/update/update.go b/internal/cmd/mongodbflex/instance/update/update.go index 69ae210d5..9b44b1572 100644 --- a/internal/cmd/mongodbflex/instance/update/update.go +++ b/internal/cmd/mongodbflex/instance/update/update.go @@ -2,6 +2,7 @@ package update import ( "context" + "encoding/json" "errors" "fmt" @@ -116,12 +117,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Updated" - if model.Async { - operationState = "Triggered update of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Updated" + if model.Async { + operationState = "Triggered update of" + } + p.Info("%s instance %q\n", operationState, instanceLabel) + return nil } - p.Info("%s instance %q\n", operationState, instanceLabel) - return nil }, } configureFlags(cmd) @@ -298,3 +304,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient MongoDBFlexC }) return req, nil } + +func outputResult(p *print.Printer, resp *mongodbflex.UpdateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal update MongoDBFlex instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/mongodbflex/user/create/create.go b/internal/cmd/mongodbflex/user/create/create.go index 27825a5f0..ccded672e 100644 --- a/internal/cmd/mongodbflex/user/create/create.go +++ b/internal/cmd/mongodbflex/user/create/create.go @@ -2,8 +2,10 @@ package create import ( "context" + "encoding/json" "fmt" + "github.com/spf13/cobra" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -12,8 +14,6 @@ import ( "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" mongodbflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/utils" - - "github.com/spf13/cobra" "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex" ) @@ -91,16 +91,21 @@ func NewCmd(p *print.Printer) *cobra.Command { } user := resp.Item - p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) - p.Outputf("Username: %s\n", *user.Username) - p.Outputf("Password: %s\n", *user.Password) - p.Outputf("Roles: %v\n", *user.Roles) - p.Outputf("Database: %s\n", *user.Database) - p.Outputf("Host: %s\n", *user.Host) - p.Outputf("Port: %d\n", *user.Port) - p.Outputf("URI: %s\n", *user.Uri) - - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, user) + default: + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("Password: %s\n", *user.Password) + p.Outputf("Roles: %v\n", *user.Roles) + p.Outputf("Database: %s\n", *user.Database) + p.Outputf("Host: %s\n", *user.Host) + p.Outputf("Port: %d\n", *user.Port) + p.Outputf("URI: %s\n", *user.Uri) + + return nil + } }, } @@ -144,3 +149,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex }) return req } + +func outputResult(p *print.Printer, user *mongodbflex.User) error { + details, err := json.MarshalIndent(user, "", " ") + if err != nil { + return fmt.Errorf("marshal MongoDB Flex user: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/mongodbflex/user/reset-password/reset_password.go b/internal/cmd/mongodbflex/user/reset-password/reset_password.go index c9e60ec13..8d269a010 100644 --- a/internal/cmd/mongodbflex/user/reset-password/reset_password.go +++ b/internal/cmd/mongodbflex/user/reset-password/reset_password.go @@ -2,6 +2,7 @@ package resetpassword import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -85,11 +86,17 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("reset MongoDB Flex user password: %w", err) } - p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) - p.Outputf("Username: %s\n", *user.Username) - p.Outputf("New password: %s\n", *user.Password) - p.Outputf("New URI: %s\n", *user.Uri) - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, user) + default: + p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("New password: %s\n", *user.Password) + p.Outputf("New URI: %s\n", *user.Uri) + return nil + } + }, } @@ -123,3 +130,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex req := apiClient.ResetUser(ctx, model.ProjectId, model.InstanceId, model.UserId) return req } + +func outputResult(p *print.Printer, user *mongodbflex.User) error { + details, err := json.MarshalIndent(user, "", " ") + if err != nil { + return fmt.Errorf("marshal MongoDB Flex reset password: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/object-storage/bucket/create/create.go b/internal/cmd/object-storage/bucket/create/create.go index 9409f257f..4451b445f 100644 --- a/internal/cmd/object-storage/bucket/create/create.go +++ b/internal/cmd/object-storage/bucket/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -60,7 +61,7 @@ func NewCmd(p *print.Printer) *cobra.Command { // Call API req := buildRequest(ctx, model, apiClient) - _, err = req.Execute() + resp, err := req.Execute() if err != nil { return fmt.Errorf("create Object Storage bucket: %w", err) } @@ -76,12 +77,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s bucket %q\n", operationState, model.BucketName) + return nil } - p.Outputf("%s bucket %q\n", operationState, model.BucketName) - return nil }, } return cmd @@ -105,3 +111,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora req := apiClient.CreateBucket(ctx, model.ProjectId, model.BucketName) return req } + +func outputResult(p *print.Printer, resp *objectstorage.CreateBucketResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Object Storage bucket: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/object-storage/credentials-group/create/create.go b/internal/cmd/object-storage/credentials-group/create/create.go index 1400289b5..4c2ee9484 100644 --- a/internal/cmd/object-storage/credentials-group/create/create.go +++ b/internal/cmd/object-storage/credentials-group/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -65,9 +66,14 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Object Storage credentials group: %w", err) } - p.Outputf("Created credentials group %q. Credentials group ID: %s\n\n", *resp.CredentialsGroup.DisplayName, *resp.CredentialsGroup.CredentialsGroupId) - p.Outputf("URN: %s\n", *resp.CredentialsGroup.Urn) - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created credentials group %q. Credentials group ID: %s\n\n", *resp.CredentialsGroup.DisplayName, *resp.CredentialsGroup.CredentialsGroupId) + p.Outputf("URN: %s\n", *resp.CredentialsGroup.Urn) + return nil + } }, } configureFlags(cmd) @@ -100,3 +106,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora }) return req } + +func outputResult(p *print.Printer, resp *objectstorage.CreateCredentialsGroupResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Object Storage credentials group: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/object-storage/credentials/create/create.go b/internal/cmd/object-storage/credentials/create/create.go index d9b26ab47..152f2909d 100644 --- a/internal/cmd/object-storage/credentials/create/create.go +++ b/internal/cmd/object-storage/credentials/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "time" @@ -79,17 +80,22 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Object Storage credentials: %w", err) } - expireDate := "Never" - if resp.Expires != nil && *resp.Expires != "" { - expireDate = *resp.Expires - } + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + expireDate := "Never" + if resp.Expires != nil && *resp.Expires != "" { + expireDate = *resp.Expires + } - p.Outputf("Created credentials in group %q. Credentials ID: %s\n\n", credentialsGroupLabel, *resp.KeyId) - p.Outputf("Access Key ID: %s\n", *resp.AccessKey) - p.Outputf("Secret Access Key: %s\n", *resp.SecretAccessKey) - p.Outputf("Expire Date: %s\n", expireDate) + p.Outputf("Created credentials in group %q. Credentials ID: %s\n\n", credentialsGroupLabel, *resp.KeyId) + p.Outputf("Access Key ID: %s\n", *resp.AccessKey) + p.Outputf("Secret Access Key: %s\n", *resp.SecretAccessKey) + p.Outputf("Expire Date: %s\n", expireDate) - return nil + return nil + } }, } configureFlags(cmd) @@ -133,3 +139,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora }) return req } + +func outputResult(p *print.Printer, resp *objectstorage.CreateAccessKeyResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Object Storage credentials: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/opensearch/credentials/create/create.go b/internal/cmd/opensearch/credentials/create/create.go index 1a6affef6..5f99641b7 100644 --- a/internal/cmd/opensearch/credentials/create/create.go +++ b/internal/cmd/opensearch/credentials/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -76,20 +77,25 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create OpenSearch credentials: %w", err) } - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil }, } configureFlags(cmd) @@ -121,3 +127,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch. req := apiClient.CreateCredentials(ctx, model.ProjectId, model.InstanceId) return req } + +func outputResult(p *print.Printer, resp *opensearch.CredentialsResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal OpenSearch credential: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/opensearch/instance/create/create.go b/internal/cmd/opensearch/instance/create/create.go index b3cb95b18..abc60df02 100644 --- a/internal/cmd/opensearch/instance/create/create.go +++ b/internal/cmd/opensearch/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "errors" "fmt" "strings" @@ -125,12 +126,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -247,3 +253,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient openSearchCl }) return req, nil } + +func outputResult(p *print.Printer, resp *opensearch.CreateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal OpenSearch instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/postgresflex/instance/clone/clone.go b/internal/cmd/postgresflex/instance/clone/clone.go index 1007bf548..d83880ec4 100644 --- a/internal/cmd/postgresflex/instance/clone/clone.go +++ b/internal/cmd/postgresflex/instance/clone/clone.go @@ -2,6 +2,7 @@ package clone import ( "context" + "encoding/json" "fmt" "time" @@ -107,13 +108,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Cloned" - if model.Async { - operationState = "Triggered cloning of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Cloned" + if model.Async { + operationState = "Triggered cloning of" + } + p.Info("%s instance from instance %q. New Instance ID: %s\n", operationState, instanceLabel, instanceId) + return nil } - - p.Info("%s instance from instance %q. New Instance ID: %s\n", operationState, instanceLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -198,3 +203,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient PostgreSQLFl }) return req, nil } + +func outputResult(p *print.Printer, resp *postgresflex.CloneInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex instance clone: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/postgresflex/instance/create/create.go b/internal/cmd/postgresflex/instance/create/create.go index c9141c17f..10d20118b 100644 --- a/internal/cmd/postgresflex/instance/create/create.go +++ b/internal/cmd/postgresflex/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "errors" "fmt" @@ -131,12 +132,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -264,3 +270,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient PostgreSQLFl }) return req, nil } + +func outputResult(p *print.Printer, resp *postgresflex.CreateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/postgresflex/instance/update/update.go b/internal/cmd/postgresflex/instance/update/update.go index 118e0a640..9e3dc3167 100644 --- a/internal/cmd/postgresflex/instance/update/update.go +++ b/internal/cmd/postgresflex/instance/update/update.go @@ -2,6 +2,7 @@ package update import ( "context" + "encoding/json" "errors" "fmt" @@ -116,12 +117,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Updated" - if model.Async { - operationState = "Triggered update of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Updated" + if model.Async { + operationState = "Triggered update of" + } + p.Info("%s instance %q\n", operationState, instanceLabel) + return nil } - p.Info("%s instance %q\n", operationState, instanceLabel) - return nil }, } configureFlags(cmd) @@ -298,3 +304,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient PostgreSQLFl }) return req, nil } + +func outputResult(p *print.Printer, resp *postgresflex.PartialUpdateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/postgresflex/user/create/create.go b/internal/cmd/postgresflex/user/create/create.go index 10f57edf1..e25872f42 100644 --- a/internal/cmd/postgresflex/user/create/create.go +++ b/internal/cmd/postgresflex/user/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -87,17 +88,22 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { return fmt.Errorf("create PostgreSQL Flex user: %w", err) } - user := resp.Item - p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) - p.Outputf("Username: %s\n", *user.Username) - p.Outputf("Password: %s\n", *user.Password) - p.Outputf("Roles: %v\n", *user.Roles) - p.Outputf("Host: %s\n", *user.Host) - p.Outputf("Port: %d\n", *user.Port) - p.Outputf("URI: %s\n", *user.Uri) - - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + user := resp.Item + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("Password: %s\n", *user.Password) + p.Outputf("Roles: %v\n", *user.Roles) + p.Outputf("Host: %s\n", *user.Host) + p.Outputf("Port: %d\n", *user.Port) + p.Outputf("URI: %s\n", *user.Uri) + + return nil + } }, } @@ -138,3 +144,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle }) return req } + +func outputResult(p *print.Printer, resp *postgresflex.CreateUserResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex user: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/postgresflex/user/reset-password/reset_password.go b/internal/cmd/postgresflex/user/reset-password/reset_password.go index b18ad672b..864453b67 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password.go @@ -2,6 +2,7 @@ package resetpassword import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -84,11 +85,16 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("reset PostgreSQL Flex user password: %w", err) } - p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) - p.Outputf("Username: %s\n", *user.Item.Username) - p.Outputf("New password: %s\n", *user.Item.Password) - p.Outputf("New URI: %s\n", *user.Item.Uri) - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, user) + default: + p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) + p.Outputf("Username: %s\n", *user.Item.Username) + p.Outputf("New password: %s\n", *user.Item.Password) + p.Outputf("New URI: %s\n", *user.Item.Uri) + return nil + } }, } @@ -122,3 +128,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle req := apiClient.ResetUser(ctx, model.ProjectId, model.InstanceId, model.UserId) return req } + +func outputResult(p *print.Printer, resp *postgresflex.ResetUserResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex user: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/project/create/create.go b/internal/cmd/project/create/create.go index cfcbf1c06..f7f822ad4 100644 --- a/internal/cmd/project/create/create.go +++ b/internal/cmd/project/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "regexp" @@ -81,8 +82,13 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create project: %w", err) } - p.Outputf("Created project under the parent with ID %q. Project ID: %s\n", *model.ParentId, *resp.ProjectId) - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created project under the parent with ID %q. Project ID: %s\n", *model.ParentId, *resp.ProjectId) + return nil + } }, } configureFlags(cmd) @@ -176,3 +182,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *resourceman return req, nil } + +func outputResult(p *print.Printer, resp *resourcemanager.ProjectResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal project: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/rabbitmq/credentials/create/create.go b/internal/cmd/rabbitmq/credentials/create/create.go index bb7c21ae9..fbc64ed2f 100644 --- a/internal/cmd/rabbitmq/credentials/create/create.go +++ b/internal/cmd/rabbitmq/credentials/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -76,21 +77,26 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create RabbitMQ credentials: %w", err) } - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) - // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil }, } configureFlags(cmd) @@ -122,3 +128,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP req := apiClient.CreateCredentials(ctx, model.ProjectId, model.InstanceId) return req } + +func outputResult(p *print.Printer, resp *rabbitmq.CredentialsResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal RabbitMQ credentials: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/rabbitmq/instance/create/create.go b/internal/cmd/rabbitmq/instance/create/create.go index d931f44fc..4153f7278 100644 --- a/internal/cmd/rabbitmq/instance/create/create.go +++ b/internal/cmd/rabbitmq/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "errors" "fmt" "strings" @@ -125,12 +126,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -247,3 +253,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient rabbitMQClie }) return req, nil } + +func outputResult(p *print.Printer, resp *rabbitmq.CreateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal RabbitMQ instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/redis/credentials/create/create.go b/internal/cmd/redis/credentials/create/create.go index db39a1e72..5ce8e2e5c 100644 --- a/internal/cmd/redis/credentials/create/create.go +++ b/internal/cmd/redis/credentials/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -76,21 +77,26 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Redis credentials: %w", err) } - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) - // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil }, } configureFlags(cmd) @@ -122,3 +128,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl req := apiClient.CreateCredentials(ctx, model.ProjectId, model.InstanceId) return req } + +func outputResult(p *print.Printer, resp *redis.CredentialsResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Redis credentials: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/redis/instance/create/create.go b/internal/cmd/redis/instance/create/create.go index 0843bcbdf..879b88e00 100644 --- a/internal/cmd/redis/instance/create/create.go +++ b/internal/cmd/redis/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "errors" "fmt" "strings" @@ -123,12 +124,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil }, } configureFlags(cmd) @@ -242,3 +248,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient redisClient) }) return req, nil } + +func outputResult(p *print.Printer, resp *redis.CreateInstanceResponse) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Redis instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/secrets-manager/instance/create/create.go b/internal/cmd/secrets-manager/instance/create/create.go index 8d0539d33..101bd6e97 100644 --- a/internal/cmd/secrets-manager/instance/create/create.go +++ b/internal/cmd/secrets-manager/instance/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -92,8 +93,13 @@ If you want to retry configuring the ACLs, you can do it via: } } - p.Outputf("Created instance for project %q. Instance ID: %s\n", projectLabel, instanceId) - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created instance for project %q. Instance ID: %s\n", projectLabel, instanceId) + return nil + } }, } configureFlags(cmd) @@ -144,3 +150,13 @@ func buildUpdateACLsRequest(ctx context.Context, model *inputModel, instanceId s return req } + +func outputResult(p *print.Printer, resp *secretsmanager.Instance) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Secrets Manager instance: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/secrets-manager/user/create/create.go b/internal/cmd/secrets-manager/user/create/create.go index da9d4597f..50e55bdd2 100644 --- a/internal/cmd/secrets-manager/user/create/create.go +++ b/internal/cmd/secrets-manager/user/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -84,13 +85,18 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Secrets Manager user: %w", err) } - p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *resp.Id) - p.Outputf("Username: %s\n", *resp.Username) - p.Outputf("Password: %s\n", *resp.Password) - p.Outputf("Description: %s\n", *resp.Description) - p.Outputf("Write Access: %t\n", *resp.Write) - - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *resp.Id) + p.Outputf("Username: %s\n", *resp.Username) + p.Outputf("Password: %s\n", *resp.Password) + p.Outputf("Description: %s\n", *resp.Description) + p.Outputf("Write Access: %t\n", *resp.Write) + + return nil + } }, } @@ -129,3 +135,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmana }) return req } + +func outputResult(p *print.Printer, resp *secretsmanager.User) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Secrets Manager user: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/service-account/create/create.go b/internal/cmd/service-account/create/create.go index aa622af30..3c39bf72f 100644 --- a/internal/cmd/service-account/create/create.go +++ b/internal/cmd/service-account/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -71,8 +72,13 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create service account: %w", err) } - p.Outputf("Created service account for project %q. Email: %s\n", projectLabel, *resp.Email) - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created service account for project %q. Email: %s\n", projectLabel, *resp.Email) + return nil + } }, } configureFlags(cmd) @@ -105,3 +111,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco }) return req } + +func outputResult(p *print.Printer, serviceAccount *serviceaccount.ServiceAccount) error { + details, err := json.MarshalIndent(serviceAccount, "", " ") + if err != nil { + return fmt.Errorf("marshal service account: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/service-account/token/create/create.go b/internal/cmd/service-account/token/create/create.go index 351f1544e..683b0fc92 100644 --- a/internal/cmd/service-account/token/create/create.go +++ b/internal/cmd/service-account/token/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -76,10 +77,15 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create access token: %w", err) } - p.Outputf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, *token.Id) - p.Outputf("Valid until: %s\n", *token.ValidUntil) - p.Outputf("Token: %s\n", *token.Token) - return nil + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, token) + default: + p.Outputf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, *token.Id) + p.Outputf("Valid until: %s\n", *token.ValidUntil) + p.Outputf("Token: %s\n", *token.Token) + return nil + } }, } @@ -131,3 +137,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco }) return req } + +func outputResult(p *print.Printer, token *serviceaccount.AccessToken) error { + details, err := json.MarshalIndent(token, "", " ") + if err != nil { + return fmt.Errorf("marshal service account access token: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/ske/cluster/create/create.go b/internal/cmd/ske/cluster/create/create.go index 05c5af178..5a618def2 100644 --- a/internal/cmd/ske/cluster/create/create.go +++ b/internal/cmd/ske/cluster/create/create.go @@ -132,12 +132,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s cluster for project %q. Cluster name: %s\n", operationState, projectLabel, name) + return nil } - p.Outputf("%s cluster for project %q. Cluster name: %s\n", operationState, projectLabel, name) - return nil }, } configureFlags(cmd) @@ -179,3 +184,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie req = req.CreateOrUpdateClusterPayload(*model.Payload) return req } + +func outputResult(p *print.Printer, resp *ske.Cluster) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal SKE cluster: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/ske/cluster/update/update.go b/internal/cmd/ske/cluster/update/update.go index e7551accb..2c747fbda 100644 --- a/internal/cmd/ske/cluster/update/update.go +++ b/internal/cmd/ske/cluster/update/update.go @@ -104,12 +104,17 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - operationState := "Updated" - if model.Async { - operationState = "Triggered update of" + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + operationState := "Updated" + if model.Async { + operationState = "Triggered update of" + } + p.Info("%s cluster %q\n", operationState, model.ClusterName) + return nil } - p.Info("%s cluster %q\n", operationState, model.ClusterName) - return nil }, } configureFlags(cmd) @@ -151,3 +156,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie req = req.CreateOrUpdateClusterPayload(model.Payload) return req } + +func outputResult(p *print.Printer, resp *ske.Cluster) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal SKE cluster: %w", err) + } + p.Outputln(string(details)) + + return nil +} diff --git a/internal/cmd/ske/kubeconfig/create/create.go b/internal/cmd/ske/kubeconfig/create/create.go index 3e8d29e38..e73766367 100644 --- a/internal/cmd/ske/kubeconfig/create/create.go +++ b/internal/cmd/ske/kubeconfig/create/create.go @@ -2,6 +2,7 @@ package create import ( "context" + "encoding/json" "fmt" "github.com/stackitcloud/stackit-cli/internal/pkg/args" @@ -107,9 +108,14 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("write kubeconfig file: %w", err) } - p.Outputf("Created kubeconfig file for cluster %s in %q, with expiration date %v (UTC)\n", model.ClusterName, kubeconfigPath, *resp.ExpirationTimestamp) + switch model.OutputFormat { + case globalflags.JSONOutputFormat: + return outputResult(p, resp) + default: + p.Outputf("Created kubeconfig file for cluster %s in %q, with expiration date %v (UTC)\n", model.ClusterName, kubeconfigPath, *resp.ExpirationTimestamp) - return nil + return nil + } }, } configureFlags(cmd) @@ -161,3 +167,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req.CreateKubeconfigPayload(payload), nil } + +func outputResult(p *print.Printer, resp *ske.Kubeconfig) error { + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal SKE Kubeconfig: %w", err) + } + p.Outputln(string(details)) + + return nil +} From cf8958b8f27e175b1cd70176055eafdb6f826962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 23 Apr 2024 09:35:56 +0200 Subject: [PATCH 4/9] fix linting --- internal/cmd/config/list/list.go | 3 --- internal/cmd/mongodbflex/user/reset-password/reset_password.go | 1 - 2 files changed, 4 deletions(-) diff --git a/internal/cmd/config/list/list.go b/internal/cmd/config/list/list.go index 52c8cdda3..3998b49a0 100644 --- a/internal/cmd/config/list/list.go +++ b/internal/cmd/config/list/list.go @@ -54,7 +54,6 @@ func NewCmd(p *print.Printer) *cobra.Command { return err } return outputResult(p, model.OutputFormat, configData) - }, } return cmd @@ -76,7 +75,6 @@ func outputResult(p *print.Printer, outputFormat string, configData map[string]a return fmt.Errorf("marshal config list: %w", err) } p.Outputln(string(details)) - return nil default: // Sort the config options by key @@ -125,5 +123,4 @@ func outputResult(p *print.Printer, outputFormat string, configData map[string]a } return nil } - } diff --git a/internal/cmd/mongodbflex/user/reset-password/reset_password.go b/internal/cmd/mongodbflex/user/reset-password/reset_password.go index 8d269a010..a08605d17 100644 --- a/internal/cmd/mongodbflex/user/reset-password/reset_password.go +++ b/internal/cmd/mongodbflex/user/reset-password/reset_password.go @@ -96,7 +96,6 @@ func NewCmd(p *print.Printer) *cobra.Command { p.Outputf("New URI: %s\n", *user.Uri) return nil } - }, } From a0c6a0fb037e5c2947b9b3ee7f45b196613d7a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 23 Apr 2024 10:46:40 +0200 Subject: [PATCH 5/9] adapt some parameters in Contribution file --- CONTRIBUTION.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTION.md b/CONTRIBUTION.md index 37e166f19..a896082e6 100644 --- a/CONTRIBUTION.md +++ b/CONTRIBUTION.md @@ -74,7 +74,7 @@ type inputModel struct { } // "bar" command constructor -func NewCmd() *cobra.Command { +func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "bar", Short: "Short description of the command (is shown in the help of parent command)", @@ -88,13 +88,13 @@ func NewCmd() *cobra.Command { ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - model, err := parseInput(cmd, args) + model, err := parseInput(p, cmd, args) if err != nil { return err } // Configure API client - apiClient, err := client.ConfigureClient(cmd) + apiClient, err := client.ConfigureClient(p, cmd) if err != nil { return err } @@ -106,7 +106,7 @@ func NewCmd() *cobra.Command { return fmt.Errorf("(...): %w", err) } - projectLabel, err := projectname.GetProjectName(ctx, cmd) + projectLabel, err := projectname.GetProjectName(ctx, p, cmd) if err != nil { projectLabel = model.ProjectId } @@ -116,7 +116,7 @@ func NewCmd() *cobra.Command { p.Info("(...)", projectLabel) return nil } - return outputResult(cmd, model.OutputFormat, instances) + return outputResult(p, cmd, model.OutputFormat, instances) }, } @@ -152,9 +152,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *foo.APIClie } // Output result based on the configured output format -func outputResult(cmd *cobra.Command, outputFormat string, resources []foo.Resource) error { +func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, resources []foo.Resource) error { switch outputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: details, err := json.MarshalIndent(resources, "", " ") if err != nil { return fmt.Errorf("marshal resource list: %w", err) @@ -177,7 +177,7 @@ func outputResult(cmd *cobra.Command, outputFormat string, resources []foo.Resou } ``` -Please remeber to always add unit tests for `parseInput`, `buildRequest` (in `bar_test.go`), and any other util functions used. +Please remember to always add unit tests for `parseInput`, `buildRequest` (in `bar_test.go`), and any other util functions used. If the new command `bar` is the first command in the CLI using a STACKIT service `foo`, please refer to [Onboarding a new STACKIT service](./CONTRIBUTION.md/#onboarding-a-new-stackit-service). From 29b99ed8806e0c55ecdcc21a2edffb2b0d506805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 23 Apr 2024 10:48:20 +0200 Subject: [PATCH 6/9] change globalflags to print to use json format --- internal/cmd/argus/instance/create/create.go | 2 +- internal/cmd/config/list/list.go | 2 +- internal/cmd/dns/record-set/create/create.go | 2 +- internal/cmd/dns/zone/create/create.go | 2 +- internal/cmd/logme/credentials/create/create.go | 2 +- internal/cmd/logme/instance/create/create.go | 2 +- internal/cmd/mariadb/credentials/create/create.go | 2 +- internal/cmd/mariadb/instance/create/create.go | 2 +- internal/cmd/mongodbflex/instance/create/create.go | 2 +- internal/cmd/mongodbflex/instance/update/update.go | 2 +- internal/cmd/mongodbflex/user/create/create.go | 2 +- internal/cmd/mongodbflex/user/reset-password/reset_password.go | 2 +- internal/cmd/object-storage/bucket/create/create.go | 2 +- internal/cmd/object-storage/credentials-group/create/create.go | 2 +- internal/cmd/object-storage/credentials/create/create.go | 2 +- internal/cmd/opensearch/credentials/create/create.go | 2 +- internal/cmd/opensearch/instance/create/create.go | 2 +- internal/cmd/postgresflex/instance/clone/clone.go | 2 +- internal/cmd/postgresflex/instance/create/create.go | 2 +- internal/cmd/postgresflex/instance/update/update.go | 2 +- internal/cmd/postgresflex/user/create/create.go | 2 +- internal/cmd/postgresflex/user/reset-password/reset_password.go | 2 +- internal/cmd/project/create/create.go | 2 +- internal/cmd/rabbitmq/credentials/create/create.go | 2 +- internal/cmd/rabbitmq/instance/create/create.go | 2 +- internal/cmd/redis/credentials/create/create.go | 2 +- internal/cmd/redis/instance/create/create.go | 2 +- internal/cmd/secrets-manager/instance/create/create.go | 2 +- internal/cmd/secrets-manager/user/create/create.go | 2 +- internal/cmd/service-account/create/create.go | 2 +- internal/cmd/service-account/token/create/create.go | 2 +- internal/cmd/ske/cluster/create/create.go | 2 +- internal/cmd/ske/cluster/update/update.go | 2 +- internal/cmd/ske/kubeconfig/create/create.go | 2 +- 34 files changed, 34 insertions(+), 34 deletions(-) diff --git a/internal/cmd/argus/instance/create/create.go b/internal/cmd/argus/instance/create/create.go index 901f47bfa..5ce82d580 100644 --- a/internal/cmd/argus/instance/create/create.go +++ b/internal/cmd/argus/instance/create/create.go @@ -104,7 +104,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/config/list/list.go b/internal/cmd/config/list/list.go index 3998b49a0..01ad20cc2 100644 --- a/internal/cmd/config/list/list.go +++ b/internal/cmd/config/list/list.go @@ -69,7 +69,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { func outputResult(p *print.Printer, outputFormat string, configData map[string]any) error { switch outputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: details, err := json.MarshalIndent(configData, "", " ") if err != nil { return fmt.Errorf("marshal config list: %w", err) diff --git a/internal/cmd/dns/record-set/create/create.go b/internal/cmd/dns/record-set/create/create.go index 722e03045..95833ab1d 100644 --- a/internal/cmd/dns/record-set/create/create.go +++ b/internal/cmd/dns/record-set/create/create.go @@ -100,7 +100,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/dns/zone/create/create.go b/internal/cmd/dns/zone/create/create.go index 5db759980..3760728ab 100644 --- a/internal/cmd/dns/zone/create/create.go +++ b/internal/cmd/dns/zone/create/create.go @@ -114,7 +114,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/logme/credentials/create/create.go b/internal/cmd/logme/credentials/create/create.go index 885dfa94a..86a818b71 100644 --- a/internal/cmd/logme/credentials/create/create.go +++ b/internal/cmd/logme/credentials/create/create.go @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) diff --git a/internal/cmd/logme/instance/create/create.go b/internal/cmd/logme/instance/create/create.go index 149b5a727..805253a53 100644 --- a/internal/cmd/logme/instance/create/create.go +++ b/internal/cmd/logme/instance/create/create.go @@ -125,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/mariadb/credentials/create/create.go b/internal/cmd/mariadb/credentials/create/create.go index eead95973..8d05a6d2e 100644 --- a/internal/cmd/mariadb/credentials/create/create.go +++ b/internal/cmd/mariadb/credentials/create/create.go @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) diff --git a/internal/cmd/mariadb/instance/create/create.go b/internal/cmd/mariadb/instance/create/create.go index 315384f10..cadbc3c4d 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 { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/mongodbflex/instance/create/create.go b/internal/cmd/mongodbflex/instance/create/create.go index 2ead97102..620ea010e 100644 --- a/internal/cmd/mongodbflex/instance/create/create.go +++ b/internal/cmd/mongodbflex/instance/create/create.go @@ -133,7 +133,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/mongodbflex/instance/update/update.go b/internal/cmd/mongodbflex/instance/update/update.go index 9b44b1572..8e1db0a2d 100644 --- a/internal/cmd/mongodbflex/instance/update/update.go +++ b/internal/cmd/mongodbflex/instance/update/update.go @@ -118,7 +118,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Updated" diff --git a/internal/cmd/mongodbflex/user/create/create.go b/internal/cmd/mongodbflex/user/create/create.go index ccded672e..f0082d3a6 100644 --- a/internal/cmd/mongodbflex/user/create/create.go +++ b/internal/cmd/mongodbflex/user/create/create.go @@ -92,7 +92,7 @@ func NewCmd(p *print.Printer) *cobra.Command { user := resp.Item switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, user) default: p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) diff --git a/internal/cmd/mongodbflex/user/reset-password/reset_password.go b/internal/cmd/mongodbflex/user/reset-password/reset_password.go index a08605d17..0f40ca6c9 100644 --- a/internal/cmd/mongodbflex/user/reset-password/reset_password.go +++ b/internal/cmd/mongodbflex/user/reset-password/reset_password.go @@ -87,7 +87,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, user) default: p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) diff --git a/internal/cmd/object-storage/bucket/create/create.go b/internal/cmd/object-storage/bucket/create/create.go index 4451b445f..6bf69b7d1 100644 --- a/internal/cmd/object-storage/bucket/create/create.go +++ b/internal/cmd/object-storage/bucket/create/create.go @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/object-storage/credentials-group/create/create.go b/internal/cmd/object-storage/credentials-group/create/create.go index 4c2ee9484..fbb57bdde 100644 --- a/internal/cmd/object-storage/credentials-group/create/create.go +++ b/internal/cmd/object-storage/credentials-group/create/create.go @@ -67,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created credentials group %q. Credentials group ID: %s\n\n", *resp.CredentialsGroup.DisplayName, *resp.CredentialsGroup.CredentialsGroupId) diff --git a/internal/cmd/object-storage/credentials/create/create.go b/internal/cmd/object-storage/credentials/create/create.go index 152f2909d..8f4ba0e22 100644 --- a/internal/cmd/object-storage/credentials/create/create.go +++ b/internal/cmd/object-storage/credentials/create/create.go @@ -81,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: expireDate := "Never" diff --git a/internal/cmd/opensearch/credentials/create/create.go b/internal/cmd/opensearch/credentials/create/create.go index 5f99641b7..9757e4d02 100644 --- a/internal/cmd/opensearch/credentials/create/create.go +++ b/internal/cmd/opensearch/credentials/create/create.go @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty diff --git a/internal/cmd/opensearch/instance/create/create.go b/internal/cmd/opensearch/instance/create/create.go index abc60df02..8ec725626 100644 --- a/internal/cmd/opensearch/instance/create/create.go +++ b/internal/cmd/opensearch/instance/create/create.go @@ -127,7 +127,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/postgresflex/instance/clone/clone.go b/internal/cmd/postgresflex/instance/clone/clone.go index d83880ec4..31f56cafb 100644 --- a/internal/cmd/postgresflex/instance/clone/clone.go +++ b/internal/cmd/postgresflex/instance/clone/clone.go @@ -109,7 +109,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Cloned" diff --git a/internal/cmd/postgresflex/instance/create/create.go b/internal/cmd/postgresflex/instance/create/create.go index 10d20118b..f87c1ed00 100644 --- a/internal/cmd/postgresflex/instance/create/create.go +++ b/internal/cmd/postgresflex/instance/create/create.go @@ -133,7 +133,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/postgresflex/instance/update/update.go b/internal/cmd/postgresflex/instance/update/update.go index 9e3dc3167..14c131980 100644 --- a/internal/cmd/postgresflex/instance/update/update.go +++ b/internal/cmd/postgresflex/instance/update/update.go @@ -118,7 +118,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Updated" diff --git a/internal/cmd/postgresflex/user/create/create.go b/internal/cmd/postgresflex/user/create/create.go index e25872f42..db0ddbc0b 100644 --- a/internal/cmd/postgresflex/user/create/create.go +++ b/internal/cmd/postgresflex/user/create/create.go @@ -90,7 +90,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: user := resp.Item diff --git a/internal/cmd/postgresflex/user/reset-password/reset_password.go b/internal/cmd/postgresflex/user/reset-password/reset_password.go index 864453b67..5fedc3b85 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password.go @@ -86,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, user) default: p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) diff --git a/internal/cmd/project/create/create.go b/internal/cmd/project/create/create.go index f7f822ad4..a084c9f22 100644 --- a/internal/cmd/project/create/create.go +++ b/internal/cmd/project/create/create.go @@ -83,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created project under the parent with ID %q. Project ID: %s\n", *model.ParentId, *resp.ProjectId) diff --git a/internal/cmd/rabbitmq/credentials/create/create.go b/internal/cmd/rabbitmq/credentials/create/create.go index fbc64ed2f..d7c07a492 100644 --- a/internal/cmd/rabbitmq/credentials/create/create.go +++ b/internal/cmd/rabbitmq/credentials/create/create.go @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) diff --git a/internal/cmd/rabbitmq/instance/create/create.go b/internal/cmd/rabbitmq/instance/create/create.go index 4153f7278..9613ff162 100644 --- a/internal/cmd/rabbitmq/instance/create/create.go +++ b/internal/cmd/rabbitmq/instance/create/create.go @@ -127,7 +127,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/redis/credentials/create/create.go b/internal/cmd/redis/credentials/create/create.go index 5ce8e2e5c..72d218c27 100644 --- a/internal/cmd/redis/credentials/create/create.go +++ b/internal/cmd/redis/credentials/create/create.go @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) diff --git a/internal/cmd/redis/instance/create/create.go b/internal/cmd/redis/instance/create/create.go index 879b88e00..d70a132a7 100644 --- a/internal/cmd/redis/instance/create/create.go +++ b/internal/cmd/redis/instance/create/create.go @@ -125,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/secrets-manager/instance/create/create.go b/internal/cmd/secrets-manager/instance/create/create.go index 101bd6e97..6942ab3fd 100644 --- a/internal/cmd/secrets-manager/instance/create/create.go +++ b/internal/cmd/secrets-manager/instance/create/create.go @@ -94,7 +94,7 @@ If you want to retry configuring the ACLs, you can do it via: } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created instance for project %q. Instance ID: %s\n", projectLabel, instanceId) diff --git a/internal/cmd/secrets-manager/user/create/create.go b/internal/cmd/secrets-manager/user/create/create.go index 50e55bdd2..b717ba468 100644 --- a/internal/cmd/secrets-manager/user/create/create.go +++ b/internal/cmd/secrets-manager/user/create/create.go @@ -86,7 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *resp.Id) diff --git a/internal/cmd/service-account/create/create.go b/internal/cmd/service-account/create/create.go index 3c39bf72f..08ab7e0ab 100644 --- a/internal/cmd/service-account/create/create.go +++ b/internal/cmd/service-account/create/create.go @@ -73,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created service account for project %q. Email: %s\n", projectLabel, *resp.Email) diff --git a/internal/cmd/service-account/token/create/create.go b/internal/cmd/service-account/token/create/create.go index 683b0fc92..5df870d26 100644 --- a/internal/cmd/service-account/token/create/create.go +++ b/internal/cmd/service-account/token/create/create.go @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, token) default: p.Outputf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, *token.Id) diff --git a/internal/cmd/ske/cluster/create/create.go b/internal/cmd/ske/cluster/create/create.go index 5a618def2..4c51adb0a 100644 --- a/internal/cmd/ske/cluster/create/create.go +++ b/internal/cmd/ske/cluster/create/create.go @@ -133,7 +133,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Created" diff --git a/internal/cmd/ske/cluster/update/update.go b/internal/cmd/ske/cluster/update/update.go index 2c747fbda..6d26c4fbd 100644 --- a/internal/cmd/ske/cluster/update/update.go +++ b/internal/cmd/ske/cluster/update/update.go @@ -105,7 +105,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: operationState := "Updated" diff --git a/internal/cmd/ske/kubeconfig/create/create.go b/internal/cmd/ske/kubeconfig/create/create.go index e73766367..51e7033cb 100644 --- a/internal/cmd/ske/kubeconfig/create/create.go +++ b/internal/cmd/ske/kubeconfig/create/create.go @@ -109,7 +109,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } switch model.OutputFormat { - case globalflags.JSONOutputFormat: + case print.JSONOutputFormat: return outputResult(p, resp) default: p.Outputf("Created kubeconfig file for cluster %s in %q, with expiration date %v (UTC)\n", model.ClusterName, kubeconfigPath, *resp.ExpirationTimestamp) From 9f7b86b8286e7767f61e0b474841585f0c3de7c5 Mon Sep 17 00:00:00 2001 From: GokceGK <161626272+GokceGK@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:36:36 +0200 Subject: [PATCH 7/9] Update internal/cmd/opensearch/credentials/create/create.go Co-authored-by: Vicente Pinto --- internal/cmd/opensearch/credentials/create/create.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/opensearch/credentials/create/create.go b/internal/cmd/opensearch/credentials/create/create.go index 9757e4d02..35f082299 100644 --- a/internal/cmd/opensearch/credentials/create/create.go +++ b/internal/cmd/opensearch/credentials/create/create.go @@ -131,7 +131,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch. func outputResult(p *print.Printer, resp *opensearch.CredentialsResponse) error { details, err := json.MarshalIndent(resp, "", " ") if err != nil { - return fmt.Errorf("marshal OpenSearch credential: %w", err) + return fmt.Errorf("marshal OpenSearch credentials: %w", err) } p.Outputln(string(details)) From 2eeca054d6dda2387d8ed030f776dcf07a518d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 23 Apr 2024 14:45:15 +0200 Subject: [PATCH 8/9] remove redundant error from return values of config list --- internal/cmd/config/list/list.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/cmd/config/list/list.go b/internal/cmd/config/list/list.go index 01ad20cc2..3da8eb0a1 100644 --- a/internal/cmd/config/list/list.go +++ b/internal/cmd/config/list/list.go @@ -49,22 +49,19 @@ func NewCmd(p *print.Printer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { configData := viper.AllSettings() - model, err := parseInput(p, cmd) - if err != nil { - return err - } + model := parseInput(p, cmd) return outputResult(p, model.OutputFormat, configData) }, } return cmd } -func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { +func parseInput(p *print.Printer, cmd *cobra.Command) *inputModel { globalFlags := globalflags.Parse(p, cmd) return &inputModel{ GlobalFlagModel: globalFlags, - }, nil + } } func outputResult(p *print.Printer, outputFormat string, configData map[string]any) error { From 8e00a84d2061bd0c7c5bbf81de8aa613b52b5f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 23 Apr 2024 17:37:09 +0200 Subject: [PATCH 9/9] shift switch case in the outputResult function --- internal/cmd/argus/instance/create/create.go | 36 ++++++------- internal/cmd/dns/record-set/create/create.go | 36 ++++++------- internal/cmd/dns/zone/create/create.go | 36 ++++++------- .../cmd/logme/credentials/create/create.go | 54 +++++++++---------- internal/cmd/logme/instance/create/create.go | 36 ++++++------- .../cmd/mariadb/credentials/create/create.go | 54 +++++++++---------- .../cmd/mariadb/instance/create/create.go | 36 ++++++------- .../cmd/mongodbflex/instance/create/create.go | 36 ++++++------- .../cmd/mongodbflex/instance/update/update.go | 36 ++++++------- .../cmd/mongodbflex/user/create/create.go | 44 +++++++-------- .../user/reset-password/reset_password.go | 34 ++++++------ .../object-storage/bucket/create/create.go | 36 ++++++------- .../credentials-group/create/create.go | 30 +++++------ .../credentials/create/create.go | 46 ++++++++-------- .../opensearch/credentials/create/create.go | 52 +++++++++--------- .../cmd/opensearch/instance/create/create.go | 36 ++++++------- .../cmd/postgresflex/instance/clone/clone.go | 36 ++++++------- .../postgresflex/instance/create/create.go | 36 ++++++------- .../postgresflex/instance/update/update.go | 36 ++++++------- .../cmd/postgresflex/user/create/create.go | 44 +++++++-------- .../user/reset-password/reset_password.go | 34 ++++++------ internal/cmd/project/create/create.go | 28 +++++----- .../cmd/rabbitmq/credentials/create/create.go | 54 +++++++++---------- .../cmd/rabbitmq/instance/create/create.go | 36 ++++++------- .../cmd/redis/credentials/create/create.go | 54 +++++++++---------- internal/cmd/redis/instance/create/create.go | 36 ++++++------- .../secrets-manager/instance/create/create.go | 28 +++++----- .../cmd/secrets-manager/user/create/create.go | 38 ++++++------- internal/cmd/service-account/create/create.go | 28 +++++----- .../service-account/token/create/create.go | 32 +++++------ internal/cmd/ske/cluster/create/create.go | 36 ++++++------- internal/cmd/ske/cluster/update/update.go | 36 ++++++------- internal/cmd/ske/kubeconfig/create/create.go | 30 +++++------ 33 files changed, 630 insertions(+), 630 deletions(-) diff --git a/internal/cmd/argus/instance/create/create.go b/internal/cmd/argus/instance/create/create.go index 5ce82d580..4e614fbb5 100644 --- a/internal/cmd/argus/instance/create/create.go +++ b/internal/cmd/argus/instance/create/create.go @@ -103,17 +103,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, resp) }, } configureFlags(cmd) @@ -197,12 +187,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient argusClient) return req, nil } -func outputResult(p *print.Printer, resp *argus.CreateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal Argus instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *argus.CreateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Argus instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, *resp.InstanceId) + return nil + } } diff --git a/internal/cmd/dns/record-set/create/create.go b/internal/cmd/dns/record-set/create/create.go index 95833ab1d..c7828440e 100644 --- a/internal/cmd/dns/record-set/create/create.go +++ b/internal/cmd/dns/record-set/create/create.go @@ -99,17 +99,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s record set for zone %s. Record set ID: %s\n", operationState, zoneLabel, recordSetId) - return nil - } + return outputResult(p, model, zoneLabel, resp) }, } configureFlags(cmd) @@ -164,12 +154,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie return req } -func outputResult(p *print.Printer, resp *dns.RecordSetResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal DNS record-set: %w", err) +func outputResult(p *print.Printer, model *inputModel, zoneLabel string, resp *dns.RecordSetResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal DNS record-set: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s record set for zone %s. Record set ID: %s\n", operationState, zoneLabel, *resp.Rrset.Id) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/dns/zone/create/create.go b/internal/cmd/dns/zone/create/create.go index 3760728ab..733f6a836 100644 --- a/internal/cmd/dns/zone/create/create.go +++ b/internal/cmd/dns/zone/create/create.go @@ -113,17 +113,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s zone for project %q. Zone ID: %s\n", operationState, projectLabel, zoneId) - return nil - } + return outputResult(p, model, projectLabel, resp) }, } configureFlags(cmd) @@ -193,12 +183,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie return req } -func outputResult(p *print.Printer, resp *dns.ZoneResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal DNS zone: %w", err) +func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *dns.ZoneResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal DNS zone: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s zone for project %q. Zone ID: %s\n", operationState, projectLabel, *resp.Zone.Id) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/logme/credentials/create/create.go b/internal/cmd/logme/credentials/create/create.go index 86a818b71..b01f98b1a 100644 --- a/internal/cmd/logme/credentials/create/create.go +++ b/internal/cmd/logme/credentials/create/create.go @@ -77,26 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create LogMe credentials: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) - // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) - } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } configureFlags(cmd) @@ -129,12 +110,31 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APICl return req } -func outputResult(p *print.Printer, resp *logme.CredentialsResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal LogMe credentials: %w", err) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *logme.CredentialsResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal LogMe credentials: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/logme/instance/create/create.go b/internal/cmd/logme/instance/create/create.go index 805253a53..92caf7e85 100644 --- a/internal/cmd/logme/instance/create/create.go +++ b/internal/cmd/logme/instance/create/create.go @@ -124,17 +124,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, resp) }, } configureFlags(cmd) @@ -249,12 +239,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient logMeClient) return req, nil } -func outputResult(p *print.Printer, resp *logme.CreateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal LogMe instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *logme.CreateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal LogMe instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, *resp.InstanceId) + return nil + } } diff --git a/internal/cmd/mariadb/credentials/create/create.go b/internal/cmd/mariadb/credentials/create/create.go index 8d05a6d2e..838a44380 100644 --- a/internal/cmd/mariadb/credentials/create/create.go +++ b/internal/cmd/mariadb/credentials/create/create.go @@ -77,26 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create MariaDB credentials: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) - // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) - } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } configureFlags(cmd) @@ -129,12 +110,31 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API return req } -func outputResult(p *print.Printer, resp *mariadb.CredentialsResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal MariaDB credentials: %w", err) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *mariadb.CredentialsResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal MariaDB credentials: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/mariadb/instance/create/create.go b/internal/cmd/mariadb/instance/create/create.go index cadbc3c4d..c2e2fd07d 100644 --- a/internal/cmd/mariadb/instance/create/create.go +++ b/internal/cmd/mariadb/instance/create/create.go @@ -124,17 +124,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, resp) }, } configureFlags(cmd) @@ -249,12 +239,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient mariaDBClien return req, nil } -func outputResult(p *print.Printer, resp *mariadb.CreateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal MariaDB instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *mariadb.CreateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal MariaDB instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, *resp.InstanceId) + return nil + } } diff --git a/internal/cmd/mongodbflex/instance/create/create.go b/internal/cmd/mongodbflex/instance/create/create.go index 620ea010e..fbce76df9 100644 --- a/internal/cmd/mongodbflex/instance/create/create.go +++ b/internal/cmd/mongodbflex/instance/create/create.go @@ -132,17 +132,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, resp) }, } configureFlags(cmd) @@ -271,12 +261,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient MongoDBFlexC return req, nil } -func outputResult(p *print.Printer, resp *mongodbflex.CreateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal MongoDBFlex instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *mongodbflex.CreateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal MongoDBFlex instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, *resp.Id) + return nil + } } diff --git a/internal/cmd/mongodbflex/instance/update/update.go b/internal/cmd/mongodbflex/instance/update/update.go index 8e1db0a2d..a559ef2d1 100644 --- a/internal/cmd/mongodbflex/instance/update/update.go +++ b/internal/cmd/mongodbflex/instance/update/update.go @@ -117,17 +117,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Updated" - if model.Async { - operationState = "Triggered update of" - } - p.Info("%s instance %q\n", operationState, instanceLabel) - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } configureFlags(cmd) @@ -305,12 +295,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient MongoDBFlexC return req, nil } -func outputResult(p *print.Printer, resp *mongodbflex.UpdateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal update MongoDBFlex instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *mongodbflex.UpdateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal update MongoDBFlex instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Updated" + if model.Async { + operationState = "Triggered update of" + } + p.Info("%s instance %q\n", operationState, instanceLabel) + return nil + } } diff --git a/internal/cmd/mongodbflex/user/create/create.go b/internal/cmd/mongodbflex/user/create/create.go index f0082d3a6..43b20982b 100644 --- a/internal/cmd/mongodbflex/user/create/create.go +++ b/internal/cmd/mongodbflex/user/create/create.go @@ -91,21 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } user := resp.Item - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, user) - default: - p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) - p.Outputf("Username: %s\n", *user.Username) - p.Outputf("Password: %s\n", *user.Password) - p.Outputf("Roles: %v\n", *user.Roles) - p.Outputf("Database: %s\n", *user.Database) - p.Outputf("Host: %s\n", *user.Host) - p.Outputf("Port: %d\n", *user.Port) - p.Outputf("URI: %s\n", *user.Uri) - - return nil - } + return outputResult(p, model, instanceLabel, user) }, } @@ -150,12 +136,26 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex return req } -func outputResult(p *print.Printer, user *mongodbflex.User) error { - details, err := json.MarshalIndent(user, "", " ") - if err != nil { - return fmt.Errorf("marshal MongoDB Flex user: %w", err) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, user *mongodbflex.User) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(user, "", " ") + if err != nil { + return fmt.Errorf("marshal MongoDB Flex user: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("Password: %s\n", *user.Password) + p.Outputf("Roles: %v\n", *user.Roles) + p.Outputf("Database: %s\n", *user.Database) + p.Outputf("Host: %s\n", *user.Host) + p.Outputf("Port: %d\n", *user.Port) + p.Outputf("URI: %s\n", *user.Uri) + + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/mongodbflex/user/reset-password/reset_password.go b/internal/cmd/mongodbflex/user/reset-password/reset_password.go index 0f40ca6c9..169e10141 100644 --- a/internal/cmd/mongodbflex/user/reset-password/reset_password.go +++ b/internal/cmd/mongodbflex/user/reset-password/reset_password.go @@ -86,16 +86,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("reset MongoDB Flex user password: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, user) - default: - p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) - p.Outputf("Username: %s\n", *user.Username) - p.Outputf("New password: %s\n", *user.Password) - p.Outputf("New URI: %s\n", *user.Uri) - return nil - } + return outputResult(p, model, userLabel, instanceLabel, user) }, } @@ -130,12 +121,21 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex return req } -func outputResult(p *print.Printer, user *mongodbflex.User) error { - details, err := json.MarshalIndent(user, "", " ") - if err != nil { - return fmt.Errorf("marshal MongoDB Flex reset password: %w", err) +func outputResult(p *print.Printer, model *inputModel, userLabel, instanceLabel string, user *mongodbflex.User) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(user, "", " ") + if err != nil { + return fmt.Errorf("marshal MongoDB Flex reset password: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("New password: %s\n", *user.Password) + p.Outputf("New URI: %s\n", *user.Uri) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/object-storage/bucket/create/create.go b/internal/cmd/object-storage/bucket/create/create.go index 6bf69b7d1..ab5836949 100644 --- a/internal/cmd/object-storage/bucket/create/create.go +++ b/internal/cmd/object-storage/bucket/create/create.go @@ -77,17 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s bucket %q\n", operationState, model.BucketName) - return nil - } + return outputResult(p, model, resp) }, } return cmd @@ -112,12 +102,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora return req } -func outputResult(p *print.Printer, resp *objectstorage.CreateBucketResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal Object Storage bucket: %w", err) +func outputResult(p *print.Printer, model *inputModel, resp *objectstorage.CreateBucketResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Object Storage bucket: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s bucket %q\n", operationState, model.BucketName) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/object-storage/credentials-group/create/create.go b/internal/cmd/object-storage/credentials-group/create/create.go index fbb57bdde..267ca7fb0 100644 --- a/internal/cmd/object-storage/credentials-group/create/create.go +++ b/internal/cmd/object-storage/credentials-group/create/create.go @@ -66,14 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Object Storage credentials group: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created credentials group %q. Credentials group ID: %s\n\n", *resp.CredentialsGroup.DisplayName, *resp.CredentialsGroup.CredentialsGroupId) - p.Outputf("URN: %s\n", *resp.CredentialsGroup.Urn) - return nil - } + return outputResult(p, model, resp) }, } configureFlags(cmd) @@ -107,12 +100,19 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora return req } -func outputResult(p *print.Printer, resp *objectstorage.CreateCredentialsGroupResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal Object Storage credentials group: %w", err) +func outputResult(p *print.Printer, model *inputModel, resp *objectstorage.CreateCredentialsGroupResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Object Storage credentials group: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created credentials group %q. Credentials group ID: %s\n\n", *resp.CredentialsGroup.DisplayName, *resp.CredentialsGroup.CredentialsGroupId) + p.Outputf("URN: %s\n", *resp.CredentialsGroup.Urn) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/object-storage/credentials/create/create.go b/internal/cmd/object-storage/credentials/create/create.go index 8f4ba0e22..9f6bd38d3 100644 --- a/internal/cmd/object-storage/credentials/create/create.go +++ b/internal/cmd/object-storage/credentials/create/create.go @@ -80,22 +80,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Object Storage credentials: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - expireDate := "Never" - if resp.Expires != nil && *resp.Expires != "" { - expireDate = *resp.Expires - } - - p.Outputf("Created credentials in group %q. Credentials ID: %s\n\n", credentialsGroupLabel, *resp.KeyId) - p.Outputf("Access Key ID: %s\n", *resp.AccessKey) - p.Outputf("Secret Access Key: %s\n", *resp.SecretAccessKey) - p.Outputf("Expire Date: %s\n", expireDate) - - return nil - } + return outputResult(p, model, credentialsGroupLabel, resp) }, } configureFlags(cmd) @@ -140,12 +125,27 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora return req } -func outputResult(p *print.Printer, resp *objectstorage.CreateAccessKeyResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal Object Storage credentials: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, credentialsGroupLabel string, resp *objectstorage.CreateAccessKeyResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Object Storage credentials: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + expireDate := "Never" + if resp.Expires != nil && *resp.Expires != "" { + expireDate = *resp.Expires + } - return nil + p.Outputf("Created credentials in group %q. Credentials ID: %s\n\n", credentialsGroupLabel, *resp.KeyId) + p.Outputf("Access Key ID: %s\n", *resp.AccessKey) + p.Outputf("Secret Access Key: %s\n", *resp.SecretAccessKey) + p.Outputf("Expire Date: %s\n", expireDate) + + return nil + } } diff --git a/internal/cmd/opensearch/credentials/create/create.go b/internal/cmd/opensearch/credentials/create/create.go index 35f082299..56c2f1d4d 100644 --- a/internal/cmd/opensearch/credentials/create/create.go +++ b/internal/cmd/opensearch/credentials/create/create.go @@ -77,25 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create OpenSearch credentials: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) - } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } configureFlags(cmd) @@ -128,12 +110,30 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch. return req } -func outputResult(p *print.Printer, resp *opensearch.CredentialsResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal OpenSearch credentials: %w", err) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *opensearch.CredentialsResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal OpenSearch credentials: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/opensearch/instance/create/create.go b/internal/cmd/opensearch/instance/create/create.go index 8ec725626..6dac5124b 100644 --- a/internal/cmd/opensearch/instance/create/create.go +++ b/internal/cmd/opensearch/instance/create/create.go @@ -126,17 +126,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, instanceId, resp) }, } configureFlags(cmd) @@ -254,12 +244,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient openSearchCl return req, nil } -func outputResult(p *print.Printer, resp *opensearch.CreateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal OpenSearch instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *opensearch.CreateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal OpenSearch instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil + } } diff --git a/internal/cmd/postgresflex/instance/clone/clone.go b/internal/cmd/postgresflex/instance/clone/clone.go index 31f56cafb..01fc2fee8 100644 --- a/internal/cmd/postgresflex/instance/clone/clone.go +++ b/internal/cmd/postgresflex/instance/clone/clone.go @@ -108,17 +108,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Cloned" - if model.Async { - operationState = "Triggered cloning of" - } - p.Info("%s instance from instance %q. New Instance ID: %s\n", operationState, instanceLabel, instanceId) - return nil - } + return outputResult(p, model, instanceLabel, instanceId, resp) }, } configureFlags(cmd) @@ -204,12 +194,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient PostgreSQLFl return req, nil } -func outputResult(p *print.Printer, resp *postgresflex.CloneInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal PostgresFlex instance clone: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, instanceLabel, instanceId string, resp *postgresflex.CloneInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex instance clone: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Cloned" + if model.Async { + operationState = "Triggered cloning of" + } + p.Info("%s instance from instance %q. New Instance ID: %s\n", operationState, instanceLabel, instanceId) + return nil + } } diff --git a/internal/cmd/postgresflex/instance/create/create.go b/internal/cmd/postgresflex/instance/create/create.go index f87c1ed00..b36651921 100644 --- a/internal/cmd/postgresflex/instance/create/create.go +++ b/internal/cmd/postgresflex/instance/create/create.go @@ -132,17 +132,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, instanceId, resp) }, } configureFlags(cmd) @@ -271,12 +261,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient PostgreSQLFl return req, nil } -func outputResult(p *print.Printer, resp *postgresflex.CreateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal PostgresFlex instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *postgresflex.CreateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil + } } diff --git a/internal/cmd/postgresflex/instance/update/update.go b/internal/cmd/postgresflex/instance/update/update.go index 14c131980..30fabc04b 100644 --- a/internal/cmd/postgresflex/instance/update/update.go +++ b/internal/cmd/postgresflex/instance/update/update.go @@ -117,17 +117,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Updated" - if model.Async { - operationState = "Triggered update of" - } - p.Info("%s instance %q\n", operationState, instanceLabel) - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } configureFlags(cmd) @@ -305,12 +295,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient PostgreSQLFl return req, nil } -func outputResult(p *print.Printer, resp *postgresflex.PartialUpdateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal PostgresFlex instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *postgresflex.PartialUpdateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Updated" + if model.Async { + operationState = "Triggered update of" + } + p.Info("%s instance %q\n", operationState, instanceLabel) + return nil + } } diff --git a/internal/cmd/postgresflex/user/create/create.go b/internal/cmd/postgresflex/user/create/create.go index db0ddbc0b..ca6b3d589 100644 --- a/internal/cmd/postgresflex/user/create/create.go +++ b/internal/cmd/postgresflex/user/create/create.go @@ -89,21 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create PostgreSQL Flex user: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - user := resp.Item - p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) - p.Outputf("Username: %s\n", *user.Username) - p.Outputf("Password: %s\n", *user.Password) - p.Outputf("Roles: %v\n", *user.Roles) - p.Outputf("Host: %s\n", *user.Host) - p.Outputf("Port: %d\n", *user.Port) - p.Outputf("URI: %s\n", *user.Uri) - - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } @@ -145,12 +131,26 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle return req } -func outputResult(p *print.Printer, resp *postgresflex.CreateUserResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal PostgresFlex user: %w", err) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *postgresflex.CreateUserResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex user: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + user := resp.Item + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *user.Id) + p.Outputf("Username: %s\n", *user.Username) + p.Outputf("Password: %s\n", *user.Password) + p.Outputf("Roles: %v\n", *user.Roles) + p.Outputf("Host: %s\n", *user.Host) + p.Outputf("Port: %d\n", *user.Port) + p.Outputf("URI: %s\n", *user.Uri) + + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/postgresflex/user/reset-password/reset_password.go b/internal/cmd/postgresflex/user/reset-password/reset_password.go index 5fedc3b85..f69b409ed 100644 --- a/internal/cmd/postgresflex/user/reset-password/reset_password.go +++ b/internal/cmd/postgresflex/user/reset-password/reset_password.go @@ -85,16 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("reset PostgreSQL Flex user password: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, user) - default: - p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) - p.Outputf("Username: %s\n", *user.Item.Username) - p.Outputf("New password: %s\n", *user.Item.Password) - p.Outputf("New URI: %s\n", *user.Item.Uri) - return nil - } + return outputResult(p, model, userLabel, instanceLabel, user) }, } @@ -129,12 +120,21 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle return req } -func outputResult(p *print.Printer, resp *postgresflex.ResetUserResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal PostgresFlex user: %w", err) +func outputResult(p *print.Printer, model *inputModel, userLabel, instanceLabel string, user *postgresflex.ResetUserResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(user, "", " ") + if err != nil { + return fmt.Errorf("marshal PostgresFlex user: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Reset password for user %q of instance %q\n\n", userLabel, instanceLabel) + p.Outputf("Username: %s\n", *user.Item.Username) + p.Outputf("New password: %s\n", *user.Item.Password) + p.Outputf("New URI: %s\n", *user.Item.Uri) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/project/create/create.go b/internal/cmd/project/create/create.go index a084c9f22..8134ceb23 100644 --- a/internal/cmd/project/create/create.go +++ b/internal/cmd/project/create/create.go @@ -82,13 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create project: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created project under the parent with ID %q. Project ID: %s\n", *model.ParentId, *resp.ProjectId) - return nil - } + return outputResult(p, model, resp) }, } configureFlags(cmd) @@ -183,12 +177,18 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *resourceman return req, nil } -func outputResult(p *print.Printer, resp *resourcemanager.ProjectResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal project: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, resp *resourcemanager.ProjectResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal project: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + p.Outputf("Created project under the parent with ID %q. Project ID: %s\n", *model.ParentId, *resp.ProjectId) + return nil + } } diff --git a/internal/cmd/rabbitmq/credentials/create/create.go b/internal/cmd/rabbitmq/credentials/create/create.go index d7c07a492..95679181b 100644 --- a/internal/cmd/rabbitmq/credentials/create/create.go +++ b/internal/cmd/rabbitmq/credentials/create/create.go @@ -77,26 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create RabbitMQ credentials: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) - // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) - } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } configureFlags(cmd) @@ -129,12 +110,31 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP return req } -func outputResult(p *print.Printer, resp *rabbitmq.CredentialsResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal RabbitMQ credentials: %w", err) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *rabbitmq.CredentialsResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal RabbitMQ credentials: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/rabbitmq/instance/create/create.go b/internal/cmd/rabbitmq/instance/create/create.go index 9613ff162..da5c6de2c 100644 --- a/internal/cmd/rabbitmq/instance/create/create.go +++ b/internal/cmd/rabbitmq/instance/create/create.go @@ -126,17 +126,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, instanceId, resp) }, } configureFlags(cmd) @@ -254,12 +244,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient rabbitMQClie return req, nil } -func outputResult(p *print.Printer, resp *rabbitmq.CreateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal RabbitMQ instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *rabbitmq.CreateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal RabbitMQ instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil + } } diff --git a/internal/cmd/redis/credentials/create/create.go b/internal/cmd/redis/credentials/create/create.go index 72d218c27..43cf118f7 100644 --- a/internal/cmd/redis/credentials/create/create.go +++ b/internal/cmd/redis/credentials/create/create.go @@ -77,26 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Redis credentials: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) - // The username field cannot be set by the user so we only display it if it's not returned empty - username := *resp.Raw.Credentials.Username - if username != "" { - p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) - } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) - } - p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) - p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) - p.Outputf("URI: %s\n", *resp.Uri) - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } configureFlags(cmd) @@ -129,12 +110,31 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *redis.APICl return req } -func outputResult(p *print.Printer, resp *redis.CredentialsResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal Redis credentials: %w", err) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *redis.CredentialsResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Redis credentials: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, *resp.Id) + // The username field cannot be set by the user so we only display it if it's not returned empty + username := *resp.Raw.Credentials.Username + if username != "" { + p.Outputf("Username: %s\n", *resp.Raw.Credentials.Username) + } + if model.HidePassword { + p.Outputf("Password: \n") + } else { + p.Outputf("Password: %s\n", *resp.Raw.Credentials.Password) + } + p.Outputf("Host: %s\n", *resp.Raw.Credentials.Host) + p.Outputf("Port: %d\n", *resp.Raw.Credentials.Port) + p.Outputf("URI: %s\n", *resp.Uri) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/redis/instance/create/create.go b/internal/cmd/redis/instance/create/create.go index d70a132a7..1aae7686e 100644 --- a/internal/cmd/redis/instance/create/create.go +++ b/internal/cmd/redis/instance/create/create.go @@ -124,17 +124,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, instanceId, resp) }, } configureFlags(cmd) @@ -249,12 +239,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient redisClient) return req, nil } -func outputResult(p *print.Printer, resp *redis.CreateInstanceResponse) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal Redis instance: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *redis.CreateInstanceResponse) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Redis instance: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId) + return nil + } } diff --git a/internal/cmd/secrets-manager/instance/create/create.go b/internal/cmd/secrets-manager/instance/create/create.go index 6942ab3fd..7b8c4466a 100644 --- a/internal/cmd/secrets-manager/instance/create/create.go +++ b/internal/cmd/secrets-manager/instance/create/create.go @@ -93,13 +93,7 @@ If you want to retry configuring the ACLs, you can do it via: } } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created instance for project %q. Instance ID: %s\n", projectLabel, instanceId) - return nil - } + return outputResult(p, model, projectLabel, instanceId, resp) }, } configureFlags(cmd) @@ -151,12 +145,18 @@ func buildUpdateACLsRequest(ctx context.Context, model *inputModel, instanceId s return req } -func outputResult(p *print.Printer, resp *secretsmanager.Instance) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal Secrets Manager instance: %w", err) +func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *secretsmanager.Instance) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Secrets Manager instance: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created instance for project %q. Instance ID: %s\n", projectLabel, instanceId) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/secrets-manager/user/create/create.go b/internal/cmd/secrets-manager/user/create/create.go index b717ba468..da9ce9bf6 100644 --- a/internal/cmd/secrets-manager/user/create/create.go +++ b/internal/cmd/secrets-manager/user/create/create.go @@ -85,18 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create Secrets Manager user: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *resp.Id) - p.Outputf("Username: %s\n", *resp.Username) - p.Outputf("Password: %s\n", *resp.Password) - p.Outputf("Description: %s\n", *resp.Description) - p.Outputf("Write Access: %t\n", *resp.Write) - - return nil - } + return outputResult(p, model, instanceLabel, resp) }, } @@ -136,12 +125,23 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmana return req } -func outputResult(p *print.Printer, resp *secretsmanager.User) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal Secrets Manager user: %w", err) +func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *secretsmanager.User) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal Secrets Manager user: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *resp.Id) + p.Outputf("Username: %s\n", *resp.Username) + p.Outputf("Password: %s\n", *resp.Password) + p.Outputf("Description: %s\n", *resp.Description) + p.Outputf("Write Access: %t\n", *resp.Write) + + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/service-account/create/create.go b/internal/cmd/service-account/create/create.go index 08ab7e0ab..ee892103b 100644 --- a/internal/cmd/service-account/create/create.go +++ b/internal/cmd/service-account/create/create.go @@ -72,13 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create service account: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created service account for project %q. Email: %s\n", projectLabel, *resp.Email) - return nil - } + return outputResult(p, model, projectLabel, resp) }, } configureFlags(cmd) @@ -112,12 +106,18 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco return req } -func outputResult(p *print.Printer, serviceAccount *serviceaccount.ServiceAccount) error { - details, err := json.MarshalIndent(serviceAccount, "", " ") - if err != nil { - return fmt.Errorf("marshal service account: %w", err) +func outputResult(p *print.Printer, model *inputModel, projectLabel string, serviceAccount *serviceaccount.ServiceAccount) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(serviceAccount, "", " ") + if err != nil { + return fmt.Errorf("marshal service account: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created service account for project %q. Email: %s\n", projectLabel, *serviceAccount.Email) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/service-account/token/create/create.go b/internal/cmd/service-account/token/create/create.go index 5df870d26..8e9881968 100644 --- a/internal/cmd/service-account/token/create/create.go +++ b/internal/cmd/service-account/token/create/create.go @@ -77,15 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("create access token: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, token) - default: - p.Outputf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, *token.Id) - p.Outputf("Valid until: %s\n", *token.ValidUntil) - p.Outputf("Token: %s\n", *token.Token) - return nil - } + return outputResult(p, model, token) }, } @@ -138,12 +130,20 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco return req } -func outputResult(p *print.Printer, token *serviceaccount.AccessToken) error { - details, err := json.MarshalIndent(token, "", " ") - if err != nil { - return fmt.Errorf("marshal service account access token: %w", err) +func outputResult(p *print.Printer, model *inputModel, token *serviceaccount.AccessToken) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(token, "", " ") + if err != nil { + return fmt.Errorf("marshal service account access token: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, *token.Id) + p.Outputf("Valid until: %s\n", *token.ValidUntil) + p.Outputf("Token: %s\n", *token.Token) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/ske/cluster/create/create.go b/internal/cmd/ske/cluster/create/create.go index 4c51adb0a..3eafb7099 100644 --- a/internal/cmd/ske/cluster/create/create.go +++ b/internal/cmd/ske/cluster/create/create.go @@ -132,17 +132,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Created" - if model.Async { - operationState = "Triggered creation of" - } - p.Outputf("%s cluster for project %q. Cluster name: %s\n", operationState, projectLabel, name) - return nil - } + return outputResult(p, model, projectLabel, resp) }, } configureFlags(cmd) @@ -185,12 +175,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req } -func outputResult(p *print.Printer, resp *ske.Cluster) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal SKE cluster: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *ske.Cluster) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal SKE cluster: %w", err) + } + p.Outputln(string(details)) - return nil + return nil + default: + operationState := "Created" + if model.Async { + operationState = "Triggered creation of" + } + p.Outputf("%s cluster for project %q. Cluster name: %s\n", operationState, projectLabel, *resp.Name) + return nil + } } diff --git a/internal/cmd/ske/cluster/update/update.go b/internal/cmd/ske/cluster/update/update.go index 6d26c4fbd..9c477c8e2 100644 --- a/internal/cmd/ske/cluster/update/update.go +++ b/internal/cmd/ske/cluster/update/update.go @@ -104,17 +104,7 @@ func NewCmd(p *print.Printer) *cobra.Command { s.Stop() } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - operationState := "Updated" - if model.Async { - operationState = "Triggered update of" - } - p.Info("%s cluster %q\n", operationState, model.ClusterName) - return nil - } + return outputResult(p, model, resp) }, } configureFlags(cmd) @@ -157,12 +147,22 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req } -func outputResult(p *print.Printer, resp *ske.Cluster) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal SKE cluster: %w", err) +func outputResult(p *print.Printer, model *inputModel, resp *ske.Cluster) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal SKE cluster: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + operationState := "Updated" + if model.Async { + operationState = "Triggered update of" + } + p.Info("%s cluster %q\n", operationState, model.ClusterName) + return nil } - p.Outputln(string(details)) - - return nil } diff --git a/internal/cmd/ske/kubeconfig/create/create.go b/internal/cmd/ske/kubeconfig/create/create.go index 51e7033cb..322f1a853 100644 --- a/internal/cmd/ske/kubeconfig/create/create.go +++ b/internal/cmd/ske/kubeconfig/create/create.go @@ -108,14 +108,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("write kubeconfig file: %w", err) } - switch model.OutputFormat { - case print.JSONOutputFormat: - return outputResult(p, resp) - default: - p.Outputf("Created kubeconfig file for cluster %s in %q, with expiration date %v (UTC)\n", model.ClusterName, kubeconfigPath, *resp.ExpirationTimestamp) - - return nil - } + return outputResult(p, model, kubeconfigPath, resp) }, } configureFlags(cmd) @@ -168,12 +161,19 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie return req.CreateKubeconfigPayload(payload), nil } -func outputResult(p *print.Printer, resp *ske.Kubeconfig) error { - details, err := json.MarshalIndent(resp, "", " ") - if err != nil { - return fmt.Errorf("marshal SKE Kubeconfig: %w", err) - } - p.Outputln(string(details)) +func outputResult(p *print.Printer, model *inputModel, kubeconfigPath string, resp *ske.Kubeconfig) error { + switch model.OutputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(resp, "", " ") + if err != nil { + return fmt.Errorf("marshal SKE Kubeconfig: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + p.Outputf("Created kubeconfig file for cluster %s in %q, with expiration date %v (UTC)\n", model.ClusterName, kubeconfigPath, *resp.ExpirationTimestamp) - return nil + return nil + } }