From 980bee0c7f6845930009c9811cf60dd69c0998fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Mon, 5 Feb 2024 16:32:05 +0000 Subject: [PATCH] Refactor DSA input plan error --- .../cmd/opensearch/instance/create/create.go | 9 ++----- .../cmd/opensearch/instance/update/update.go | 6 ++--- internal/pkg/errors/errors.go | 25 ++++++++++++------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/internal/cmd/opensearch/instance/create/create.go b/internal/cmd/opensearch/instance/create/create.go index a620f2c8e..c62218585 100644 --- a/internal/cmd/opensearch/instance/create/create.go +++ b/internal/cmd/opensearch/instance/create/create.go @@ -155,9 +155,6 @@ func configureFlags(cmd *cobra.Command) { } func parseInput(cmd *cobra.Command) (*inputModel, error) { - service := "opensearch" - operation := cmd.Use - globalFlags := globalflags.Parse(cmd) if globalFlags.ProjectId == "" { return nil, &cliErr.ProjectIdError{} @@ -169,14 +166,12 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) { if planId == nil && (planName == "" || version == "") { return nil, &cliErr.DSAInputPlanError{ - Service: service, - Operation: operation, + Cmd: cmd, } } if planId != nil && (planName != "" || version != "") { return nil, &cliErr.DSAInputPlanError{ - Service: service, - Operation: operation, + Cmd: cmd, } } diff --git a/internal/cmd/opensearch/instance/update/update.go b/internal/cmd/opensearch/instance/update/update.go index 31ada83f6..c5f96e800 100644 --- a/internal/cmd/opensearch/instance/update/update.go +++ b/internal/cmd/opensearch/instance/update/update.go @@ -149,8 +149,6 @@ func configureFlags(cmd *cobra.Command) { } func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { - service := "opensearch" - operation := cmd.Use instanceId := inputArgs[0] globalFlags := globalflags.Parse(cmd) @@ -172,8 +170,8 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { if planId != nil && (planName != "" || version != "") { return nil, &cliErr.DSAInputPlanError{ - Service: service, - Operation: operation, + Cmd: cmd, + Args: inputArgs, } } diff --git a/internal/pkg/errors/errors.go b/internal/pkg/errors/errors.go index aa8710995..153e5c992 100644 --- a/internal/pkg/errors/errors.go +++ b/internal/pkg/errors/errors.go @@ -2,6 +2,7 @@ package errors import ( "fmt" + "strings" "github.com/spf13/cobra" ) @@ -37,14 +38,14 @@ For more details run: DSA_INVALID_INPUT_PLAN = `the instance plan was not correctly provided. -Either provide plan-id by running: - $ stackit %[1]s instance %[2]s --project-id xxx --name my-instance --plan-id +Either provide the plan ID: + $ %[1]s --plan-id [flags] -or provide plan-name and version: - $ stackit %[1]s instance %[2]s --project-id xxx --name my-instance --plan-name --version +or provide plan name and version: + $ %[1]s --plan-name --version [flags] For more details on the available plans, run: - $ stackit %[1]s plans` + $ stackit %[2]s plans` DSA_INVALID_PLAN = `the provided instance plan is not valid. @@ -55,7 +56,7 @@ For more details on the available plans, run: DATABASE_INVALID_INPUT_FLAVOR = `the instance flavor was not correctly provided. -Either provide flavor-id by running: +Either provide flavor ID by: $ stackit %[1]s instance %[2]s --project-id xxx --flavor-id [flags] or provide CPU and RAM: @@ -121,12 +122,18 @@ func (e *ActivateServiceAccountError) Error() string { } type DSAInputPlanError struct { - Service string - Operation string + Cmd *cobra.Command + Args []string } func (e *DSAInputPlanError) Error() string { - return fmt.Sprintf(DSA_INVALID_INPUT_PLAN, e.Service, e.Operation) + fullCommandPath := e.Cmd.CommandPath() + if len(e.Args) > 0 { + fullCommandPath = fmt.Sprintf("%s %s", fullCommandPath, strings.Join(e.Args, " ")) + } + service := e.Cmd.Parent().Parent().Use + + return fmt.Sprintf(DSA_INVALID_INPUT_PLAN, fullCommandPath, service) } type DSAInvalidPlanError struct {