Shortcircuit commands with version or help flags (#7683)

* Shortcircuit search with help and version flag

Signed-off-by: Derek Nola <derek.nola@suse.com>

* Keep functions seperate

Signed-off-by: Derek Nola <derek.nola@suse.com>

---------

Signed-off-by: Derek Nola <derek.nola@suse.com>
pull/7691/head
Derek Nola 1 year ago committed by GitHub
parent 3a8e98a3b8
commit dc6c569b98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,7 +9,7 @@ import (
var DefaultParser = &Parser{ var DefaultParser = &Parser{
After: []string{"server", "agent", "etcd-snapshot:1"}, After: []string{"server", "agent", "etcd-snapshot:1"},
FlagNames: []string{"--config", "-c"}, ConfigFlags: []string{"--config", "-c"},
EnvName: version.ProgramUpper + "_CONFIG_FILE", EnvName: version.ProgramUpper + "_CONFIG_FILE",
DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml", DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml",
ValidFlags: map[string][]cli.Flag{"server": cmds.ServerFlags, "etcd-snapshot": cmds.EtcdSnapshotFlags}, 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 { func MustFindString(args []string, target string) string {
parser := &Parser{ parser := &Parser{
After: []string{}, OverrideFlags: []string{"--help", "-h", "--version", "-v"},
FlagNames: []string{},
EnvName: version.ProgramUpper + "_CONFIG_FILE", EnvName: version.ProgramUpper + "_CONFIG_FILE",
DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml", DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml",
} }

@ -20,10 +20,13 @@ import (
type Parser struct { type Parser struct {
After []string After []string
FlagNames []string ConfigFlags []string
OverrideFlags []string
EnvName string EnvName string
DefaultConfig 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. // 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) { 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) configFile, isSet := p.findConfigFileFlag(args)
var lastVal string var lastVal string
if configFile != "" { if configFile != "" {
@ -140,13 +149,25 @@ func (p *Parser) FindString(args []string, target string) (string, error) {
return lastVal, nil 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) { func (p *Parser) findConfigFileFlag(args []string) (string, bool) {
if envVal := os.Getenv(p.EnvName); p.EnvName != "" && envVal != "" { if envVal := os.Getenv(p.EnvName); p.EnvName != "" && envVal != "" {
return envVal, true return envVal, true
} }
for i, arg := range args { for i, arg := range args {
for _, flagName := range p.FlagNames { for _, flagName := range p.ConfigFlags {
if flagName == arg { if flagName == arg {
if len(args) > i+1 { if len(args) > i+1 {
return args[i+1], true return args[i+1], true

@ -199,7 +199,7 @@ func Test_UnitParser_findConfigFileFlag(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := Parser{ p := Parser{
FlagNames: []string{"--config", "-c"}, ConfigFlags: []string{"--config", "-c"},
EnvName: "_TEST_FLAG_ENV", EnvName: "_TEST_FLAG_ENV",
DefaultConfig: tt.fields.DefaultConfig, DefaultConfig: tt.fields.DefaultConfig,
} }
@ -328,7 +328,7 @@ func Test_UnitParser_Parse(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := &Parser{ p := &Parser{
After: tt.fields.After, After: tt.fields.After,
FlagNames: tt.fields.FlagNames, ConfigFlags: tt.fields.FlagNames,
EnvName: tt.fields.EnvName, EnvName: tt.fields.EnvName,
DefaultConfig: tt.fields.DefaultConfig, DefaultConfig: tt.fields.DefaultConfig,
} }
@ -447,7 +447,7 @@ func Test_UnitParser_FindString(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
p := &Parser{ p := &Parser{
After: tt.fields.After, After: tt.fields.After,
FlagNames: tt.fields.FlagNames, ConfigFlags: tt.fields.FlagNames,
EnvName: tt.fields.EnvName, EnvName: tt.fields.EnvName,
DefaultConfig: tt.fields.DefaultConfig, DefaultConfig: tt.fields.DefaultConfig,
} }

Loading…
Cancel
Save