Skip to content

Commit 72bb99c

Browse files
authored
Merge pull request #2545 from keboola/mvasko/PSGO-200-shell-completion
feat(cli): add shell completion support
2 parents f66edbf + 3b5049f commit 72bb99c

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

  • internal/pkg/service/cli/cmd

internal/pkg/service/cli/cmd/cmd.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func NewRootCommand(stdin io.Reader, stdout io.Writer, stderr io.Writer, osEnvs
118118
Use: "kbc", // name of the binary
119119
Version: version.Version(),
120120
Short: helpmsg.Read(`app`),
121-
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
121+
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
122122
SilenceUsage: true,
123123
SilenceErrors: true, // custom error handling, see printError
124124
RunE: func(cmd *cobra.Command, args []string) (cmdErr error) {
@@ -145,6 +145,12 @@ func NewRootCommand(stdin io.Reader, stdout io.Writer, stderr io.Writer, osEnvs
145145
// Init when flags are parsed
146146
p := &dependencies.ProviderRef{}
147147
root.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
148+
// Completion commands don't need the full dependency stack.
149+
// __complete is the hidden command invoked by the shell on every TAB press.
150+
if cmd.Name() == "__complete" || strings.HasPrefix(cmd.CommandPath(), "kbc completion") {
151+
return nil
152+
}
153+
148154
// Bind flags - without ENVs from files
149155
root.globalFlags = flag.DefaultGlobalFlags()
150156
err := cmdconfig.NewBinder(osEnvs, log.NewNopLogger()).Bind(cmd.Context(), cmd.Flags(), args, &root.globalFlags)

0 commit comments

Comments
 (0)