Skip to content

Commit

Permalink
🐛 pre-process all CLI flags
Browse files Browse the repository at this point in the history
We were getting errors for commands that had not been pre-processed,
like `login --token`.

This PR changes the CLI pre-processor and makes it traverse the root
command to find all possible flags across any command and add them to
the pre-processing flags. This avoid the warning/error.

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus committed Sep 28, 2023
1 parent 458356d commit eb1c811
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions cli/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func AttachCLIs(rootCmd *cobra.Command, commands ...*Command) error {
return err
}

connectorName, autoUpdate := detectConnectorName(os.Args, commands, existing)
connectorName, autoUpdate := detectConnectorName(os.Args, rootCmd, commands, existing)
if connectorName != "" {
if _, err := providers.EnsureProvider(existing, connectorName, "", autoUpdate); err != nil {
return err
Expand All @@ -49,7 +49,7 @@ func AttachCLIs(rootCmd *cobra.Command, commands ...*Command) error {
return nil
}

func detectConnectorName(args []string, commands []*Command, providers providers.Providers) (string, bool) {
func detectConnectorName(args []string, rootCmd *cobra.Command, commands []*Command, providers providers.Providers) (string, bool) {
autoUpdate := true

config.InitViperConfig()
Expand All @@ -66,12 +66,24 @@ func detectConnectorName(args []string, commands []*Command, providers providers
attachFlag(flags, builtins[i])
}

for i := range commands {
cmd := commands[i]
cmd.Command.Flags().VisitAll(func(flag *pflag.Flag) {
if found := flags.Lookup(flag.Name); found == nil {
flags.AddFlag(flag)
// To avoid warnings about flags, we need to mock every single flag across
// all commands that are attached to the runtime. This is done by adding
// flags from the root command and all its descendent commands. We don't
// care about these flags, we just want to avoid the errors and make sure
// they are parsed correctly (e.g. --key a,b --etc)
cobraCmds := []*cobra.Command{rootCmd}
for i := 0; i < len(cobraCmds); i++ {
cobraCmds = append(cobraCmds, cobraCmds[i].Commands()...)
cobraCmds[i].Flags().VisitAll(func(flag *pflag.Flag) {
if found := flags.Lookup(flag.Name); found != nil {
return
}
if flag.Shorthand != "" {
if found := flags.ShorthandLookup(flag.Shorthand); found != nil {
return
}
}
flags.AddFlag(flag)
})
}

Expand All @@ -95,14 +107,14 @@ func detectConnectorName(args []string, commands []*Command, providers providers

autoUpdate, _ = flags.GetBool("auto-update")

remaining := flags.Args()
if len(remaining) <= 1 {
parsedArgs := flags.Args()
if len(parsedArgs) <= 1 {
return "", autoUpdate
}

commandFound := false
for j := range commands {
if commands[j].Command.Use == remaining[1] {
if commands[j].Command.Use == parsedArgs[1] {
commandFound = true
break
}
Expand All @@ -113,11 +125,11 @@ func detectConnectorName(args []string, commands []*Command, providers providers

// since we have a known command, we can now expect the connector to be
// local by default if nothing else is set
if len(remaining) == 2 {
if len(parsedArgs) == 2 {
return "local", autoUpdate
}

connector := remaining[2]
connector := parsedArgs[2]
// we may want to double-check if the connector exists

return connector, autoUpdate
Expand Down

0 comments on commit eb1c811

Please sign in to comment.