Skip to content

Commit

Permalink
🐛 fix -h/--help for all attached subcommands (#1774)
Browse files Browse the repository at this point in the history
This fixes a regression in v9

Before:


![image](https://github.com/mondoohq/cnquery/assets/1307529/c4d1f8dd-8a3a-4d8a-91a3-42c529a5fde9)

After:


![image](https://github.com/mondoohq/cnquery/assets/1307529/a1c6e81a-cd69-4047-964e-c2bf87985825)

Works for `run`, `shell`, and `scan` now

Here is the same thing when the provider is not yet installed:


![image](https://github.com/mondoohq/cnquery/assets/1307529/a7bb6c1c-f509-4d96-b5d0-2d4dec7c9ada)

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
  • Loading branch information
arlimus authored Sep 18, 2023
1 parent 326a025 commit 5a90a90
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions cli/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.mondoo.com/cnquery/cli/components"
"go.mondoo.com/cnquery/cli/config"
"go.mondoo.com/cnquery/llx"
"go.mondoo.com/cnquery/providers"
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
Expand Down Expand Up @@ -53,44 +52,51 @@ func AttachCLIs(rootCmd *cobra.Command, commands ...*Command) error {

func detectConnectorName(args []string, commands []*Command) (string, bool) {
autoUpdate := true
connector := ""

cmds := make(map[string]struct{}, len(commands))
flags := pflag.NewFlagSet("set", pflag.ContinueOnError)
flags.Bool("auto-update", true, "")
flags.BoolP("help", "h", false, "")
for i := range commands {
cmds[commands[i].Command.Use] = struct{}{}
cmd := commands[i]
cmd.Command.Flags().VisitAll(func(flag *pflag.Flag) {
if found := flags.Lookup(flag.Name); found == nil {
flags.AddFlag(flag)
}
})
}

preRunRoot := &cobra.Command{
Use: "root",
Run: func(cmd *cobra.Command, args []string) {
autoUpdate = viper.GetBool("auto_update")
for i, arg := range args {
if _, ok := cmds[arg]; ok {
if len(args) == i+1 {
connector = "local"
} else {
connector = args[i+1]
}
return
}
}
},
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
err := flags.Parse(args)
if err != nil {
log.Warn().Err(err).Msg("pre-processing of cli had an error")
}

preRunRoot.Flags().Bool("auto-update", true, "Enable automatic provider installation and update")
viper.BindPFlag("auto_update", preRunRoot.Flags().Lookup("auto-update"))
config.Init(preRunRoot)
autoUpdate, _ = flags.GetBool("auto-update")

err := preRunRoot.Execute()
if err != nil {
log.Debug().Err(err).Msg("early detection error")
log.Error().Msg("failed to run early command detection")
return "", false
remaining := flags.Args()
if len(remaining) <= 1 {
return "", autoUpdate
}

commandFound := false
for j := range commands {
if commands[j].Command.Use == remaining[1] {
commandFound = true
break
}
}
if !commandFound {
return "", autoUpdate
}

// 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 {
return "local", autoUpdate
}

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

return connector, autoUpdate
}

Expand Down

0 comments on commit 5a90a90

Please sign in to comment.