From c9ff8d23242a92f7b232eb5f0bd8a9d2c7e5f365 Mon Sep 17 00:00:00 2001 From: Maxim Kondratenko Date: Thu, 9 Jan 2025 19:02:03 +0200 Subject: [PATCH 1/5] EVEREST-1778 Ask user provide namespace name if it is missed in `everestctl namespaces add` command --- commands/namespaces/add.go | 27 +++++++++++++++++++++++---- pkg/cli/namespaces/add.go | 2 ++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/commands/namespaces/add.go b/commands/namespaces/add.go index 5da81eb7a..be31c94af 100644 --- a/commands/namespaces/add.go +++ b/commands/namespaces/add.go @@ -6,6 +6,7 @@ import ( "fmt" "os" + "github.com/AlecAivazis/survey/v2" "github.com/spf13/cobra" "github.com/spf13/viper" "go.uber.org/zap" @@ -28,7 +29,7 @@ func NewAddCommand(l *zap.SugaredLogger) *cobra.Command { Use: "add", Long: "Add a new namespace", Short: "Add a new namespace", - Example: `everestctl namespaces add [NAMESPACE] [FLAGS]`, + Example: `everestctl namespaces add ns-1 --operator.mongodb=true --operator.postgresql=true --operator.xtradb-cluster=true --skip-wizard`, Run: func(cmd *cobra.Command, args []string) { initAddViperFlags(cmd) c := &namespaces.NamespaceAddConfig{} @@ -39,11 +40,29 @@ func NewAddCommand(l *zap.SugaredLogger) *cobra.Command { } bindInstallHelmOpts(c) - if len(args) != 1 { - output.PrintError(fmt.Errorf("invalid number of arguments: expected 1, got %d", len(args)), l, true) + if len(args) == 1 { + // user provided namespace name + c.Namespaces = args[0] + } else if c.SkipWizard { + // user didn't provide namespace name, but we can't ask him to provide it + output.PrintError(namespaces.ErrNamespaceIsMissed, l, true) os.Exit(1) + } else { + // ask user to provide namespace name + var resp string + q := &survey.Input{Message: "Enter namespace name"} + if err = survey.AskOne(q, &resp); err != nil { + output.PrintError(err, l, true) + os.Exit(1) + } + + if resp == "" { + // user didn't provide namespace name + output.PrintError(namespaces.ErrNamespaceIsMissed, l, true) + os.Exit(1) + } + c.Namespaces = resp } - c.Namespaces = args[0] enableLogging := viper.GetBool("verbose") || viper.GetBool("json") c.Pretty = !enableLogging diff --git a/pkg/cli/namespaces/add.go b/pkg/cli/namespaces/add.go index 5304f5d47..e330f3cd4 100644 --- a/pkg/cli/namespaces/add.go +++ b/pkg/cli/namespaces/add.go @@ -197,6 +197,8 @@ var ( ErrNamespaceAlreadyExists = errors.New("namespace already exists") // ErrNamespaceAlreadyOwned appears when the namespace is already owned by Everest. ErrNamespaceAlreadyOwned = errors.New("namespace already exists and is managed by Everest") + // ErrNamespaceIsMissed appears when the namespace name is missed. + ErrNamespaceIsMissed = errors.New("namespace name is mandatory") ) func (cfg *NamespaceAddConfig) validateNamespaceOwnership( From 527221571260ffe12214ca18c6371ab384e9023b Mon Sep 17 00:00:00 2001 From: Maxim Kondratenko Date: Fri, 10 Jan 2025 13:16:49 +0200 Subject: [PATCH 2/5] Revert "EVEREST-1778 Ask user provide namespace name if it is missed in `everestctl namespaces add` command" This reverts commit b76fe75b475886569edd0f0726e6c3c4a6a071d1. --- commands/namespaces/add.go | 27 ++++----------------------- pkg/cli/namespaces/add.go | 2 -- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/commands/namespaces/add.go b/commands/namespaces/add.go index be31c94af..5da81eb7a 100644 --- a/commands/namespaces/add.go +++ b/commands/namespaces/add.go @@ -6,7 +6,6 @@ import ( "fmt" "os" - "github.com/AlecAivazis/survey/v2" "github.com/spf13/cobra" "github.com/spf13/viper" "go.uber.org/zap" @@ -29,7 +28,7 @@ func NewAddCommand(l *zap.SugaredLogger) *cobra.Command { Use: "add", Long: "Add a new namespace", Short: "Add a new namespace", - Example: `everestctl namespaces add ns-1 --operator.mongodb=true --operator.postgresql=true --operator.xtradb-cluster=true --skip-wizard`, + Example: `everestctl namespaces add [NAMESPACE] [FLAGS]`, Run: func(cmd *cobra.Command, args []string) { initAddViperFlags(cmd) c := &namespaces.NamespaceAddConfig{} @@ -40,29 +39,11 @@ func NewAddCommand(l *zap.SugaredLogger) *cobra.Command { } bindInstallHelmOpts(c) - if len(args) == 1 { - // user provided namespace name - c.Namespaces = args[0] - } else if c.SkipWizard { - // user didn't provide namespace name, but we can't ask him to provide it - output.PrintError(namespaces.ErrNamespaceIsMissed, l, true) + if len(args) != 1 { + output.PrintError(fmt.Errorf("invalid number of arguments: expected 1, got %d", len(args)), l, true) os.Exit(1) - } else { - // ask user to provide namespace name - var resp string - q := &survey.Input{Message: "Enter namespace name"} - if err = survey.AskOne(q, &resp); err != nil { - output.PrintError(err, l, true) - os.Exit(1) - } - - if resp == "" { - // user didn't provide namespace name - output.PrintError(namespaces.ErrNamespaceIsMissed, l, true) - os.Exit(1) - } - c.Namespaces = resp } + c.Namespaces = args[0] enableLogging := viper.GetBool("verbose") || viper.GetBool("json") c.Pretty = !enableLogging diff --git a/pkg/cli/namespaces/add.go b/pkg/cli/namespaces/add.go index e330f3cd4..5304f5d47 100644 --- a/pkg/cli/namespaces/add.go +++ b/pkg/cli/namespaces/add.go @@ -197,8 +197,6 @@ var ( ErrNamespaceAlreadyExists = errors.New("namespace already exists") // ErrNamespaceAlreadyOwned appears when the namespace is already owned by Everest. ErrNamespaceAlreadyOwned = errors.New("namespace already exists and is managed by Everest") - // ErrNamespaceIsMissed appears when the namespace name is missed. - ErrNamespaceIsMissed = errors.New("namespace name is mandatory") ) func (cfg *NamespaceAddConfig) validateNamespaceOwnership( From cbcc4e898feb009406bd865395e34b81c4f9a4d9 Mon Sep 17 00:00:00 2001 From: Maxim Kondratenko Date: Fri, 10 Jan 2025 14:00:44 +0200 Subject: [PATCH 3/5] EVEREST-1778 Extend help message for CLI `namespace add` command --- commands/namespaces/add.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/commands/namespaces/add.go b/commands/namespaces/add.go index 5da81eb7a..dedfe6833 100644 --- a/commands/namespaces/add.go +++ b/commands/namespaces/add.go @@ -25,10 +25,11 @@ var ( // NewAddCommand returns a new command to add a new namespace. func NewAddCommand(l *zap.SugaredLogger) *cobra.Command { cmd := &cobra.Command{ - Use: "add", + Use: "add [flags] namespace", Long: "Add a new namespace", Short: "Add a new namespace", - Example: `everestctl namespaces add [NAMESPACE] [FLAGS]`, + Example: `everestctl namespaces add --operator.mongodb=true --skip-wizard ns-1,ns-2`, + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { initAddViperFlags(cmd) c := &namespaces.NamespaceAddConfig{} @@ -39,12 +40,7 @@ func NewAddCommand(l *zap.SugaredLogger) *cobra.Command { } bindInstallHelmOpts(c) - if len(args) != 1 { - output.PrintError(fmt.Errorf("invalid number of arguments: expected 1, got %d", len(args)), l, true) - os.Exit(1) - } c.Namespaces = args[0] - enableLogging := viper.GetBool("verbose") || viper.GetBool("json") c.Pretty = !enableLogging From c4c9354387c62d99552c3affb7fe7267284f28fb Mon Sep 17 00:00:00 2001 From: Maxim Kondratenko Date: Mon, 13 Jan 2025 11:37:01 +0200 Subject: [PATCH 4/5] EVEREST-1778 Extend example for `namespaces add` CLI command --- commands/namespaces/add.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/namespaces/add.go b/commands/namespaces/add.go index dedfe6833..17b2bac4a 100644 --- a/commands/namespaces/add.go +++ b/commands/namespaces/add.go @@ -28,7 +28,7 @@ func NewAddCommand(l *zap.SugaredLogger) *cobra.Command { Use: "add [flags] namespace", Long: "Add a new namespace", Short: "Add a new namespace", - Example: `everestctl namespaces add --operator.mongodb=true --skip-wizard ns-1,ns-2`, + Example: `everestctl namespaces add --operator.mongodb=true --operator.postgresql=false --operator.xtradb-cluster=false --skip-wizard ns-1,ns-2`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { initAddViperFlags(cmd) From 9f7e0118f9a199fee3e02036a0431ca354401771 Mon Sep 17 00:00:00 2001 From: Maxim Kondratenko Date: Mon, 13 Jan 2025 13:32:07 +0200 Subject: [PATCH 5/5] Update commands/namespaces/add.go --- commands/namespaces/add.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/namespaces/add.go b/commands/namespaces/add.go index 17b2bac4a..16739e839 100644 --- a/commands/namespaces/add.go +++ b/commands/namespaces/add.go @@ -25,7 +25,7 @@ var ( // NewAddCommand returns a new command to add a new namespace. func NewAddCommand(l *zap.SugaredLogger) *cobra.Command { cmd := &cobra.Command{ - Use: "add [flags] namespace", + Use: "add [flags] NAMESPACES", Long: "Add a new namespace", Short: "Add a new namespace", Example: `everestctl namespaces add --operator.mongodb=true --operator.postgresql=false --operator.xtradb-cluster=false --skip-wizard ns-1,ns-2`,