Skip to content

Commit

Permalink
feat(comp): Improve completion for plugin commands
Browse files Browse the repository at this point in the history
The 'plugin update' and 'plugin uninstall' commands can accept more than
one plugin name as arguments; this commit teaches the completion logic
to respect this.

Also, the commit adds go test for completion of the plugin commands.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
  • Loading branch information
marckhouzam committed Feb 24, 2021
1 parent 7b6dcfa commit 1f68f65
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 11 deletions.
31 changes: 28 additions & 3 deletions cmd/helm/plugin_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,37 @@ func newPluginListCmd(out io.Writer) *cobra.Command {
return cmd
}

// Returns all plugins from plugins, except those with names matching ignoredPluginNames
func filterPlugins(plugins []*plugin.Plugin, ignoredPluginNames []string) []*plugin.Plugin {
// if ignoredPluginNames is nil, just return plugins
if ignoredPluginNames == nil {
return plugins
}

var filteredPlugins []*plugin.Plugin
for _, plugin := range plugins {
found := false
for _, ignoredName := range ignoredPluginNames {
if plugin.Metadata.Name == ignoredName {
found = true
break
}
}
if !found {
filteredPlugins = append(filteredPlugins, plugin)
}
}

return filteredPlugins
}

// Provide dynamic auto-completion for plugin names
func compListPlugins(toComplete string) []string {
func compListPlugins(toComplete string, ignoredPluginNames []string) []string {
var pNames []string
plugins, err := plugin.FindPlugins(settings.PluginsDirectory)
if err == nil {
for _, p := range plugins {
if err == nil && len(plugins) > 0 {
filteredPlugins := filterPlugins(plugins, ignoredPluginNames)
for _, p := range filteredPlugins {
if strings.HasPrefix(p.Metadata.Name, toComplete) {
pNames = append(pNames, p.Metadata.Name)
}
Expand Down
44 changes: 44 additions & 0 deletions cmd/helm/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,50 @@ func TestLoadPlugins_HelmNoPlugins(t *testing.T) {
}
}

func TestPluginCmdsCompletion(t *testing.T) {

tests := []cmdTestCase{{
name: "completion for plugin update",
cmd: "__complete plugin update ''",
golden: "output/plugin_list_comp.txt",
rels: []*release.Release{},
}, {
name: "completion for plugin update repetition",
cmd: "__complete plugin update args ''",
golden: "output/plugin_repeat_comp.txt",
rels: []*release.Release{},
}, {
name: "completion for plugin uninstall",
cmd: "__complete plugin uninstall ''",
golden: "output/plugin_list_comp.txt",
rels: []*release.Release{},
}, {
name: "completion for plugin uninstall repetition",
cmd: "__complete plugin uninstall args ''",
golden: "output/plugin_repeat_comp.txt",
rels: []*release.Release{},
}, {
name: "completion for plugin list",
cmd: "__complete plugin list ''",
golden: "output/empty_nofile_comp.txt",
rels: []*release.Release{},
}, {
name: "completion for plugin install no args",
cmd: "__complete plugin install ''",
golden: "output/empty_default_comp.txt",
rels: []*release.Release{},
}, {
name: "completion for plugin install one arg",
cmd: "__complete plugin list /tmp ''",
golden: "output/empty_nofile_comp.txt",
rels: []*release.Release{},
}, {}}
for _, test := range tests {
settings.PluginsDirectory = "testdata/helmhome/helm/plugins"
runTestCmd(t, []cmdTestCase{test})
}
}

func TestPluginFileCompletion(t *testing.T) {
checkFileCompletion(t, "plugin", false)
}
Expand Down
5 changes: 1 addition & 4 deletions cmd/helm/plugin_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ func newPluginUninstallCmd(out io.Writer) *cobra.Command {
Aliases: []string{"rm", "remove"},
Short: "uninstall one or more Helm plugins",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return compListPlugins(toComplete), cobra.ShellCompDirectiveNoFileComp
return compListPlugins(toComplete, args), cobra.ShellCompDirectiveNoFileComp
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return o.complete(args)
Expand Down
5 changes: 1 addition & 4 deletions cmd/helm/plugin_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ func newPluginUpdateCmd(out io.Writer) *cobra.Command {
Aliases: []string{"up"},
Short: "update one or more Helm plugins",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return compListPlugins(toComplete), cobra.ShellCompDirectiveNoFileComp
return compListPlugins(toComplete, args), cobra.ShellCompDirectiveNoFileComp
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return o.complete(args)
Expand Down
2 changes: 2 additions & 0 deletions cmd/helm/testdata/output/empty_default_comp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:0
Completion ended with directive: ShellCompDirectiveDefault
2 changes: 2 additions & 0 deletions cmd/helm/testdata/output/empty_nofile_comp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
7 changes: 7 additions & 0 deletions cmd/helm/testdata/output/plugin_list_comp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
args
echo
env
exitwith
fullenv
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
6 changes: 6 additions & 0 deletions cmd/helm/testdata/output/plugin_repeat_comp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo
env
exitwith
fullenv
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

0 comments on commit 1f68f65

Please sign in to comment.