From dc6c569b9881c2978aa6c673e06b0c112ff36f78 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Wed, 7 Jun 2023 15:57:52 -0700 Subject: [PATCH] Shortcircuit commands with version or help flags (#7683) * Shortcircuit search with help and version flag Signed-off-by: Derek Nola * Keep functions seperate Signed-off-by: Derek Nola --------- Signed-off-by: Derek Nola --- pkg/configfilearg/defaultparser.go | 5 ++--- pkg/configfilearg/parser.go | 27 ++++++++++++++++++++++++--- pkg/configfilearg/parser_test.go | 6 +++--- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/pkg/configfilearg/defaultparser.go b/pkg/configfilearg/defaultparser.go index bbc44cb087..b9b6d04fca 100644 --- a/pkg/configfilearg/defaultparser.go +++ b/pkg/configfilearg/defaultparser.go @@ -9,7 +9,7 @@ import ( var DefaultParser = &Parser{ After: []string{"server", "agent", "etcd-snapshot:1"}, - FlagNames: []string{"--config", "-c"}, + ConfigFlags: []string{"--config", "-c"}, EnvName: version.ProgramUpper + "_CONFIG_FILE", DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml", ValidFlags: map[string][]cli.Flag{"server": cmds.ServerFlags, "etcd-snapshot": cmds.EtcdSnapshotFlags}, @@ -25,8 +25,7 @@ func MustParse(args []string) []string { func MustFindString(args []string, target string) string { parser := &Parser{ - After: []string{}, - FlagNames: []string{}, + OverrideFlags: []string{"--help", "-h", "--version", "-v"}, EnvName: version.ProgramUpper + "_CONFIG_FILE", DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml", } diff --git a/pkg/configfilearg/parser.go b/pkg/configfilearg/parser.go index f93e13911b..06fce3cf25 100644 --- a/pkg/configfilearg/parser.go +++ b/pkg/configfilearg/parser.go @@ -20,10 +20,13 @@ import ( type Parser struct { After []string - FlagNames []string + ConfigFlags []string + OverrideFlags []string EnvName string DefaultConfig string - ValidFlags map[string][]cli.Flag + // ValidFlags are maps of flags that are valid for that particular conmmand. This enables us to ignore flags in + // the config file that do no apply to the current command. + ValidFlags map[string][]cli.Flag } // Parse will parse an os.Args style slice looking for Parser.FlagNames after Parse.After. @@ -97,6 +100,12 @@ func (p *Parser) stripInvalidFlags(command string, args []string) ([]string, err } func (p *Parser) FindString(args []string, target string) (string, error) { + + // Check for --help or --version flags, which override any other flags + if val, found := p.findOverrideFlag(args); found { + return val, nil + } + configFile, isSet := p.findConfigFileFlag(args) var lastVal string if configFile != "" { @@ -140,13 +149,25 @@ func (p *Parser) FindString(args []string, target string) (string, error) { return lastVal, nil } +func (p *Parser) findOverrideFlag(args []string) (string, bool) { + for _, arg := range args { + for _, flagName := range p.OverrideFlags { + if flagName == arg { + return arg, true + } + } + } + + return "", false +} + func (p *Parser) findConfigFileFlag(args []string) (string, bool) { if envVal := os.Getenv(p.EnvName); p.EnvName != "" && envVal != "" { return envVal, true } for i, arg := range args { - for _, flagName := range p.FlagNames { + for _, flagName := range p.ConfigFlags { if flagName == arg { if len(args) > i+1 { return args[i+1], true diff --git a/pkg/configfilearg/parser_test.go b/pkg/configfilearg/parser_test.go index 66faa75868..1dc4640ab9 100644 --- a/pkg/configfilearg/parser_test.go +++ b/pkg/configfilearg/parser_test.go @@ -199,7 +199,7 @@ func Test_UnitParser_findConfigFileFlag(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := Parser{ - FlagNames: []string{"--config", "-c"}, + ConfigFlags: []string{"--config", "-c"}, EnvName: "_TEST_FLAG_ENV", DefaultConfig: tt.fields.DefaultConfig, } @@ -328,7 +328,7 @@ func Test_UnitParser_Parse(t *testing.T) { t.Run(tt.name, func(t *testing.T) { p := &Parser{ After: tt.fields.After, - FlagNames: tt.fields.FlagNames, + ConfigFlags: tt.fields.FlagNames, EnvName: tt.fields.EnvName, DefaultConfig: tt.fields.DefaultConfig, } @@ -447,7 +447,7 @@ func Test_UnitParser_FindString(t *testing.T) { t.Run(tt.name, func(t *testing.T) { p := &Parser{ After: tt.fields.After, - FlagNames: tt.fields.FlagNames, + ConfigFlags: tt.fields.FlagNames, EnvName: tt.fields.EnvName, DefaultConfig: tt.fields.DefaultConfig, }