Repository: helm/helm (go)

Issue:
helm env accept a specific variable to return just that value
The `helm env` command returns a list of the Helm environment variables and their values. It would be useful if you could pass in a specific environment variable and get its value.




Pull request description:
Add support of accepting a specific variable for helm env
Signed-off-by: Yuchen Cheng <rudeigerc@gmail.com>

<!--  Thanks for sending a pull request!  Here are some tips for you:
1. Make sure to read the Contributing Guide before submitting your PR: https://github.com/helm/helm/blob/master/CONTRIBUTING.md
2. If this PR closes another issue, add 'closes #<issue number>' somewhere in the PR summary. GitHub will automatically close that issue when this PR gets merged. Alternatively, adding 'refs #<issue number>' will not close the issue, but help provide the reviewer more context.-->

**What this PR does / why we need it**:

Closes #8635 

Add support of accepting a specific variable for `helm env`.

```
$ bin/helm env HELM_BIN
bin/helm
```

**Special notes for your reviewer**:

**If applicable**:
- [ ] this PR contains documentation
- [x] this PR contains unit tests
- [x] this PR has been tested for backwards compatibility


Patch:
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
+}


Test patch:
diff --git a/cmd/helm/env_test.go b/cmd/helm/env_test.go
index a7170a8cc..01ef25933 100644
--- a/cmd/helm/env_test.go
+++ b/cmd/helm/env_test.go
@@ -20,6 +20,16 @@ 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)
+	checkFileCompletion(t, "env HELM_BIN", 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