Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle --all-clusters flag #1085

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pkg/execute/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func ParseFlags(cmd string) (Flags, error) {
if err != nil {
return Flags{}, err
}

// all-clusters flag is the flag used when multiple clusters are connected to the same instance,
// we need to remove it so other commands won't report problems with unknown flag.
cmd, _, err = extractBoolParam(cmd, "all-clusters")
if err != nil {
return Flags{}, fmt.Errorf("while extracting all-cluster flag: %w", err)
}

tokenized, err := shellwords.Parse(cmd)
if err != nil {
return Flags{}, errors.New(cantParseCmd)
Expand Down Expand Up @@ -94,3 +102,41 @@ func extractParam(cmd, flagName string) (string, string, error) {
}
return cmd, withParam, nil
}

func extractBoolParam(cmd, flagName string) (string, bool, error) {
flag := fmt.Sprintf("--%s", flagName)
var isSet bool
args, _ := shellwords.Parse(cmd)
f := pflag.NewFlagSet("extract-params", pflag.ContinueOnError)
f.BoolP("help", "h", false, "to make sure that parsing is ignoring the --help,-h flags")

f.ParseErrorsWhitelist.UnknownFlags = true
f.BoolVar(&isSet, flagName, false, "Boolean flag")
if err := f.Parse(args); err != nil {
return "", false, fmt.Errorf(incorrectParamFlag, flag, err)
}

for _, val := range []string{"true", "false"} {
paramFlagRegex, err := regexFlag(flag, val)
if err != nil {
return "", false, fmt.Errorf("could not extract provided %s", flagName)
}

matches := paramFlagRegex.FindStringSubmatch(cmd)
if len(matches) == 0 {
continue
}
cmd = strings.Replace(cmd, fmt.Sprintf(" %s", matches[0]), "", -1)
}

cmd = strings.ReplaceAll(cmd, flag, "")
return cmd, isSet, nil
}

func regexFlag(flag, escapedParamVal string) (*regexp.Regexp, error) {
return regexp.Compile(fmt.Sprintf(`%s[=|(' ')]*('%s'|"%s"|%s)("|')*`,
flag,
escapedParamVal,
escapedParamVal,
escapedParamVal))
}