diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 41ea6a3b4..c395d1f55 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -19,7 +19,6 @@ package main // import "k8s.io/helm/cmd/helm" import ( "errors" "fmt" - "io" "io/ioutil" "log" "os" @@ -45,9 +44,7 @@ var ( tlsKeyFile string // path to TLS key file tlsVerify bool // enable TLS and verify remote certificates tlsEnable bool // enable TLS -) -var ( kubeContext string tillerTunnel *kube.Tunnel settings helm_env.EnvSettings @@ -114,7 +111,7 @@ func initRootFlags(cmd *cobra.Command) { tlsKeyFile = os.ExpandEnv(tlsKeyFile) } -func newRootCmd(out io.Writer) *cobra.Command { +func newRootCmd() *cobra.Command { cmd := &cobra.Command{ Use: "helm", Short: "The Helm package manager for Kubernetes.", @@ -123,11 +120,12 @@ func newRootCmd(out io.Writer) *cobra.Command { PersistentPreRun: func(cmd *cobra.Command, _ []string) { initRootFlags(cmd) }, - PersistentPostRun: func(_ *cobra.Command, _ []string) { + PersistentPostRun: func(*cobra.Command, []string) { teardown() }, } addRootFlags(cmd) + out := cmd.OutOrStdout() cmd.AddCommand( // chart commands @@ -157,7 +155,7 @@ func newRootCmd(out io.Writer) *cobra.Command { addFlagsTLS(newVersionCmd(nil, out)), newCompletionCmd(out), - newHomeCmd(out), + newHomeCmd(), newInitCmd(out), newPluginCmd(out), @@ -180,7 +178,7 @@ func init() { } func main() { - cmd := newRootCmd(os.Stdout) + cmd := newRootCmd() if err := cmd.Execute(); err != nil { os.Exit(1) } diff --git a/cmd/helm/helm_test.go b/cmd/helm/helm_test.go index 8789ffd1f..910daa22a 100644 --- a/cmd/helm/helm_test.go +++ b/cmd/helm/helm_test.go @@ -23,6 +23,7 @@ import ( "io/ioutil" "math/rand" "os" + "path/filepath" "regexp" "sync" "testing" @@ -337,3 +338,77 @@ func ensureTestHome(home helmpath.Home, t *testing.T) error { t.Logf("$HELM_HOME has been configured at %s.\n", settings.Home.String()) return nil } + +func TestRootCmd(t *testing.T) { + oldhome := os.Getenv("HELM_HOME") + defer os.Setenv("HELM_HOME", oldhome) + + tests := []struct { + name string + args []string + envars map[string]string + home string + }{ + { + name: "defaults", + home: filepath.Join(os.Getenv("HOME"), "/.helm"), + }, + { + name: "with --home set", + args: []string{"--home", "/foo"}, + home: "/foo", + }, + { + name: "subcommands with --home set", + args: []string{"home", "--home", "/foo"}, + home: "/foo", + }, + { + name: "with $HELM_HOME set", + envars: map[string]string{"HELM_HOME": "/bar"}, + home: "/bar", + }, + { + name: "subcommands with $HELM_HOME set", + args: []string{"home"}, + envars: map[string]string{"HELM_HOME": "/bar"}, + home: "/bar", + }, + { + name: "with $HELM_HOME and --home set", + args: []string{"home", "--home", "/foo"}, + envars: map[string]string{"HELM_HOME": "/bar"}, + home: "/foo", + }, + } + + // ensure not set locally + os.Unsetenv("HELM_HOME") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer os.Unsetenv("HELM_HOME") + + for k, v := range tt.envars { + os.Setenv(k, v) + } + + cmd := newRootCmd() + cmd.SetOutput(ioutil.Discard) + cmd.SetArgs(tt.args) + cmd.Run = func(*cobra.Command, []string) {} + if err := cmd.Execute(); err != nil { + t.Errorf("unexpected error: %s", err) + } + + if settings.Home.String() != tt.home { + t.Errorf("expected home %q, got %q", tt.home, settings.Home) + } + homeFlag := cmd.Flag("home").Value.String() + homeFlag = os.ExpandEnv(homeFlag) + if homeFlag != tt.home { + t.Errorf("expected home %q, got %q", tt.home, homeFlag) + } + }) + } +} diff --git a/cmd/helm/home.go b/cmd/helm/home.go index 25809b521..abd7fb101 100644 --- a/cmd/helm/home.go +++ b/cmd/helm/home.go @@ -17,9 +17,6 @@ limitations under the License. package main import ( - "fmt" - "io" - "github.com/spf13/cobra" ) @@ -28,25 +25,24 @@ This command displays the location of HELM_HOME. This is where any helm configuration files live. ` -func newHomeCmd(out io.Writer) *cobra.Command { +func newHomeCmd() *cobra.Command { cmd := &cobra.Command{ Use: "home", Short: "displays the location of HELM_HOME", Long: longHomeHelp, Run: func(cmd *cobra.Command, args []string) { h := settings.Home - fmt.Fprintf(out, "%s\n", h) + cmd.Println(h) if settings.Debug { - fmt.Fprintf(out, "Repository: %s\n", h.Repository()) - fmt.Fprintf(out, "RepositoryFile: %s\n", h.RepositoryFile()) - fmt.Fprintf(out, "Cache: %s\n", h.Cache()) - fmt.Fprintf(out, "Stable CacheIndex: %s\n", h.CacheIndex("stable")) - fmt.Fprintf(out, "Starters: %s\n", h.Starters()) - fmt.Fprintf(out, "LocalRepository: %s\n", h.LocalRepository()) - fmt.Fprintf(out, "Plugins: %s\n", h.Plugins()) + cmd.Printf("Repository: %s\n", h.Repository()) + cmd.Printf("RepositoryFile: %s\n", h.RepositoryFile()) + cmd.Printf("Cache: %s\n", h.Cache()) + cmd.Printf("Stable CacheIndex: %s\n", h.CacheIndex("stable")) + cmd.Printf("Starters: %s\n", h.Starters()) + cmd.Printf("LocalRepository: %s\n", h.LocalRepository()) + cmd.Printf("Plugins: %s\n", h.Plugins()) } }, } - return cmd }