Browse Source

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
commit
dc6c569b98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      pkg/configfilearg/defaultparser.go
  2. 27
      pkg/configfilearg/parser.go
  3. 6
      pkg/configfilearg/parser_test.go

5
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",
}

27
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

6
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,
}

Loading…
Cancel
Save