diff --git a/cmd/k3s/main.go b/cmd/k3s/main.go index 4f9f720356d1..1a8afe69f123 100644 --- a/cmd/k3s/main.go +++ b/cmd/k3s/main.go @@ -21,6 +21,7 @@ import ( "github.com/pkg/errors" "github.com/rancher/wrangler/pkg/resolvehome" "github.com/sirupsen/logrus" + "github.com/spf13/pflag" "github.com/urfave/cli" ) @@ -101,29 +102,21 @@ func findDataDir() string { return dataDir } -// findPreferBundledBin searches for prefer-bundled-bin from the CLI args and config file. +// findPreferBundledBin searches for prefer-bundled-bin from the config file, then CLI args. +// we use pflag to process the args because we don't have a cli.Context yet func findPreferBundledBin(args []string) bool { + var preferBundledBin bool + fs := pflag.NewFlagSet("prefer-set", pflag.ContinueOnError) + fs.ParseErrorsWhitelist.UnknownFlags = true + fs.BoolVar(&preferBundledBin, "prefer-bundled-bin", false, "Prefer bundled binaries") + preferRes := configfilearg.MustFindString(args, "prefer-bundled-bin") - flagName := "--prefer-bundled-bin" - for i, arg := range args { - if flagName == arg { - if len(args) > i+1 { - if _, err := strconv.ParseBool(args[i+1]); err == nil { - preferRes = args[i+1] - } - } else { - //Naked flag, assume true - preferRes = "true" - } - } else if strings.HasPrefix(arg, flagName+"=") { - preferRes = arg[len(flagName)+1:] - } + if preferRes != "" { + preferBundledBin, _ = strconv.ParseBool(preferRes) } - res, err := strconv.ParseBool(preferRes) - if err != nil { - return false - } - return res + + fs.Parse(args) + return preferBundledBin } // runCLIs handles the case where the binary is being executed as a symlink alias, diff --git a/cmd/k3s/main_test.go b/cmd/k3s/main_test.go index c200257ec409..29e01dd84206 100644 --- a/cmd/k3s/main_test.go +++ b/cmd/k3s/main_test.go @@ -29,28 +29,23 @@ func Test_UnitFindPreferBundledBin(t *testing.T) { want: false, }, { - name: "Argument with space 1", - args: []string{"--prefer-bundled-bin", "1"}, + name: "Argument with equal 1", + args: []string{"--prefer-bundled-bin=1"}, want: true, }, { - name: "Argument with space 0", - args: []string{"--prefer-bundled-bin", "0"}, + name: "Argument with equal 0", + args: []string{"--prefer-bundled-bin=0"}, want: false, }, { - name: "Multiple arguments with space true", - args: []string{"--abcd", "--prefer-bundled-bin", "true", "--efgh"}, + name: "Multiple arguments", + args: []string{"--abcd", "--prefer-bundled-bin", "--efgh"}, want: true, }, - { - name: "Multiple arguments with space false", - args: []string{"--abcd", "--prefer-bundled-bin", "false", "--efgh"}, - want: false, - }, { name: "Repeated arguments", - args: []string{"--abcd", "--prefer-bundled-bin", "false", "--prefer-bundled-bin"}, + args: []string{"--abcd", "--prefer-bundled-bin=false", "--prefer-bundled-bin"}, want: true, }, }