Skip to content

Commit

Permalink
Convert parsing to pflag library
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <[email protected]>
  • Loading branch information
dereknola committed Nov 11, 2022
1 parent 8cc1cc7 commit fe01d06
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
33 changes: 13 additions & 20 deletions cmd/k3s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
19 changes: 7 additions & 12 deletions cmd/k3s/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down

0 comments on commit fe01d06

Please sign in to comment.