diff --git a/cmd/config/set_context.go b/cmd/config/set_context.go index f440cdd62..ec07b74f0 100644 --- a/cmd/config/set_context.go +++ b/cmd/config/set_context.go @@ -60,11 +60,13 @@ func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra Example: setContextExample, Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + o.Name = args[0] nFlags := cmd.Flags().NFlag() if nFlags == 0 { - // Change the current context to the provided context name if no flags are provided - o.CurrentContext = true + fmt.Fprintf(cmd.OutOrStdout(), "Context %q not modified. No new options provided.\n", o.Name) + return nil } + if len(args) == 1 { //context name is made optional with --current flag added o.Name = args[0] diff --git a/cmd/config/set_context_test.go b/cmd/config/set_context_test.go index 7ae29005d..b017e7b64 100644 --- a/cmd/config/set_context_test.go +++ b/cmd/config/set_context_test.go @@ -51,6 +51,11 @@ func TestConfigSetContext(t *testing.T) { CmdLine: "--help", Cmd: NewCmdConfigSetContext(nil), }, + { + Name: "config-cmd-set-context-no-flags", + CmdLine: "context", + Cmd: NewCmdConfigSetContext(nil), + }, { Name: "config-cmd-set-context-too-many-args", CmdLine: "arg1 arg2", @@ -101,7 +106,7 @@ func TestSetContext(t *testing.T) { test.run(t) } -func TestSetCurrentContext(t *testing.T) { +func TestSetCurrentContextNoOptions(t *testing.T) { tname := "def_target" given, cleanupGiven := config.InitConfig(t) defer cleanupGiven(t) @@ -112,10 +117,9 @@ func TestSetCurrentContext(t *testing.T) { expected.CurrentContext = "def_target" test := setContextTest{ - description: "Testing 'airshipctl config set-context' with a new current context", givenConfig: given, args: []string{tname}, - expectedOutput: fmt.Sprintf("Context %q modified.\n", tname), + expectedOutput: fmt.Sprintf("Context %q not modified. No new options provided.\n", tname), expectedConfig: expected, } test.run(t) diff --git a/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-no-flags.golden b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-no-flags.golden new file mode 100644 index 000000000..3880d99cc --- /dev/null +++ b/cmd/config/testdata/TestConfigSetContextGoldenOutput/config-cmd-set-context-no-flags.golden @@ -0,0 +1 @@ +Context "context" not modified. No new options provided.