From 10d4d2ed999a403e2464f673e0e19b95ee4e1ac6 Mon Sep 17 00:00:00 2001 From: rudeigerc Date: Sat, 22 Aug 2020 02:30:22 +0800 Subject: [PATCH 1/2] feat(env): add support of accepting a specific variable for helm env Signed-off-by: Yuchen Cheng --- cmd/helm/env.go | 48 +++++++++++++++++++-------- cmd/helm/env_test.go | 9 +++++ cmd/helm/testdata/output/env-comp.txt | 16 +++++++++ 3 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 cmd/helm/testdata/output/env-comp.txt diff --git a/cmd/helm/env.go b/cmd/helm/env.go index 4db3d80de..3754b748d 100644 --- a/cmd/helm/env.go +++ b/cmd/helm/env.go @@ -32,25 +32,45 @@ Env prints out all the environment information in use by Helm. func newEnvCmd(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "env", - Short: "helm client environment information", - Long: envHelp, - Args: require.NoArgs, - ValidArgsFunction: noCompletions, + Use: "env", + Short: "helm client environment information", + Long: envHelp, + Args: require.MaximumNArgs(1), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) == 0 { + keys := getSortedEnvVarKeys() + return keys, cobra.ShellCompDirectiveNoFileComp + } + + return nil, cobra.ShellCompDirectiveNoFileComp + }, Run: func(cmd *cobra.Command, args []string) { envVars := settings.EnvVars() - // Sort the variables by alphabetical order. - // This allows for a constant output across calls to 'helm env'. - var keys []string - for k := range envVars { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - fmt.Fprintf(out, "%s=\"%s\"\n", k, envVars[k]) + if len(args) == 0 { + // Sort the variables by alphabetical order. + // This allows for a constant output across calls to 'helm env'. + keys := getSortedEnvVarKeys() + + for _, k := range keys { + fmt.Fprintf(out, "%s=\"%s\"\n", k, envVars[k]) + } + } else { + fmt.Fprintf(out, "%s\n", envVars[args[0]]) } }, } return cmd } + +func getSortedEnvVarKeys() []string { + envVars := settings.EnvVars() + + var keys []string + for k := range envVars { + keys = append(keys, k) + } + sort.Strings(keys) + + return keys +} diff --git a/cmd/helm/env_test.go b/cmd/helm/env_test.go index a7170a8cc..abdc9c94e 100644 --- a/cmd/helm/env_test.go +++ b/cmd/helm/env_test.go @@ -20,6 +20,15 @@ import ( "testing" ) +func TestEnv(t *testing.T) { + tests := []cmdTestCase{{ + name: "completion for env", + cmd: "__complete env ''", + golden: "output/env-comp.txt", + }} + runTestCmd(t, tests) +} + func TestEnvFileCompletion(t *testing.T) { checkFileCompletion(t, "env", false) } diff --git a/cmd/helm/testdata/output/env-comp.txt b/cmd/helm/testdata/output/env-comp.txt new file mode 100644 index 000000000..c4b46ae6b --- /dev/null +++ b/cmd/helm/testdata/output/env-comp.txt @@ -0,0 +1,16 @@ +HELM_BIN +HELM_CACHE_HOME +HELM_CONFIG_HOME +HELM_DATA_HOME +HELM_DEBUG +HELM_KUBEAPISERVER +HELM_KUBECONTEXT +HELM_KUBETOKEN +HELM_MAX_HISTORY +HELM_NAMESPACE +HELM_PLUGINS +HELM_REGISTRY_CONFIG +HELM_REPOSITORY_CACHE +HELM_REPOSITORY_CONFIG +:4 +Completion ended with directive: ShellCompDirectiveNoFileComp From 195d7a650712d8eee2db887bf8e1a4e2c363328b Mon Sep 17 00:00:00 2001 From: Yuchen Cheng Date: Sat, 22 Aug 2020 22:04:09 +0800 Subject: [PATCH 2/2] add checkFileCompletion for env HELM_BIN Signed-off-by: Yuchen Cheng --- cmd/helm/env_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/helm/env_test.go b/cmd/helm/env_test.go index abdc9c94e..01ef25933 100644 --- a/cmd/helm/env_test.go +++ b/cmd/helm/env_test.go @@ -31,4 +31,5 @@ func TestEnv(t *testing.T) { func TestEnvFileCompletion(t *testing.T) { checkFileCompletion(t, "env", false) + checkFileCompletion(t, "env HELM_BIN", false) }