Merge "[#56] Add use-context subcommand"

This commit is contained in:
Zuul 2020-02-26 18:35:08 +00:00 committed by Gerrit Code Review
commit 5c78f19284
10 changed files with 174 additions and 0 deletions

View File

@ -22,6 +22,7 @@ like "airshipctl config set-context my-context" `,
configRootCmd.AddCommand(NewCmdConfigInit(rootSettings))
configRootCmd.AddCommand(NewCmdConfigSetAuthInfo(rootSettings))
configRootCmd.AddCommand(NewCmdConfigGetAuthInfo(rootSettings))
configRootCmd.AddCommand(NewCmdConfigUseContext(rootSettings))
return configRootCmd
}

View File

@ -13,6 +13,7 @@ Available Commands:
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-credentials Sets a user entry in the airshipctl config
use-context Switch to a different airshipctl context.
Flags:
-h, --help help for config

View File

@ -13,6 +13,7 @@ Available Commands:
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-credentials Sets a user entry in the airshipctl config
use-context Switch to a different airshipctl context.
Flags:
-h, --help help for config

View 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

View 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

View File

@ -0,0 +1 @@
Switched to context "dummy_context".

59
cmd/config/use_context.go Normal file
View 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
}

View 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)
}
}

View File

@ -237,3 +237,18 @@ func RunSetContext(o *ContextOptions, airconfig *Config, writeToStorage bool) (b
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
}

View File

@ -247,3 +247,17 @@ func TestRunSetContext(t *testing.T) {
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)
})
}