airshipctl/cmd/config/set_context.go
Drew Walters 18d6e2ac29 [#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 <andrew.walters@att.com>
2020-02-18 13:06:37 -06:00

98 lines
3.2 KiB
Go

/*
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 (
setContextLong = `
Sets a context entry in arshipctl config.
Specifying a name that already exists will merge new fields on top of existing values for those fields.`
setContextExample = fmt.Sprintf(`
# Create a completely new e2e context entry
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`,
config.FlagNamespace,
config.FlagManifest,
config.FlagAuthInfoName,
config.FlagClusterType,
config.Target)
)
// NewCmdConfigSetContext creates a command object for the "set-context" action, which
// defines a new Context airshipctl config.
func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
theContext := &config.ContextOptions{}
setcontextcmd := &cobra.Command{
Use: "set-context NAME",
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
}
if modified {
fmt.Fprintf(cmd.OutOrStdout(), "Context %q modified.\n", theContext.Name)
} else {
fmt.Fprintf(cmd.OutOrStdout(), "Context %q created.\n", theContext.Name)
}
return nil
},
}
sctxInitFlags(theContext, setcontextcmd)
return setcontextcmd
}
func sctxInitFlags(o *config.ContextOptions, setcontextcmd *cobra.Command) {
setcontextcmd.Flags().StringVar(&o.Cluster, config.FlagClusterName, o.Cluster,
config.FlagClusterName+" for the context entry in airshipctl config")
setcontextcmd.Flags().StringVar(&o.AuthInfo, config.FlagAuthInfoName, o.AuthInfo,
config.FlagAuthInfoName+" for the context entry in airshipctl config")
setcontextcmd.Flags().StringVar(&o.Manifest, config.FlagManifest, o.Manifest,
config.FlagManifest+" for the context entry in airshipctl config")
setcontextcmd.Flags().StringVar(&o.Namespace, config.FlagNamespace, o.Namespace,
config.FlagNamespace+" for the context entry in airshipctl config")
setcontextcmd.Flags().StringVar(&o.ClusterType, config.FlagClusterType, "",
config.FlagClusterType+" for the context entry in airshipctl config")
}