From eb1c811513c980cd9510188fbdd8b30f3737f551 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Thu, 28 Sep 2023 00:59:54 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20pre-process=20all=20CLI=20flags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- cli/providers/providers.go | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/cli/providers/providers.go b/cli/providers/providers.go index e67b108317..7c1fb20416 100644 --- a/cli/providers/providers.go +++ b/cli/providers/providers.go @@ -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 @@ -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() @@ -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) }) } @@ -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 } @@ -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