From 876cbc205c8d3a158a65db903b256596df2523e5 Mon Sep 17 00:00:00 2001
From: Adam Reese <adam@reese.io>
Date: Tue, 11 Jul 2017 14:26:25 -0700
Subject: [PATCH] fix(helm): support HELM_HOME during plugin loading

closes #2630
---
 cmd/helm/load_plugins.go | 11 +++++++++++
 cmd/helm/plugin_test.go  | 12 ++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/cmd/helm/load_plugins.go b/cmd/helm/load_plugins.go
index ee773cb4b..ba056bd8c 100644
--- a/cmd/helm/load_plugins.go
+++ b/cmd/helm/load_plugins.go
@@ -24,6 +24,7 @@ import (
 	"strings"
 
 	"github.com/spf13/cobra"
+	"github.com/spf13/pflag"
 
 	helm_env "k8s.io/helm/pkg/helm/environment"
 	"k8s.io/helm/pkg/plugin"
@@ -41,6 +42,16 @@ func loadPlugins(baseCmd *cobra.Command, out io.Writer) {
 		return
 	}
 
+	// manually handel processing of HELM_HOME and --home
+	helmHome := "$HOME/.helm"
+	if h, ok := os.LookupEnv("HELM_HOME"); ok {
+		helmHome = h
+	}
+
+	fs := pflag.NewFlagSet("homer", pflag.ContinueOnError)
+	fs.StringVar((*string)(&settings.Home), "home", helmHome, "location of your Helm config. Overrides $HELM_HOME")
+	fs.Parse(os.Args)
+
 	found, err := findPlugins(settings.PluginDirs())
 	if err != nil {
 		fmt.Fprintf(os.Stderr, "failed to load plugins: %s", err)
diff --git a/cmd/helm/plugin_test.go b/cmd/helm/plugin_test.go
index 77ef00c5d..335cd281e 100644
--- a/cmd/helm/plugin_test.go
+++ b/cmd/helm/plugin_test.go
@@ -64,12 +64,24 @@ func TestManuallyProcessArgs(t *testing.T) {
 
 }
 
+// resetEnv sets an env var, and returns a defer function to reset the env
+func resetEnv(name, val string) func() {
+	original, ok := os.LookupEnv(name)
+	os.Setenv(name, val)
+	if ok {
+		return func() { os.Setenv(name, original) }
+	}
+	return func() { os.Unsetenv(name) }
+}
+
 func TestLoadPlugins(t *testing.T) {
 	// Set helm home to point to testdata
 	old := settings.Home
 	settings.Home = "testdata/helmhome"
+	cleanup := resetEnv("HELM_HOME", settings.Home.String())
 	defer func() {
 		settings.Home = old
+		cleanup()
 	}()
 	hh := settings.Home
 
-- 
GitLab