mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-09 13:35:52 +02:00
Use cobra (#234)
switch to cobra Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
+85
-143
@@ -8,125 +8,21 @@ import (
|
||||
"github.com/helmfile/helmfile/pkg/app/version"
|
||||
"github.com/helmfile/helmfile/pkg/config"
|
||||
"github.com/helmfile/helmfile/pkg/helmexec"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/urfave/cli"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var logger *zap.SugaredLogger
|
||||
var globalUsage = "Declaratively deploy your Kubernetes manifests, Kustomize configs, and Charts as Helm releases in one shot"
|
||||
|
||||
// RootCmd is the root command for helmfile.
|
||||
func RootCommand() *cli.App {
|
||||
cliApp := cli.NewApp()
|
||||
cliApp.Name = "helmfile"
|
||||
cliApp.Usage = ""
|
||||
cliApp.Version = version.GetVersion()
|
||||
cliApp.EnableBashCompletion = true
|
||||
cliApp.Before = configureLogging
|
||||
setRootCommandFlags(cliApp)
|
||||
|
||||
// add subcommands
|
||||
addDepsSubcommand(cliApp)
|
||||
addReposSubcommand(cliApp)
|
||||
addChartsSubcommand(cliApp)
|
||||
addDiffSubcommand(cliApp)
|
||||
addTemplateSubcommand(cliApp)
|
||||
addWriteValuesSubcommand(cliApp)
|
||||
addLintSubcommand(cliApp)
|
||||
addFetchSubcommand(cliApp)
|
||||
addSyncSubcommand(cliApp)
|
||||
addApplySubcommand(cliApp)
|
||||
addStatusSubcommand(cliApp)
|
||||
addDeleteSubcommand(cliApp)
|
||||
addDestroySubcommand(cliApp)
|
||||
addTestSubcommand(cliApp)
|
||||
addBuildSubcommand(cliApp)
|
||||
addListSubcommand(cliApp)
|
||||
addCacheSubcommand(cliApp)
|
||||
addVersionSubcommand(cliApp)
|
||||
|
||||
return cliApp
|
||||
}
|
||||
|
||||
// setRootCommandFlags sets the flags for the root command.
|
||||
func setRootCommandFlags(cliApp *cli.App) {
|
||||
cliApp.Flags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "helm-binary, b",
|
||||
Usage: "path to helm binary",
|
||||
Value: app.DefaultHelmBinary,
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "file, f",
|
||||
Usage: "load config from file or directory. defaults to `helmfile.yaml` or `helmfile.d`(means `helmfile.d/*.yaml`) in this preference",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "environment, e",
|
||||
Usage: `specify the environment name. defaults to "default"`,
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "state-values-set",
|
||||
Usage: "set state values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "state-values-file",
|
||||
Usage: "specify state values in a YAML file",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "quiet, q",
|
||||
Usage: "Silence output. Equivalent to log-level warn",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "kube-context",
|
||||
Usage: "Set kubectl context. Uses current context by default",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "Enable verbose output for Helm and set log-level to debug, this disables --quiet/-q effect",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "color",
|
||||
Usage: "Output with color",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "no-color",
|
||||
Usage: "Output without color",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "log-level",
|
||||
Usage: "Set log level, default info",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "namespace, n",
|
||||
Usage: "Set namespace. Uses the namespace set in the context by default, and is available in templates as {{ .Namespace }}",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "chart, c",
|
||||
Usage: "Set chart. Uses the chart set in release by default, and is available in template as {{ .Chart }}",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "selector, l",
|
||||
Usage: `Only run using the releases that match labels. Labels can take the form of foo=bar or foo!=bar.
|
||||
A release must match all labels in a group in order to be used. Multiple groups can be specified at once.
|
||||
--selector tier=frontend,tier!=proxy --selector tier=backend. Will match all frontend, non-proxy releases AND all backend releases.
|
||||
The name of a release can be used as a label. --selector name=myrelease`,
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "allow-no-matching-release",
|
||||
Usage: `Do not exit with an error code if the provided selector has no matching releases.`,
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "interactive, i",
|
||||
Usage: "Request confirmation before attempting to modify clusters",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func toCliError(c *cli.Context, err error) error {
|
||||
func toCLIError(g *config.GlobalImpl, err error) error {
|
||||
if err != nil {
|
||||
switch e := err.(type) {
|
||||
case *app.NoMatchingHelmfileError:
|
||||
noMatchingExitCode := 3
|
||||
if c.GlobalBool("allow-no-matching-release") {
|
||||
if g.AllowNoMatchingRelease {
|
||||
noMatchingExitCode = 0
|
||||
}
|
||||
return cli.NewExitError(e.Error(), noMatchingExitCode)
|
||||
@@ -141,42 +37,88 @@ func toCliError(c *cli.Context, err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func configureLogging(c *cli.Context) error {
|
||||
// Valid levels:
|
||||
// https://github.com/uber-go/zap/blob/7e7e266a8dbce911a49554b945538c5b950196b8/zapcore/level.go#L126
|
||||
logLevel := c.GlobalString("log-level")
|
||||
if c.GlobalBool("debug") {
|
||||
logLevel = "debug"
|
||||
} else if c.GlobalBool("quiet") {
|
||||
logLevel = "warn"
|
||||
// NewRootCmd creates the root command for the CLI.
|
||||
func NewRootCmd(globalConfig *config.GlobalOptions, args []string) (*cobra.Command, error) {
|
||||
cmd := &cobra.Command{
|
||||
Use: "helmfile",
|
||||
Short: globalUsage,
|
||||
Long: globalUsage,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Version: version.GetVersion(),
|
||||
SilenceUsage: true,
|
||||
PersistentPreRunE: func(c *cobra.Command, args []string) error {
|
||||
// Valid levels:
|
||||
// https://github.com/uber-go/zap/blob/7e7e266a8dbce911a49554b945538c5b950196b8/zapcore/level.go#L126
|
||||
logLevel := globalConfig.LogLevel
|
||||
switch {
|
||||
case globalConfig.Debug:
|
||||
logLevel = "debug"
|
||||
case globalConfig.Quiet:
|
||||
logLevel = "warn"
|
||||
}
|
||||
logger = helmexec.NewLogger(os.Stderr, logLevel)
|
||||
globalConfig.SetLogger(logger)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
logger = helmexec.NewLogger(os.Stderr, logLevel)
|
||||
if c.App.Metadata == nil {
|
||||
// Auto-initialized in 1.19.0
|
||||
// https://github.com/urfave/cli/blob/master/CHANGELOG.md#1190---2016-11-19
|
||||
c.App.Metadata = make(map[string]interface{})
|
||||
flags := cmd.PersistentFlags()
|
||||
|
||||
// Set the global options for the root command.
|
||||
setGlobalOptionsForRootCmd(flags, globalConfig)
|
||||
|
||||
// We can safely ignore any errors that flags.Parse encounters since
|
||||
// those errors will be caught later during the call to cmd.Execution.
|
||||
// This call is required to gather configuration information prior to
|
||||
// execution.
|
||||
flags.ParseErrorsWhitelist.UnknownFlags = true
|
||||
|
||||
err := flags.Parse(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.App.Metadata["logger"] = logger
|
||||
return nil
|
||||
globalImpl := config.NewGlobalImpl(globalConfig)
|
||||
cmd.AddCommand(
|
||||
NewApplyCmd(globalImpl),
|
||||
NewBuildCmd(globalImpl),
|
||||
NewCacheCmd(globalImpl),
|
||||
NewChartsCmd(globalImpl),
|
||||
NewDeleteCmd(globalImpl),
|
||||
NewDepsCmd(globalImpl),
|
||||
NewDestroyCmd(globalImpl),
|
||||
NewFetchCmd(globalImpl),
|
||||
NewListCmd(globalImpl),
|
||||
NewReposCmd(globalImpl),
|
||||
NewVersionCmd(),
|
||||
NewLintCmd(globalImpl),
|
||||
NewWriteValuesCmd(globalImpl),
|
||||
NewTestCmd(globalImpl),
|
||||
NewTemplateCmd(globalImpl),
|
||||
NewSyncCmd(globalImpl),
|
||||
NewDiffCmd(globalImpl),
|
||||
NewStatusCmd(globalImpl),
|
||||
)
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func Action(do func(*app.App, config.ConfigImpl) error) func(*cli.Context) error {
|
||||
return func(implCtx *cli.Context) error {
|
||||
conf, err := config.NewUrfaveCliConfigImpl(implCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := app.ValidateConfig(conf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a := app.New(conf)
|
||||
|
||||
if err := do(a, conf); err != nil {
|
||||
return toCliError(implCtx, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func setGlobalOptionsForRootCmd(fs *pflag.FlagSet, globalOptions *config.GlobalOptions) {
|
||||
fs.StringVarP(&globalOptions.HelmBinary, "helm-binary", "b", app.DefaultHelmBinary, "Path to the helm binary")
|
||||
fs.StringVarP(&globalOptions.File, "file", "f", globalOptions.File, "load config from file or directory. defaults to `helmfile.yaml` or `helmfile.d`(means `helmfile.d/*.yaml`) in this preference")
|
||||
fs.StringVarP(&globalOptions.Environment, "env", "e", globalOptions.Environment, `specify the environment name. defaults to "default"`)
|
||||
fs.StringArrayVarP(&globalOptions.StateValuesSet, "state-values-set", "s", globalOptions.StateValuesSet, "set state values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
|
||||
fs.StringArrayVarP(&globalOptions.StateValuesFile, "state-values-file", "", globalOptions.StateValuesFile, "specify state values in a YAML file")
|
||||
fs.BoolVarP(&globalOptions.Quiet, "quiet", "q", globalOptions.Quiet, "Silence output. Equivalent to log-level warn")
|
||||
fs.StringVar(&globalOptions.KubeContext, "kube-context", globalOptions.KubeContext, "Set kubectl context. Uses current context by default")
|
||||
fs.BoolVar(&globalOptions.Debug, "debug", globalOptions.Debug, "Enable verbose output for Helm and set log-level to debug, this disables --quiet/-q effect")
|
||||
fs.BoolVar(&globalOptions.Color, "color", globalOptions.Color, "Output with color")
|
||||
fs.BoolVar(&globalOptions.NoColor, "no-color", globalOptions.NoColor, "Output without color")
|
||||
fs.StringVar(&globalOptions.LogLevel, "log-level", globalOptions.LogLevel, "Set log level, default info")
|
||||
fs.StringVar(&globalOptions.Namespace, "namespace", globalOptions.Namespace, "Set namespace. Uses the namespace set in the context by default, and is available in templates as {{ .Namespace }}")
|
||||
fs.StringVar(&globalOptions.Chart, "chart", globalOptions.Chart, "Set chart. Uses the chart set in release by default, and is available in template as {{ .Chart }}")
|
||||
fs.StringArrayVarP(&globalOptions.Selector, "selector", "l", globalOptions.Selector, `Only run using the releases that match labels. Labels can take the form of foo=bar or foo!=bar.
|
||||
A release must match all labels in a group in order to be used. Multiple groups can be specified at once.
|
||||
--selector tier=frontend,tier!=proxy --selector tier=backend. Will match all frontend, non-proxy releases AND all backend releases.
|
||||
The name of a release can be used as a label. --selector name=myrelease`)
|
||||
fs.BoolVar(&globalOptions.AllowNoMatchingRelease, "allow-no-matching-release", globalOptions.AllowNoMatchingRelease, `Do not exit with an error code if the provided selector has no matching releases.`)
|
||||
fs.BoolVarP(&globalOptions.Interactive, "interactive", "i", globalOptions.Interactive, "Request confirmation before attempting to modify clusters")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user