mirror of https://github.com/k3s-io/k3s
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
parent
3a8e98a3b8
commit
dc6c569b98
|
@ -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",
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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…
Reference in New Issue