[#56] Add use-context subcommand
Currently, airshipctl users switch their current context by modifying the airshipctl config file or executing airshipctl config set-context [NAME]. This change introduces the use-context subcommand, which replaces the current behavior of using set-context without flags. After this change, set-context will only be used to modify contexts. Change-Id: If3980138a0abeeb6c59c07615b5373654073610a Signed-off-by: Drew Walters <andrew.walters@att.com>
This commit is contained in:
parent
ea52398654
commit
a437caa141
@ -22,6 +22,7 @@ like "airshipctl config set-context my-context" `,
|
|||||||
configRootCmd.AddCommand(NewCmdConfigInit(rootSettings))
|
configRootCmd.AddCommand(NewCmdConfigInit(rootSettings))
|
||||||
configRootCmd.AddCommand(NewCmdConfigSetAuthInfo(rootSettings))
|
configRootCmd.AddCommand(NewCmdConfigSetAuthInfo(rootSettings))
|
||||||
configRootCmd.AddCommand(NewCmdConfigGetAuthInfo(rootSettings))
|
configRootCmd.AddCommand(NewCmdConfigGetAuthInfo(rootSettings))
|
||||||
|
configRootCmd.AddCommand(NewCmdConfigUseContext(rootSettings))
|
||||||
|
|
||||||
return configRootCmd
|
return configRootCmd
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ Available Commands:
|
|||||||
set-cluster Sets a cluster entry in the airshipctl config
|
set-cluster Sets a cluster entry in the airshipctl config
|
||||||
set-context Switch to a new context or update context values 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
|
set-credentials Sets a user entry in the airshipctl config
|
||||||
|
use-context Switch to a different airshipctl context.
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-h, --help help for config
|
-h, --help help for config
|
||||||
|
@ -13,6 +13,7 @@ Available Commands:
|
|||||||
set-cluster Sets a cluster entry in the airshipctl config
|
set-cluster Sets a cluster entry in the airshipctl config
|
||||||
set-context Switch to a new context or update context values 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
|
set-credentials Sets a user entry in the airshipctl config
|
||||||
|
use-context Switch to a different airshipctl context.
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-h, --help help for config
|
-h, --help help for config
|
||||||
|
12
cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-does-not-exist.golden
vendored
Normal file
12
cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-does-not-exist.golden
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
Error: Missing configuration: Context with name 'foo'
|
||||||
|
Usage:
|
||||||
|
use-context NAME [flags]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
# Switch to a context named "e2e"
|
||||||
|
airshipctl config use-context e2e
|
||||||
|
|
||||||
|
Flags:
|
||||||
|
-h, --help help for use-context
|
||||||
|
|
12
cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-no-args.golden
vendored
Normal file
12
cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-no-args.golden
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
Error: accepts 1 arg(s), received 0
|
||||||
|
Usage:
|
||||||
|
use-context NAME [flags]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
# Switch to a context named "e2e"
|
||||||
|
airshipctl config use-context e2e
|
||||||
|
|
||||||
|
Flags:
|
||||||
|
-h, --help help for use-context
|
||||||
|
|
1
cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context.golden
vendored
Normal file
1
cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context.golden
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
Switched to context "dummy_context".
|
59
cmd/config/use_context.go
Normal file
59
cmd/config/use_context.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"opendev.org/airship/airshipctl/pkg/config"
|
||||||
|
"opendev.org/airship/airshipctl/pkg/environment"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
useContextLong = "Switch to a new context defined in the airshipctl config file."
|
||||||
|
|
||||||
|
useContextExample = `
|
||||||
|
# Switch to a context named "e2e"
|
||||||
|
airshipctl config use-context e2e`
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewCmdConfigUseContext creates a command object for the "use-context" action, which
|
||||||
|
// switches to a defined airshipctl context.
|
||||||
|
func NewCmdConfigUseContext(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "use-context NAME",
|
||||||
|
Short: "Switch to a different airshipctl context.",
|
||||||
|
Long: useContextLong,
|
||||||
|
Example: useContextExample,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
contextName := args[0]
|
||||||
|
err := config.RunUseContext(contextName, rootSettings.Config())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(cmd.OutOrStdout(), "Switched to context %q.\n", contextName)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
58
cmd/config/use_context_test.go
Normal file
58
cmd/config/use_context_test.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package config_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
cmd "opendev.org/airship/airshipctl/cmd/config"
|
||||||
|
"opendev.org/airship/airshipctl/pkg/config"
|
||||||
|
"opendev.org/airship/airshipctl/pkg/environment"
|
||||||
|
"opendev.org/airship/airshipctl/testutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConfigUseContext(t *testing.T) {
|
||||||
|
conf := config.DummyConfig()
|
||||||
|
|
||||||
|
settings := &environment.AirshipCTLSettings{}
|
||||||
|
settings.SetConfig(conf)
|
||||||
|
|
||||||
|
cmdTests := []*testutil.CmdTest{
|
||||||
|
{
|
||||||
|
Name: "config-use-context",
|
||||||
|
CmdLine: "dummy_context",
|
||||||
|
Cmd: cmd.NewCmdConfigUseContext(settings),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "config-use-context-no-args",
|
||||||
|
CmdLine: "",
|
||||||
|
Cmd: cmd.NewCmdConfigUseContext(settings),
|
||||||
|
Error: errors.New("accepts 1 arg(s), received 0"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "config-use-context-does-not-exist",
|
||||||
|
CmdLine: "foo",
|
||||||
|
Cmd: cmd.NewCmdConfigUseContext(settings),
|
||||||
|
Error: errors.New("Missing configuration: Context with name 'foo'"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range cmdTests {
|
||||||
|
testutil.RunTest(t, tt)
|
||||||
|
}
|
||||||
|
}
|
@ -237,3 +237,18 @@ func RunSetContext(o *ContextOptions, airconfig *Config, writeToStorage bool) (b
|
|||||||
|
|
||||||
return modified, nil
|
return modified, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RunUseContext(desiredContext string, airconfig *Config) error {
|
||||||
|
if _, err := airconfig.GetContext(desiredContext); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if airconfig.CurrentContext != desiredContext {
|
||||||
|
airconfig.CurrentContext = desiredContext
|
||||||
|
if err := airconfig.PersistConfig(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -247,3 +247,17 @@ func TestRunSetContext(t *testing.T) {
|
|||||||
assert.Equal(t, "new_namespace", conf.Contexts["dummy_context"].kContext.Namespace)
|
assert.Equal(t, "new_namespace", conf.Contexts["dummy_context"].kContext.Namespace)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunUseContext(t *testing.T) {
|
||||||
|
t.Run("testUseContext", func(t *testing.T) {
|
||||||
|
conf := DummyConfig()
|
||||||
|
err := RunUseContext("dummy_context", conf)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("testUseContextDoesNotExist", func(t *testing.T) {
|
||||||
|
conf := NewConfig()
|
||||||
|
err := RunUseContext("foo", conf)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user