From 18d6e2ac295d18cdf1c68ab1fd40b03fb8ad13dc Mon Sep 17 00:00:00 2001 From: Drew Walters Date: Tue, 18 Feb 2020 12:07:10 -0600 Subject: [PATCH] [#51] Remove current-context flag Executing "airshipctl config set-context [NAME]" misleadingly prints a message saying the context has been modified, but doesn't change to that context or modify any context values. This change modifies the behavior of context command to switch to a provided context when no flags are specified. It also removes the current-context flag, which becomes redundant with this change. Change-Id: I2622fbf59539513feb1236f13b9090978aeb81ef Signed-off-by: Drew Walters --- cmd/config/set_context.go | 16 +++++++++------- cmd/config/set_context_test.go | 9 +++------ .../config-cmd-with-defaults.golden | 2 +- .../config-cmd-with-help.golden | 2 +- .../config-cmd-set-context-too-few-args.golden | 3 +-- .../config-cmd-set-context-too-many-args.golden | 3 +-- .../config-cmd-set-context-with-help.golden | 3 +-- 7 files changed, 17 insertions(+), 21 deletions(-) diff --git a/cmd/config/set_context.go b/cmd/config/set_context.go index 8ea1d97d8..ac7713d21 100644 --- a/cmd/config/set_context.go +++ b/cmd/config/set_context.go @@ -35,13 +35,12 @@ 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 --%v=true`, +airshipctl config set-context e2e`, config.FlagNamespace, config.FlagManifest, config.FlagAuthInfoName, config.FlagClusterType, - config.Target, - config.FlagCurrentContext) + config.Target) ) // NewCmdConfigSetContext creates a command object for the "set-context" action, which @@ -51,13 +50,19 @@ func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra setcontextcmd := &cobra.Command{ Use: "set-context NAME", - Short: "Sets a context entry or updates current-context in the airshipctl config", + Short: "Switch to a new context or update context values in the airshipctl config", Long: setContextLong, Example: setContextExample, Args: cobra.ExactArgs(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 + theContext.CurrentContext = true + } theContext.Name = cmd.Flags().Args()[0] modified, err := config.RunSetContext(theContext, rootSettings.Config(), true) + if err != nil { return err } @@ -75,9 +80,6 @@ func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra } func sctxInitFlags(o *config.ContextOptions, setcontextcmd *cobra.Command) { - setcontextcmd.Flags().BoolVar(&o.CurrentContext, config.FlagCurrentContext, false, - config.FlagCurrentContext+" for the context entry in airshipctl config") - setcontextcmd.Flags().StringVar(&o.Cluster, config.FlagClusterName, o.Cluster, config.FlagClusterName+" for the context entry in airshipctl config") diff --git a/cmd/config/set_context_test.go b/cmd/config/set_context_test.go index 6fcad51f2..687f2026e 100644 --- a/cmd/config/set_context_test.go +++ b/cmd/config/set_context_test.go @@ -112,12 +112,9 @@ func TestSetCurrentContext(t *testing.T) { expconf.CurrentContext = "def_target" test := setContextTest{ - description: "Testing 'airshipctl config set-context' with a new current context", - config: conf, - args: []string{tname}, - flags: []string{ - "--" + config.FlagCurrentContext + "=true", - }, + description: "Testing 'airshipctl config set-context' with a new current context", + config: conf, + args: []string{tname}, expected: `Context "` + tname + `" modified.` + "\n", expectedConfig: expconf, } diff --git a/cmd/config/testdata/TestConfigGoldenOutput/config-cmd-with-defaults.golden b/cmd/config/testdata/TestConfigGoldenOutput/config-cmd-with-defaults.golden index 2db28fab3..bfa89e315 100644 --- a/cmd/config/testdata/TestConfigGoldenOutput/config-cmd-with-defaults.golden +++ b/cmd/config/testdata/TestConfigGoldenOutput/config-cmd-with-defaults.golden @@ -11,7 +11,7 @@ Available Commands: help Help about any command init Generate initial configuration files for airshipctl set-cluster Sets a cluster entry in the airshipctl config - set-context Sets a context entry or updates current-context in the airshipctl config + set-context Switch to a new context or update context values in the airshipctl config set-credentials Sets a user entry in the airshipctl config Flags: diff --git a/cmd/config/testdata/TestConfigGoldenOutput/config-cmd-with-help.golden b/cmd/config/testdata/TestConfigGoldenOutput/config-cmd-with-help.golden index 2db28fab3..bfa89e315 100644 --- a/cmd/config/testdata/TestConfigGoldenOutput/config-cmd-with-help.golden +++ b/cmd/config/testdata/TestConfigGoldenOutput/config-cmd-with-help.golden @@ -11,7 +11,7 @@ Available Commands: help Help about any command init Generate initial configuration files for airshipctl set-cluster Sets a cluster entry in the airshipctl config - set-context Sets a context entry or updates current-context in the airshipctl config + set-context Switch to a new context or update context values in the airshipctl config set-credentials Sets a user entry in the airshipctl config Flags: diff --git a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-few-args.golden b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-few-args.golden index 491364939..89aa5d481 100644 --- a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-few-args.golden +++ b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-too-few-args.golden @@ -8,12 +8,11 @@ Examples: airshipctl config set-context e2e --namespace=kube-system --manifest=manifest --user=auth-info --cluster-type=target # Update the current-context to e2e -airshipctl config set-context e2e --current-context=true +airshipctl config set-context e2e Flags: --cluster string cluster for the context entry in airshipctl config --cluster-type string cluster-type for the context entry in airshipctl config - --current-context current-context for the context entry in airshipctl config -h, --help help for set-context --manifest string manifest for the context entry in airshipctl config --namespace string namespace for the context entry in airshipctl config 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 67246757b..70662d749 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 @@ -8,12 +8,11 @@ Examples: airshipctl config set-context e2e --namespace=kube-system --manifest=manifest --user=auth-info --cluster-type=target # Update the current-context to e2e -airshipctl config set-context e2e --current-context=true +airshipctl config set-context e2e Flags: --cluster string cluster for the context entry in airshipctl config --cluster-type string cluster-type for the context entry in airshipctl config - --current-context current-context for the context entry in airshipctl config -h, --help help for set-context --manifest string manifest for the context entry in airshipctl config --namespace string namespace for the context entry in 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 9d373be24..31fb4c19f 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 @@ -11,12 +11,11 @@ Examples: airshipctl config set-context e2e --namespace=kube-system --manifest=manifest --user=auth-info --cluster-type=target # Update the current-context to e2e -airshipctl config set-context e2e --current-context=true +airshipctl config set-context e2e Flags: --cluster string cluster for the context entry in airshipctl config --cluster-type string cluster-type for the context entry in airshipctl config - --current-context current-context for the context entry in airshipctl config -h, --help help for set-context --manifest string manifest for the context entry in airshipctl config --namespace string namespace for the context entry in airshipctl config