Skip to content

Commit

Permalink
Merge pull request helm#13261 from marckhouzam/feat/activeHelpNoComp
Browse files Browse the repository at this point in the history
ActiveHelp for cmds that don't take any more args
  • Loading branch information
mattfarina authored Aug 20, 2024
2 parents 47c8cde + c92cc07 commit 7ca545b
Show file tree
Hide file tree
Showing 30 changed files with 57 additions and 42 deletions.
22 changes: 15 additions & 7 deletions cmd/helm/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
Short: "generate autocompletion script for bash",
Long: bashCompDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
return runCompletionBash(out, cmd)
},
Expand All @@ -114,7 +114,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
Short: "generate autocompletion script for zsh",
Long: zshCompDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
return runCompletionZsh(out, cmd)
},
Expand All @@ -126,7 +126,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
Short: "generate autocompletion script for fish",
Long: fishCompDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
return runCompletionFish(out, cmd)
},
Expand All @@ -138,7 +138,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
Short: "generate autocompletion script for powershell",
Long: powershellCompDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
return runCompletionPowershell(out, cmd)
},
Expand Down Expand Up @@ -209,7 +209,15 @@ func runCompletionPowershell(out io.Writer, cmd *cobra.Command) error {
return cmd.Root().GenPowerShellCompletionWithDesc(out)
}

// Function to disable file completion
func noCompletions(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
// noMoreArgsCompFunc deactivates file completion when doing argument shell completion.
// It also provides some ActiveHelp to indicate no more arguments are accepted.
func noMoreArgsCompFunc(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return noMoreArgsComp()
}

// noMoreArgsComp deactivates file completion when doing argument shell completion.
// It also provides some ActiveHelp to indicate no more arguments are accepted.
func noMoreArgsComp() ([]string, cobra.ShellCompDirective) {
activeHelpMsg := "This command does not take any more arguments (but may accept flags)."
return cobra.AppendActiveHelp(nil, activeHelpMsg), cobra.ShellCompDirectiveNoFileComp
}
2 changes: 1 addition & 1 deletion cmd/helm/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func newCreateCmd(out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveDefault
}
// No more completions, so disable file completion
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
o.name = args[0]
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newDocsCmd(out io.Writer) *cobra.Command {
Long: docsDesc,
Hidden: true,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
o.topCmd = cmd.Root()
return o.run(out)
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newEnvCmd(out io.Writer) *cobra.Command {
return keys, cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
Run: func(_ *cobra.Command, args []string) {
envVars := settings.EnvVars()
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/get_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newGetAllCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/get_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newGetHooksCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/get_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newGetManifestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/get_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newGetMetadataCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/get_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newGetNotesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/get_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func newHistoryCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Long: listHelp,
Aliases: []string{"ls"},
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(cmd *cobra.Command, _ []string) error {
if client.AllNamespaces {
if err := cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/plugin_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newPluginInstallCmd(out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveDefault
}
// No more completion once the plugin path has been specified
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
PreRunE: func(_ *cobra.Command, args []string) error {
return o.complete(args)
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/plugin_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newPluginListCmd(out io.Writer) *cobra.Command {
Use: "list",
Aliases: []string{"ls"},
Short: "list installed Helm plugins",
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(_ *cobra.Command, _ []string) error {
debug("pluginDirs: %s", settings.PluginsDirectory)
plugins, err := plugin.FindPlugins(settings.PluginsDirectory)
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func newPushCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
}
return comps, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
registryClient, err := newRegistryClient(o.certFile, o.keyFile, o.caFile, o.insecureSkipTLSverify, o.plainHTTP)
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/registry_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newRegistryLoginCmd(cfg *action.Configuration, out io.Writer) *cobra.Comman
Short: "login to a registry",
Long: registryLoginDesc,
Args: require.MinimumNArgs(1),
ValidArgsFunction: noCompletions,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(_ *cobra.Command, args []string) error {
hostname := args[0]

Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/registry_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newRegistryLogoutCmd(cfg *action.Configuration, out io.Writer) *cobra.Comma
Short: "logout from a registry",
Long: registryLogoutDesc,
Args: require.MinimumNArgs(1),
ValidArgsFunction: noCompletions,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(_ *cobra.Command, args []string) error {
hostname := args[0]
return action.NewRegistryLogout(cfg).Run(out, hostname)
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/release_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
13 changes: 9 additions & 4 deletions cmd/helm/repo_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ func newRepoAddCmd(out io.Writer) *cobra.Command {
o := &repoAddOptions{}

cmd := &cobra.Command{
Use: "add [NAME] [URL]",
Short: "add a chart repository",
Args: require.ExactArgs(2),
ValidArgsFunction: noCompletions,
Use: "add [NAME] [URL]",
Short: "add a chart repository",
Args: require.ExactArgs(2),
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if len(args) > 1 {
return noMoreArgsComp()
}
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(_ *cobra.Command, args []string) error {
o.name = args[0]
o.url = args[1]
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/repo_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newRepoIndexCmd(out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveDefault
}
// No more completions, so disable file completion
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
o.dir = args[0]
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newRepoListCmd(out io.Writer) *cobra.Command {
Aliases: []string{"ls"},
Short: "list chart repositories",
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(_ *cobra.Command, _ []string) error {
f, _ := repo.LoadFile(settings.RepositoryConfig)
if len(f.Repositories) == 0 && !(outfmt == output.JSON || outfmt == output.YAML) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func newRollbackCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
return compListRevisions(toComplete, cfg, args[0])
}

return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
if len(args) > 1 {
Expand Down
13 changes: 6 additions & 7 deletions cmd/helm/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client := action.NewShowWithConfig(action.ShowAll, cfg)

showCommand := &cobra.Command{
Use: "show",
Short: "show information of a chart",
Aliases: []string{"inspect"},
Long: showDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions, // Disable file completion
Use: "show",
Short: "show information of a chart",
Aliases: []string{"inspect"},
Long: showDesc,
Args: require.NoArgs,
}

// Function providing dynamic auto-completion
validArgsFunc := func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListCharts(toComplete, true)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newStatusCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
}
return compListReleases(toComplete, args, cfg)
},
Expand Down
1 change: 1 addition & 0 deletions cmd/helm/testdata/output/empty_nofile_comp.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
_activeHelp_ This command does not take any more arguments (but may accept flags).
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
1 change: 1 addition & 0 deletions cmd/helm/testdata/output/rollback-wrong-args-comp.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
_activeHelp_ This command does not take any more arguments (but may accept flags).
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
1 change: 1 addition & 0 deletions cmd/helm/testdata/output/status-wrong-args-comp.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
_activeHelp_ This command does not take any more arguments (but may accept flags).
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
2 changes: 1 addition & 1 deletion cmd/helm/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if len(args) == 1 {
return compListCharts(toComplete, true)
}
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
client.Namespace = settings.Namespace()
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newVerifyCmd(out io.Writer) *cobra.Command {
return nil, cobra.ShellCompDirectiveDefault
}
// No more completions, so disable file completion
return nil, cobra.ShellCompDirectiveNoFileComp
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
err := client.Run(args[0])
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func newVersionCmd(out io.Writer) *cobra.Command {
Short: "print the client version information",
Long: versionDesc,
Args: require.NoArgs,
ValidArgsFunction: noCompletions,
ValidArgsFunction: noMoreArgsCompFunc,
RunE: func(_ *cobra.Command, _ []string) error {
return o.run(out)
},
Expand Down

0 comments on commit 7ca545b

Please sign in to comment.