From 8d97d5d7eef9dd0c5e0750755859ea91354830a0 Mon Sep 17 00:00:00 2001 From: "Yasin, Siraj (SY495P)" Date: Fri, 21 Feb 2020 13:38:06 +0000 Subject: [PATCH] [#57] - Added --current flag to set-context * Added --current flag to set-context command to use current-context when context not provided * Added validation to throw error when both context and --current flag * Updated golden files to match the recent change given * Added validation to check if current context is set when --current flag is provided Old Format: airshipctl config set-context default --manifest default --namespace default New Format: airshipctl config set-context --current --manifest default --namespace default Change-Id: I9ffe3a34f33ffd7ff03ca42de7609f91f5386b37 --- cmd/config/set_context.go | 23 +++++++++++++++---- cmd/config/set_context_test.go | 8 +------ ...onfig-cmd-set-context-too-many-args.golden | 6 ++++- .../config-cmd-set-context-with-help.golden | 4 ++++ pkg/config/cmds.go | 7 ++++++ pkg/config/constants.go | 1 + pkg/config/errors.go | 8 +++++++ pkg/config/options.go | 7 +++++- 8 files changed, 51 insertions(+), 13 deletions(-) diff --git a/cmd/config/set_context.go b/cmd/config/set_context.go index f580fe117..f440cdd62 100644 --- a/cmd/config/set_context.go +++ b/cmd/config/set_context.go @@ -35,12 +35,17 @@ Specifying a name that already exists will merge new fields on top of existing v airshipctl config set-context e2e --%v=kube-system --%v=manifest --%v=auth-info --%v=%v # Update the current-context to e2e -airshipctl config set-context e2e`, +airshipctl config set-context e2e + +# Update attributes of the current-context +airshipctl config set-context --%s --%v=manifest`, config.FlagNamespace, config.FlagManifest, config.FlagAuthInfoName, config.FlagClusterType, - config.Target) + config.Target, + config.FlagCurrent, + config.FlagManifest) ) // NewCmdConfigSetContext creates a command object for the "set-context" action, which @@ -53,15 +58,19 @@ func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra Short: "Switch to a new context or update context values in the airshipctl config", Long: setContextLong, Example: setContextExample, - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { nFlags := cmd.Flags().NFlag() if nFlags == 0 { // Change the current context to the provided context name if no flags are provided o.CurrentContext = true } - o.Name = args[0] + if len(args) == 1 { + //context name is made optional with --current flag added + o.Name = args[0] + } modified, err := config.RunSetContext(o, rootSettings.Config(), true) + if err != nil { return err } @@ -110,4 +119,10 @@ func addSetContextFlags(o *config.ContextOptions, cmd *cobra.Command) { config.FlagClusterType, "", "sets the "+config.FlagClusterType+" for the specified context in the airshipctl config") + + flags.BoolVar( + &o.Current, + config.FlagCurrent, + false, + "use current context from airshipctl config") } diff --git a/cmd/config/set_context_test.go b/cmd/config/set_context_test.go index b6c9d88a6..7ae29005d 100644 --- a/cmd/config/set_context_test.go +++ b/cmd/config/set_context_test.go @@ -55,13 +55,7 @@ func TestConfigSetContext(t *testing.T) { Name: "config-cmd-set-context-too-many-args", CmdLine: "arg1 arg2", Cmd: NewCmdConfigSetContext(nil), - Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2), - }, - { - Name: "config-cmd-set-context-too-few-args", - CmdLine: "", - Cmd: NewCmdConfigSetContext(nil), - Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 0), + Error: fmt.Errorf("accepts at most %d arg(s), received %d", 1, 2), }, } diff --git a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-many-args.golden b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-many-args.golden index d509a9db6..266cd938b 100644 --- a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-many-args.golden +++ b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-many-args.golden @@ -1,4 +1,4 @@ -Error: accepts 1 arg(s), received 2 +Error: accepts at most 1 arg(s), received 2 Usage: set-context NAME [flags] @@ -10,9 +10,13 @@ airshipctl config set-context e2e --namespace=kube-system --manifest=manifest -- # Update the current-context to e2e airshipctl config set-context e2e +# Update attributes of the current-context +airshipctl config set-context --current --manifest=manifest + Flags: --cluster string sets the cluster for the specified context in the airshipctl config --cluster-type string sets the cluster-type for the specified context in the airshipctl config + --current use current context from airshipctl config -h, --help help for set-context --manifest string sets the manifest for the specified context in the airshipctl config --namespace string sets the namespace for the specified context in the airshipctl config diff --git a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-with-help.golden b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-with-help.golden index edd4b19c7..d7fd0c3f8 100644 --- a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-with-help.golden +++ b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-with-help.golden @@ -13,9 +13,13 @@ airshipctl config set-context e2e --namespace=kube-system --manifest=manifest -- # Update the current-context to e2e airshipctl config set-context e2e +# Update attributes of the current-context +airshipctl config set-context --current --manifest=manifest + Flags: --cluster string sets the cluster for the specified context in the airshipctl config --cluster-type string sets the cluster-type for the specified context in the airshipctl config + --current use current context from airshipctl config -h, --help help for set-context --manifest string sets the manifest for the specified context in the airshipctl config --namespace string sets the namespace for the specified context in the airshipctl config diff --git a/pkg/config/cmds.go b/pkg/config/cmds.go index 3f880a47e..075333802 100644 --- a/pkg/config/cmds.go +++ b/pkg/config/cmds.go @@ -194,6 +194,13 @@ func RunSetContext(o *ContextOptions, airconfig *Config, writeToStorage bool) (b if err != nil { return modified, err } + if o.Current { + if airconfig.CurrentContext == "" { + return modified, ErrMissingCurrentContext{} + } + // when --current flag is passed, use current context + o.Name = airconfig.CurrentContext + } context, err := airconfig.GetContext(o.Name) if err != nil { diff --git a/pkg/config/constants.go b/pkg/config/constants.go index fd4369631..3afe83c8d 100644 --- a/pkg/config/constants.go +++ b/pkg/config/constants.go @@ -61,4 +61,5 @@ const ( FlagPassword = "password" FlagTimeout = "request-timeout" FlagUsername = "username" + FlagCurrent = "current" ) diff --git a/pkg/config/errors.go b/pkg/config/errors.go index c2a82a8f2..99b885dd8 100644 --- a/pkg/config/errors.go +++ b/pkg/config/errors.go @@ -84,3 +84,11 @@ type ErrConfigFailed struct { func (e ErrConfigFailed) Error() string { return "Configuration failed to complete." } + +// ErrMissingCurrentContext returned in case --current used without setting current-context +type ErrMissingCurrentContext struct { +} + +func (e ErrMissingCurrentContext) Error() string { + return "Current context must be set before using --current flag" +} diff --git a/pkg/config/options.go b/pkg/config/options.go index 576de94c9..2096090f3 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -42,6 +42,7 @@ type ContextOptions struct { AuthInfo string Manifest string Namespace string + Current bool } type ClusterOptions struct { @@ -75,10 +76,14 @@ func (o *AuthInfoOptions) Validate() error { } func (o *ContextOptions) Validate() error { - if o.Name == "" { + if !o.Current && o.Name == "" { return errors.New("you must specify a non-empty context name") } + if o.Current && o.Name != "" { + return fmt.Errorf("you cannot specify context and --%s Flag at the same time", FlagCurrent) + } + // If the user simply wants to change the current context, no further validation is needed if o.CurrentContext { return nil