2019-12-04 17:19:06 -05:00
|
|
|
/*
|
|
|
|
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 (
|
|
|
|
"github.com/spf13/cobra"
|
2021-02-26 23:22:08 -06:00
|
|
|
"github.com/spf13/pflag"
|
2019-12-04 17:19:06 -05:00
|
|
|
|
|
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
|
|
|
)
|
|
|
|
|
2020-04-15 16:59:49 -05:00
|
|
|
const (
|
2020-02-05 16:26:19 -06:00
|
|
|
setContextLong = `
|
2021-05-06 14:46:42 +05:30
|
|
|
Creates or modifies context in the airshipctl config file based on the CONTEXT_NAME passed or for the current context
|
|
|
|
if --current flag is specified. It accepts optional flags which include manifest name and management-config name.
|
2020-04-15 16:59:49 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
setContextExample = `
|
2021-05-06 14:46:42 +05:30
|
|
|
To create a new context named "exampleContext"
|
|
|
|
# airshipctl config set-context exampleContext --manifest=exampleManifest
|
|
|
|
|
|
|
|
To update the manifest of the current-context
|
|
|
|
# airshipctl config set-context --current --manifest=exampleManifest
|
2020-04-15 16:59:49 -05:00
|
|
|
`
|
2021-02-26 23:22:08 -06:00
|
|
|
|
|
|
|
setContextManifestFlag = "manifest"
|
|
|
|
setContextManagementConfigFlag = "management-config"
|
|
|
|
setContextCurrentFlag = "current"
|
2019-12-04 17:19:06 -05:00
|
|
|
)
|
|
|
|
|
2020-04-15 16:59:49 -05:00
|
|
|
// NewSetContextCommand creates a command for creating and modifying contexts
|
|
|
|
// in the airshipctl config
|
2020-08-28 18:32:33 -05:00
|
|
|
func NewSetContextCommand(cfgFactory config.Factory) *cobra.Command {
|
2020-02-07 16:27:05 -06:00
|
|
|
o := &config.ContextOptions{}
|
|
|
|
cmd := &cobra.Command{
|
2021-05-06 14:46:42 +05:30
|
|
|
Use: "set-context CONTEXT_NAME",
|
|
|
|
Short: "Airshipctl command to create/modify context in airshipctl config file",
|
2020-04-15 16:59:49 -05:00
|
|
|
Long: setContextLong[1:],
|
2019-12-04 17:19:06 -05:00
|
|
|
Example: setContextExample,
|
2020-02-21 13:38:06 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
2021-02-26 23:22:08 -06:00
|
|
|
RunE: setContextRunE(cfgFactory, o),
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|
|
|
|
|
2021-02-26 23:22:08 -06:00
|
|
|
addSetContextFlags(cmd, o)
|
2020-02-07 16:27:05 -06:00
|
|
|
return cmd
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|
|
|
|
|
2021-02-26 23:22:08 -06:00
|
|
|
func addSetContextFlags(cmd *cobra.Command, o *config.ContextOptions) {
|
2020-02-07 16:27:05 -06:00
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2021-05-06 14:46:42 +05:30
|
|
|
flags.StringVar(&o.Manifest, setContextManifestFlag, "",
|
2020-04-15 16:59:49 -05:00
|
|
|
"set the manifest for the specified context")
|
2021-05-06 14:46:42 +05:30
|
|
|
flags.StringVar(&o.ManagementConfiguration, setContextManagementConfigFlag, "",
|
2021-02-26 23:22:08 -06:00
|
|
|
"set the management config for the specified context")
|
2021-05-06 14:46:42 +05:30
|
|
|
flags.BoolVar(&o.Current, setContextCurrentFlag, false,
|
2020-04-15 16:59:49 -05:00
|
|
|
"update the current context")
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|
2021-02-26 23:22:08 -06:00
|
|
|
|
|
|
|
func setContextRunE(cfgFactory config.Factory, o *config.ContextOptions) func(cmd *cobra.Command, args []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctxName := ""
|
|
|
|
if len(args) == 1 {
|
|
|
|
ctxName = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all the flags that have been set
|
|
|
|
var opts []config.ContextOption
|
|
|
|
fn := func(flag *pflag.Flag) {
|
|
|
|
switch flag.Name {
|
|
|
|
case setContextManifestFlag:
|
|
|
|
opts = append(opts, config.SetContextManifest(o.Manifest))
|
|
|
|
case setContextManagementConfigFlag:
|
|
|
|
opts = append(opts, config.SetContextManagementConfig(o.ManagementConfiguration))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd.Flags().Visit(fn)
|
|
|
|
|
|
|
|
options := &config.RunSetContextOptions{
|
|
|
|
CfgFactory: cfgFactory,
|
|
|
|
CtxName: ctxName,
|
|
|
|
Current: o.Current,
|
|
|
|
Writer: cmd.OutOrStdout(),
|
|
|
|
}
|
|
|
|
return options.RunSetContext(opts...)
|
|
|
|
}
|
|
|
|
}
|