[#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
This commit is contained in:
parent
70eb76e608
commit
8d97d5d7ee
@ -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
|
airshipctl config set-context e2e --%v=kube-system --%v=manifest --%v=auth-info --%v=%v
|
||||||
|
|
||||||
# Update the current-context to e2e
|
# 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.FlagNamespace,
|
||||||
config.FlagManifest,
|
config.FlagManifest,
|
||||||
config.FlagAuthInfoName,
|
config.FlagAuthInfoName,
|
||||||
config.FlagClusterType,
|
config.FlagClusterType,
|
||||||
config.Target)
|
config.Target,
|
||||||
|
config.FlagCurrent,
|
||||||
|
config.FlagManifest)
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewCmdConfigSetContext creates a command object for the "set-context" action, which
|
// 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",
|
Short: "Switch to a new context or update context values in the airshipctl config",
|
||||||
Long: setContextLong,
|
Long: setContextLong,
|
||||||
Example: setContextExample,
|
Example: setContextExample,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.MaximumNArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
nFlags := cmd.Flags().NFlag()
|
nFlags := cmd.Flags().NFlag()
|
||||||
if nFlags == 0 {
|
if nFlags == 0 {
|
||||||
// Change the current context to the provided context name if no flags are provided
|
// Change the current context to the provided context name if no flags are provided
|
||||||
o.CurrentContext = true
|
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)
|
modified, err := config.RunSetContext(o, rootSettings.Config(), true)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -110,4 +119,10 @@ func addSetContextFlags(o *config.ContextOptions, cmd *cobra.Command) {
|
|||||||
config.FlagClusterType,
|
config.FlagClusterType,
|
||||||
"",
|
"",
|
||||||
"sets the "+config.FlagClusterType+" for the specified context in the airshipctl config")
|
"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")
|
||||||
}
|
}
|
||||||
|
@ -55,13 +55,7 @@ func TestConfigSetContext(t *testing.T) {
|
|||||||
Name: "config-cmd-set-context-too-many-args",
|
Name: "config-cmd-set-context-too-many-args",
|
||||||
CmdLine: "arg1 arg2",
|
CmdLine: "arg1 arg2",
|
||||||
Cmd: NewCmdConfigSetContext(nil),
|
Cmd: NewCmdConfigSetContext(nil),
|
||||||
Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2),
|
Error: fmt.Errorf("accepts at most %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),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
Error: accepts 1 arg(s), received 2
|
Error: accepts at most 1 arg(s), received 2
|
||||||
Usage:
|
Usage:
|
||||||
set-context NAME [flags]
|
set-context NAME [flags]
|
||||||
|
|
||||||
@ -10,9 +10,13 @@ airshipctl config set-context e2e --namespace=kube-system --manifest=manifest --
|
|||||||
# Update the current-context to e2e
|
# 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 --current --manifest=manifest
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
--cluster string sets the cluster for the specified context in the airshipctl config
|
--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
|
--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
|
-h, --help help for set-context
|
||||||
--manifest string sets the manifest for the specified context in the airshipctl config
|
--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
|
--namespace string sets the namespace for the specified context in the airshipctl config
|
||||||
|
@ -13,9 +13,13 @@ airshipctl config set-context e2e --namespace=kube-system --manifest=manifest --
|
|||||||
# Update the current-context to e2e
|
# 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 --current --manifest=manifest
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
--cluster string sets the cluster for the specified context in the airshipctl config
|
--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
|
--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
|
-h, --help help for set-context
|
||||||
--manifest string sets the manifest for the specified context in the airshipctl config
|
--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
|
--namespace string sets the namespace for the specified context in the airshipctl config
|
||||||
|
@ -194,6 +194,13 @@ func RunSetContext(o *ContextOptions, airconfig *Config, writeToStorage bool) (b
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return modified, err
|
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)
|
context, err := airconfig.GetContext(o.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -61,4 +61,5 @@ const (
|
|||||||
FlagPassword = "password"
|
FlagPassword = "password"
|
||||||
FlagTimeout = "request-timeout"
|
FlagTimeout = "request-timeout"
|
||||||
FlagUsername = "username"
|
FlagUsername = "username"
|
||||||
|
FlagCurrent = "current"
|
||||||
)
|
)
|
||||||
|
@ -84,3 +84,11 @@ type ErrConfigFailed struct {
|
|||||||
func (e ErrConfigFailed) Error() string {
|
func (e ErrConfigFailed) Error() string {
|
||||||
return "Configuration failed to complete."
|
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"
|
||||||
|
}
|
||||||
|
@ -42,6 +42,7 @@ type ContextOptions struct {
|
|||||||
AuthInfo string
|
AuthInfo string
|
||||||
Manifest string
|
Manifest string
|
||||||
Namespace string
|
Namespace string
|
||||||
|
Current bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClusterOptions struct {
|
type ClusterOptions struct {
|
||||||
@ -75,10 +76,14 @@ func (o *AuthInfoOptions) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *ContextOptions) 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")
|
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 the user simply wants to change the current context, no further validation is needed
|
||||||
if o.CurrentContext {
|
if o.CurrentContext {
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user