From b8a994bd78a5baa34aa446c486a73a79998a0000 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Fri, 4 Feb 2022 16:27:34 -0500 Subject: [PATCH] feat(plugin): Allow plugins to receive empty arg Empty arguments were being swallowed by the plugin parsing logic; this commit fixes that. This was not a big problem as empty arguments are probably quite rare. However, the shell completion logic does have a use case for an empty argument. Plugins using Cobra have a '__complete' command that can be called to obtain the valid completion choices. That command takes as arguments what the user has typed. When the user has not typed anything the command needs to be passed an explicit empty argument. For example: helm 2to3 __complete '' but since the empty argument is not being passed to the plugin we get a missing argument error because the __complete command requires at least one argument. Signed-off-by: Marc Khouzam --- cmd/helm/load_plugins.go | 4 +++- cmd/helm/plugin_test.go | 3 +++ cmd/helm/testdata/helmhome/helm/plugins/numargs/numargs.sh | 2 ++ cmd/helm/testdata/helmhome/helm/plugins/numargs/plugin.yaml | 4 ++++ cmd/helm/testdata/output/plugin_list_comp.txt | 1 + cmd/helm/testdata/output/plugin_repeat_comp.txt | 1 + 6 files changed, 14 insertions(+), 1 deletion(-) create mode 100755 cmd/helm/testdata/helmhome/helm/plugins/numargs/numargs.sh create mode 100644 cmd/helm/testdata/helmhome/helm/plugins/numargs/plugin.yaml diff --git a/cmd/helm/load_plugins.go b/cmd/helm/load_plugins.go index a97d49a93..a0c5fce66 100644 --- a/cmd/helm/load_plugins.go +++ b/cmd/helm/load_plugins.go @@ -170,7 +170,9 @@ func manuallyProcessArgs(args []string) ([]string, []string) { return v } } - return "" + // Return something different than what we received by adding a random prefix. + // We cannot simply return an empty string as v could be an empty string itself. + return fmt.Sprintf("x%s", v) } for i := 0; i < len(args); i++ { diff --git a/cmd/helm/plugin_test.go b/cmd/helm/plugin_test.go index 33de33522..6c5a0a2dc 100644 --- a/cmd/helm/plugin_test.go +++ b/cmd/helm/plugin_test.go @@ -116,6 +116,8 @@ func TestLoadPlugins(t *testing.T) { {"env", "env stuff", "show the env", "env\n", []string{}, 0}, {"exitwith", "exitwith code", "This exits with the specified exit code", "", []string{"2"}, 2}, {"fullenv", "show env vars", "show all env vars", envs + "\n", []string{}, 0}, + // Test that empty arguments are passed to the plugin + {"numargs", "echo num args", "echo number of arguments received", "3\n", []string{"-a", "", "-b"}, 0}, } plugins := cmd.Commands() @@ -196,6 +198,7 @@ func TestLoadPluginsForCompletion(t *testing.T) { {"more", []string{"one", "two"}, []string{"b", "ball"}, []staticCompletionDetails{}}, }}, }}, + {"numargs", []string{}, []string{}, []staticCompletionDetails{}}, } checkCommand(t, cmd.Commands(), tests) } diff --git a/cmd/helm/testdata/helmhome/helm/plugins/numargs/numargs.sh b/cmd/helm/testdata/helmhome/helm/plugins/numargs/numargs.sh new file mode 100755 index 000000000..11d0c56e8 --- /dev/null +++ b/cmd/helm/testdata/helmhome/helm/plugins/numargs/numargs.sh @@ -0,0 +1,2 @@ +#!/bin/bash +echo $# diff --git a/cmd/helm/testdata/helmhome/helm/plugins/numargs/plugin.yaml b/cmd/helm/testdata/helmhome/helm/plugins/numargs/plugin.yaml new file mode 100644 index 000000000..df9ab1b25 --- /dev/null +++ b/cmd/helm/testdata/helmhome/helm/plugins/numargs/plugin.yaml @@ -0,0 +1,4 @@ +name: numargs +usage: "echo num args" +description: "echo number of arguments received" +command: "$HELM_PLUGIN_DIR/numargs.sh" diff --git a/cmd/helm/testdata/output/plugin_list_comp.txt b/cmd/helm/testdata/output/plugin_list_comp.txt index 833efc5e9..96f2abe1f 100644 --- a/cmd/helm/testdata/output/plugin_list_comp.txt +++ b/cmd/helm/testdata/output/plugin_list_comp.txt @@ -3,5 +3,6 @@ echo echo stuff env env stuff exitwith exitwith code fullenv show env vars +numargs echo num args :4 Completion ended with directive: ShellCompDirectiveNoFileComp diff --git a/cmd/helm/testdata/output/plugin_repeat_comp.txt b/cmd/helm/testdata/output/plugin_repeat_comp.txt index 3fa05f0b3..b3dc44d83 100644 --- a/cmd/helm/testdata/output/plugin_repeat_comp.txt +++ b/cmd/helm/testdata/output/plugin_repeat_comp.txt @@ -2,5 +2,6 @@ echo echo stuff env env stuff exitwith exitwith code fullenv show env vars +numargs echo num args :4 Completion ended with directive: ShellCompDirectiveNoFileComp