Skip to content

Commit c92cc07

Browse files
committedAug 19, 2024·
ActiveHelp for cmds that don't take any more args
The result is that when doing shell completion (bash and zsh only), instead of getting no suggestions for commands that take no more arguments, the user will instead be shown an informative message: $ helm list <TAB> This command does not take any more arguments (but may accept flags). Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
1 parent 109dbe7 commit c92cc07

30 files changed

+57
-42
lines changed
 

‎cmd/helm/completion.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
102102
Short: "generate autocompletion script for bash",
103103
Long: bashCompDesc,
104104
Args: require.NoArgs,
105-
ValidArgsFunction: noCompletions,
105+
ValidArgsFunction: noMoreArgsCompFunc,
106106
RunE: func(cmd *cobra.Command, _ []string) error {
107107
return runCompletionBash(out, cmd)
108108
},
@@ -114,7 +114,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
114114
Short: "generate autocompletion script for zsh",
115115
Long: zshCompDesc,
116116
Args: require.NoArgs,
117-
ValidArgsFunction: noCompletions,
117+
ValidArgsFunction: noMoreArgsCompFunc,
118118
RunE: func(cmd *cobra.Command, _ []string) error {
119119
return runCompletionZsh(out, cmd)
120120
},
@@ -126,7 +126,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
126126
Short: "generate autocompletion script for fish",
127127
Long: fishCompDesc,
128128
Args: require.NoArgs,
129-
ValidArgsFunction: noCompletions,
129+
ValidArgsFunction: noMoreArgsCompFunc,
130130
RunE: func(cmd *cobra.Command, _ []string) error {
131131
return runCompletionFish(out, cmd)
132132
},
@@ -138,7 +138,7 @@ func newCompletionCmd(out io.Writer) *cobra.Command {
138138
Short: "generate autocompletion script for powershell",
139139
Long: powershellCompDesc,
140140
Args: require.NoArgs,
141-
ValidArgsFunction: noCompletions,
141+
ValidArgsFunction: noMoreArgsCompFunc,
142142
RunE: func(cmd *cobra.Command, _ []string) error {
143143
return runCompletionPowershell(out, cmd)
144144
},
@@ -209,7 +209,15 @@ func runCompletionPowershell(out io.Writer, cmd *cobra.Command) error {
209209
return cmd.Root().GenPowerShellCompletionWithDesc(out)
210210
}
211211

212-
// Function to disable file completion
213-
func noCompletions(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
214-
return nil, cobra.ShellCompDirectiveNoFileComp
212+
// noMoreArgsCompFunc deactivates file completion when doing argument shell completion.
213+
// It also provides some ActiveHelp to indicate no more arguments are accepted.
214+
func noMoreArgsCompFunc(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
215+
return noMoreArgsComp()
216+
}
217+
218+
// noMoreArgsComp deactivates file completion when doing argument shell completion.
219+
// It also provides some ActiveHelp to indicate no more arguments are accepted.
220+
func noMoreArgsComp() ([]string, cobra.ShellCompDirective) {
221+
activeHelpMsg := "This command does not take any more arguments (but may accept flags)."
222+
return cobra.AppendActiveHelp(nil, activeHelpMsg), cobra.ShellCompDirectiveNoFileComp
215223
}

‎cmd/helm/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func newCreateCmd(out io.Writer) *cobra.Command {
7171
return nil, cobra.ShellCompDirectiveDefault
7272
}
7373
// No more completions, so disable file completion
74-
return nil, cobra.ShellCompDirectiveNoFileComp
74+
return noMoreArgsComp()
7575
},
7676
RunE: func(_ *cobra.Command, args []string) error {
7777
o.name = args[0]

‎cmd/helm/docs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newDocsCmd(out io.Writer) *cobra.Command {
5858
Long: docsDesc,
5959
Hidden: true,
6060
Args: require.NoArgs,
61-
ValidArgsFunction: noCompletions,
61+
ValidArgsFunction: noMoreArgsCompFunc,
6262
RunE: func(cmd *cobra.Command, _ []string) error {
6363
o.topCmd = cmd.Root()
6464
return o.run(out)

‎cmd/helm/env.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func newEnvCmd(out io.Writer) *cobra.Command {
4242
return keys, cobra.ShellCompDirectiveNoFileComp
4343
}
4444

45-
return nil, cobra.ShellCompDirectiveNoFileComp
45+
return noMoreArgsComp()
4646
},
4747
Run: func(_ *cobra.Command, args []string) {
4848
envVars := settings.EnvVars()

‎cmd/helm/get_all.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func newGetAllCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
4343
Args: require.ExactArgs(1),
4444
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4545
if len(args) != 0 {
46-
return nil, cobra.ShellCompDirectiveNoFileComp
46+
return noMoreArgsComp()
4747
}
4848
return compListReleases(toComplete, args, cfg)
4949
},

‎cmd/helm/get_hooks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func newGetHooksCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
4343
Args: require.ExactArgs(1),
4444
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4545
if len(args) != 0 {
46-
return nil, cobra.ShellCompDirectiveNoFileComp
46+
return noMoreArgsComp()
4747
}
4848
return compListReleases(toComplete, args, cfg)
4949
},

‎cmd/helm/get_manifest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func newGetManifestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
4545
Args: require.ExactArgs(1),
4646
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4747
if len(args) != 0 {
48-
return nil, cobra.ShellCompDirectiveNoFileComp
48+
return noMoreArgsComp()
4949
}
5050
return compListReleases(toComplete, args, cfg)
5151
},

‎cmd/helm/get_metadata.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func newGetMetadataCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
4242
Args: require.ExactArgs(1),
4343
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4444
if len(args) != 0 {
45-
return nil, cobra.ShellCompDirectiveNoFileComp
45+
return noMoreArgsComp()
4646
}
4747
return compListReleases(toComplete, args, cfg)
4848
},

‎cmd/helm/get_notes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func newGetNotesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
4141
Args: require.ExactArgs(1),
4242
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4343
if len(args) != 0 {
44-
return nil, cobra.ShellCompDirectiveNoFileComp
44+
return noMoreArgsComp()
4545
}
4646
return compListReleases(toComplete, args, cfg)
4747
},

‎cmd/helm/get_values.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func newGetValuesCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
4848
Args: require.ExactArgs(1),
4949
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
5050
if len(args) != 0 {
51-
return nil, cobra.ShellCompDirectiveNoFileComp
51+
return noMoreArgsComp()
5252
}
5353
return compListReleases(toComplete, args, cfg)
5454
},

‎cmd/helm/history.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func newHistoryCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
6262
Args: require.ExactArgs(1),
6363
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
6464
if len(args) != 0 {
65-
return nil, cobra.ShellCompDirectiveNoFileComp
65+
return noMoreArgsComp()
6666
}
6767
return compListReleases(toComplete, args, cfg)
6868
},

‎cmd/helm/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
6868
Long: listHelp,
6969
Aliases: []string{"ls"},
7070
Args: require.NoArgs,
71-
ValidArgsFunction: noCompletions,
71+
ValidArgsFunction: noMoreArgsCompFunc,
7272
RunE: func(cmd *cobra.Command, _ []string) error {
7373
if client.AllNamespaces {
7474
if err := cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug); err != nil {

‎cmd/helm/plugin_install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func newPluginInstallCmd(out io.Writer) *cobra.Command {
5050
return nil, cobra.ShellCompDirectiveDefault
5151
}
5252
// No more completion once the plugin path has been specified
53-
return nil, cobra.ShellCompDirectiveNoFileComp
53+
return noMoreArgsComp()
5454
},
5555
PreRunE: func(_ *cobra.Command, args []string) error {
5656
return o.complete(args)

‎cmd/helm/plugin_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func newPluginListCmd(out io.Writer) *cobra.Command {
3030
Use: "list",
3131
Aliases: []string{"ls"},
3232
Short: "list installed Helm plugins",
33-
ValidArgsFunction: noCompletions,
33+
ValidArgsFunction: noMoreArgsCompFunc,
3434
RunE: func(_ *cobra.Command, _ []string) error {
3535
debug("pluginDirs: %s", settings.PluginsDirectory)
3636
plugins, err := plugin.FindPlugins(settings.PluginsDirectory)

‎cmd/helm/push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func newPushCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
6565
}
6666
return comps, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
6767
}
68-
return nil, cobra.ShellCompDirectiveNoFileComp
68+
return noMoreArgsComp()
6969
},
7070
RunE: func(_ *cobra.Command, args []string) error {
7171
registryClient, err := newRegistryClient(o.certFile, o.keyFile, o.caFile, o.insecureSkipTLSverify, o.plainHTTP)

‎cmd/helm/registry_login.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func newRegistryLoginCmd(cfg *action.Configuration, out io.Writer) *cobra.Comman
5353
Short: "login to a registry",
5454
Long: registryLoginDesc,
5555
Args: require.MinimumNArgs(1),
56-
ValidArgsFunction: noCompletions,
56+
ValidArgsFunction: cobra.NoFileCompletions,
5757
RunE: func(_ *cobra.Command, args []string) error {
5858
hostname := args[0]
5959

‎cmd/helm/registry_logout.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func newRegistryLogoutCmd(cfg *action.Configuration, out io.Writer) *cobra.Comma
3535
Short: "logout from a registry",
3636
Long: registryLogoutDesc,
3737
Args: require.MinimumNArgs(1),
38-
ValidArgsFunction: noCompletions,
38+
ValidArgsFunction: cobra.NoFileCompletions,
3939
RunE: func(_ *cobra.Command, args []string) error {
4040
hostname := args[0]
4141
return action.NewRegistryLogout(cfg).Run(out, hostname)

‎cmd/helm/release_testing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
5050
Args: require.ExactArgs(1),
5151
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
5252
if len(args) != 0 {
53-
return nil, cobra.ShellCompDirectiveNoFileComp
53+
return noMoreArgsComp()
5454
}
5555
return compListReleases(toComplete, args, cfg)
5656
},

‎cmd/helm/repo_add.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ func newRepoAddCmd(out io.Writer) *cobra.Command {
6868
o := &repoAddOptions{}
6969

7070
cmd := &cobra.Command{
71-
Use: "add [NAME] [URL]",
72-
Short: "add a chart repository",
73-
Args: require.ExactArgs(2),
74-
ValidArgsFunction: noCompletions,
71+
Use: "add [NAME] [URL]",
72+
Short: "add a chart repository",
73+
Args: require.ExactArgs(2),
74+
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
75+
if len(args) > 1 {
76+
return noMoreArgsComp()
77+
}
78+
return nil, cobra.ShellCompDirectiveNoFileComp
79+
},
7580
RunE: func(_ *cobra.Command, args []string) error {
7681
o.name = args[0]
7782
o.url = args[1]

‎cmd/helm/repo_index.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func newRepoIndexCmd(out io.Writer) *cobra.Command {
6060
return nil, cobra.ShellCompDirectiveDefault
6161
}
6262
// No more completions, so disable file completion
63-
return nil, cobra.ShellCompDirectiveNoFileComp
63+
return noMoreArgsComp()
6464
},
6565
RunE: func(_ *cobra.Command, args []string) error {
6666
o.dir = args[0]

‎cmd/helm/repo_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func newRepoListCmd(out io.Writer) *cobra.Command {
3636
Aliases: []string{"ls"},
3737
Short: "list chart repositories",
3838
Args: require.NoArgs,
39-
ValidArgsFunction: noCompletions,
39+
ValidArgsFunction: noMoreArgsCompFunc,
4040
RunE: func(_ *cobra.Command, _ []string) error {
4141
f, _ := repo.LoadFile(settings.RepositoryConfig)
4242
if len(f.Repositories) == 0 && !(outfmt == output.JSON || outfmt == output.YAML) {

‎cmd/helm/rollback.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func newRollbackCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
5555
return compListRevisions(toComplete, cfg, args[0])
5656
}
5757

58-
return nil, cobra.ShellCompDirectiveNoFileComp
58+
return noMoreArgsComp()
5959
},
6060
RunE: func(_ *cobra.Command, args []string) error {
6161
if len(args) > 1 {

‎cmd/helm/show.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,17 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
6060
client := action.NewShowWithConfig(action.ShowAll, cfg)
6161

6262
showCommand := &cobra.Command{
63-
Use: "show",
64-
Short: "show information of a chart",
65-
Aliases: []string{"inspect"},
66-
Long: showDesc,
67-
Args: require.NoArgs,
68-
ValidArgsFunction: noCompletions, // Disable file completion
63+
Use: "show",
64+
Short: "show information of a chart",
65+
Aliases: []string{"inspect"},
66+
Long: showDesc,
67+
Args: require.NoArgs,
6968
}
7069

7170
// Function providing dynamic auto-completion
7271
validArgsFunc := func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
7372
if len(args) != 0 {
74-
return nil, cobra.ShellCompDirectiveNoFileComp
73+
return noMoreArgsComp()
7574
}
7675
return compListCharts(toComplete, true)
7776
}

‎cmd/helm/status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func newStatusCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
6060
Args: require.ExactArgs(1),
6161
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
6262
if len(args) != 0 {
63-
return nil, cobra.ShellCompDirectiveNoFileComp
63+
return noMoreArgsComp()
6464
}
6565
return compListReleases(toComplete, args, cfg)
6666
},
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
_activeHelp_ This command does not take any more arguments (but may accept flags).
12
:4
23
Completion ended with directive: ShellCompDirectiveNoFileComp
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
_activeHelp_ This command does not take any more arguments (but may accept flags).
12
:4
23
Completion ended with directive: ShellCompDirectiveNoFileComp
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
_activeHelp_ This command does not take any more arguments (but may accept flags).
12
:4
23
Completion ended with directive: ShellCompDirectiveNoFileComp

‎cmd/helm/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
9797
if len(args) == 1 {
9898
return compListCharts(toComplete, true)
9999
}
100-
return nil, cobra.ShellCompDirectiveNoFileComp
100+
return noMoreArgsComp()
101101
},
102102
RunE: func(_ *cobra.Command, args []string) error {
103103
client.Namespace = settings.Namespace()

‎cmd/helm/verify.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func newVerifyCmd(out io.Writer) *cobra.Command {
5050
return nil, cobra.ShellCompDirectiveDefault
5151
}
5252
// No more completions, so disable file completion
53-
return nil, cobra.ShellCompDirectiveNoFileComp
53+
return noMoreArgsComp()
5454
},
5555
RunE: func(_ *cobra.Command, args []string) error {
5656
err := client.Run(args[0])

‎cmd/helm/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func newVersionCmd(out io.Writer) *cobra.Command {
6565
Short: "print the client version information",
6666
Long: versionDesc,
6767
Args: require.NoArgs,
68-
ValidArgsFunction: noCompletions,
68+
ValidArgsFunction: noMoreArgsCompFunc,
6969
RunE: func(_ *cobra.Command, _ []string) error {
7070
return o.run(out)
7171
},

0 commit comments

Comments
 (0)
Please sign in to comment.