From 0cc1d31ac2e01492762b9e58510d4fe9a60ebf9e Mon Sep 17 00:00:00 2001 From: Vibhav Bobade Date: Sat, 17 Aug 2019 11:13:24 +0530 Subject: [PATCH 1/3] Logic for the helm env command Signed-off-by: Vibhav Bobade --- cmd/helm/env.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/helm/root.go | 1 + 2 files changed, 103 insertions(+) create mode 100644 cmd/helm/env.go diff --git a/cmd/helm/env.go b/cmd/helm/env.go new file mode 100644 index 000000000..5b07462ff --- /dev/null +++ b/cmd/helm/env.go @@ -0,0 +1,102 @@ +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + "io" + "os" + "strings" + + "helm.sh/helm/pkg/helmpath" + "helm.sh/helm/pkg/helmpath/xdg" + + "helm.sh/helm/pkg/cli" + + "helm.sh/helm/pkg/plugin" + + "github.com/spf13/cobra" + + "helm.sh/helm/cmd/helm/require" +) + +var ( + envHelp = ` +Env prints out all the environment information in use by Helm. +` +) + +func newEnvCmd(out io.Writer) *cobra.Command { + o := &envOptions{} + + cmd := &cobra.Command{ + Use: "env", + Short: "environment information in use the Helm client", + Long: envHelp, + Args: require.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return o.run(out) + }, + } + + return cmd +} + +type envOptions struct { +} + +// run . +func (o *envOptions) run(out io.Writer) error { + plugin.SetupPluginEnv(&cli.EnvSettings{}, "", "") + o.setXdgPaths() + o.setGoPaths() + for _, prefix := range []string{ + "HELM_", + "XDG_", + "GO", + } { + for _, e := range os.Environ() { + if strings.HasPrefix(e, prefix) { + fmt.Println(e) + } + } + } + return nil +} + +func (o *envOptions) setXdgPaths() { + for key, val := range map[string]string{ + xdg.CacheHomeEnvVar: helmpath.CachePath(), + xdg.ConfigHomeEnvVar: helmpath.ConfigPath(), + xdg.DataHomeEnvVar: helmpath.DataPath(), + } { + if eVal := os.Getenv(key); len(eVal) <= 0 { + os.Setenv(key, val) + } + } +} + +func (o *envOptions) setGoPaths() { + for key, val := range map[string]string{ + "GOPATH": "~/go", + "GOBIN": "~/go/bin", + } { + if eVal := os.Getenv(key); len(eVal) <= 0 { + os.Setenv(key, val) + } + } +} diff --git a/cmd/helm/root.go b/cmd/helm/root.go index 004701725..2b1ac1282 100644 --- a/cmd/helm/root.go +++ b/cmd/helm/root.go @@ -172,6 +172,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string newUpgradeCmd(actionConfig, out), newCompletionCmd(out), + newEnvCmd(out), newPluginCmd(out), newVersionCmd(out), From 586780d034b9a760afebb9336cde57f2f35388ba Mon Sep 17 00:00:00 2001 From: Vibhav Bobade Date: Tue, 27 Aug 2019 03:56:05 +0530 Subject: [PATCH 2/3] Updated to get Helm env Paths and XDG env paths only Signed-off-by: Vibhav Bobade --- cmd/helm/env.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/cmd/helm/env.go b/cmd/helm/env.go index 5b07462ff..eda206607 100644 --- a/cmd/helm/env.go +++ b/cmd/helm/env.go @@ -25,10 +25,6 @@ import ( "helm.sh/helm/pkg/helmpath" "helm.sh/helm/pkg/helmpath/xdg" - "helm.sh/helm/pkg/cli" - - "helm.sh/helm/pkg/plugin" - "github.com/spf13/cobra" "helm.sh/helm/cmd/helm/require" @@ -45,7 +41,7 @@ func newEnvCmd(out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "env", - Short: "environment information in use the Helm client", + Short: "Helm client environment information", Long: envHelp, Args: require.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { @@ -59,15 +55,12 @@ func newEnvCmd(out io.Writer) *cobra.Command { type envOptions struct { } -// run . func (o *envOptions) run(out io.Writer) error { - plugin.SetupPluginEnv(&cli.EnvSettings{}, "", "") + o.setHelmPaths() o.setXdgPaths() - o.setGoPaths() for _, prefix := range []string{ "HELM_", "XDG_", - "GO", } { for _, e := range os.Environ() { if strings.HasPrefix(e, prefix) { @@ -78,11 +71,15 @@ func (o *envOptions) run(out io.Writer) error { return nil } -func (o *envOptions) setXdgPaths() { +func (o *envOptions) setHelmPaths() { for key, val := range map[string]string{ - xdg.CacheHomeEnvVar: helmpath.CachePath(), - xdg.ConfigHomeEnvVar: helmpath.ConfigPath(), - xdg.DataHomeEnvVar: helmpath.DataPath(), + "HELM_HOME": helmpath.DataPath(), + "HELM_PATH_STARTER": helmpath.DataPath("starters"), + "HELM_DEBUG": fmt.Sprint(settings.Debug), + "HELM_REGISTRY_CONFIG": settings.RegistryConfig, + "HELM_PATH_REPOSITORY_FILE": settings.RepositoryConfig, + "HELM_PATH_REPOSITORY_CACHE": settings.RepositoryCache, + "HELM_PLUGIN": settings.PluginsDirectory, } { if eVal := os.Getenv(key); len(eVal) <= 0 { os.Setenv(key, val) @@ -90,10 +87,11 @@ func (o *envOptions) setXdgPaths() { } } -func (o *envOptions) setGoPaths() { +func (o *envOptions) setXdgPaths() { for key, val := range map[string]string{ - "GOPATH": "~/go", - "GOBIN": "~/go/bin", + xdg.CacheHomeEnvVar: helmpath.CachePath(), + xdg.ConfigHomeEnvVar: helmpath.ConfigPath(), + xdg.DataHomeEnvVar: helmpath.DataPath(), } { if eVal := os.Getenv(key); len(eVal) <= 0 { os.Setenv(key, val) From 66b037f6be10007bec5572f328cff95c0219c585 Mon Sep 17 00:00:00 2001 From: Vibhav Bobade Date: Fri, 30 Aug 2019 23:08:32 +0530 Subject: [PATCH 3/3] Move the logic for checking env in pkg/cli and store all envs in a central place Signed-off-by: Vibhav Bobade --- cmd/helm/env.go | 48 ++++----------------------------- pkg/cli/environment.go | 61 +++++++++++++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 55 deletions(-) diff --git a/cmd/helm/env.go b/cmd/helm/env.go index eda206607..7c09d1632 100644 --- a/cmd/helm/env.go +++ b/cmd/helm/env.go @@ -19,11 +19,8 @@ package main import ( "fmt" "io" - "os" - "strings" - "helm.sh/helm/pkg/helmpath" - "helm.sh/helm/pkg/helmpath/xdg" + "helm.sh/helm/pkg/cli" "github.com/spf13/cobra" @@ -38,6 +35,7 @@ Env prints out all the environment information in use by Helm. func newEnvCmd(out io.Writer) *cobra.Command { o := &envOptions{} + o.settings = cli.New() cmd := &cobra.Command{ Use: "env", @@ -53,48 +51,12 @@ func newEnvCmd(out io.Writer) *cobra.Command { } type envOptions struct { + settings *cli.EnvSettings } func (o *envOptions) run(out io.Writer) error { - o.setHelmPaths() - o.setXdgPaths() - for _, prefix := range []string{ - "HELM_", - "XDG_", - } { - for _, e := range os.Environ() { - if strings.HasPrefix(e, prefix) { - fmt.Println(e) - } - } + for _, e := range o.settings.EnvironmentVariables { + fmt.Printf("%s=\"%s\" \n", e.Name, e.Value) } return nil } - -func (o *envOptions) setHelmPaths() { - for key, val := range map[string]string{ - "HELM_HOME": helmpath.DataPath(), - "HELM_PATH_STARTER": helmpath.DataPath("starters"), - "HELM_DEBUG": fmt.Sprint(settings.Debug), - "HELM_REGISTRY_CONFIG": settings.RegistryConfig, - "HELM_PATH_REPOSITORY_FILE": settings.RepositoryConfig, - "HELM_PATH_REPOSITORY_CACHE": settings.RepositoryCache, - "HELM_PLUGIN": settings.PluginsDirectory, - } { - if eVal := os.Getenv(key); len(eVal) <= 0 { - os.Setenv(key, val) - } - } -} - -func (o *envOptions) setXdgPaths() { - for key, val := range map[string]string{ - xdg.CacheHomeEnvVar: helmpath.CachePath(), - xdg.ConfigHomeEnvVar: helmpath.ConfigPath(), - xdg.DataHomeEnvVar: helmpath.DataPath(), - } { - if eVal := os.Getenv(key); len(eVal) <= 0 { - os.Setenv(key, val) - } - } -} diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go index b6b0b114b..7ac1dbc41 100644 --- a/pkg/cli/environment.go +++ b/pkg/cli/environment.go @@ -23,11 +23,13 @@ These dependencies are expressed as interfaces so that alternate implementations package cli import ( + "fmt" "os" "github.com/spf13/pflag" "helm.sh/helm/pkg/helmpath" + "helm.sh/helm/pkg/helmpath/xdg" ) // EnvSettings describes all of the environment settings. @@ -49,15 +51,26 @@ type EnvSettings struct { RepositoryCache string // PluginsDirectory is the path to the plugins directory. PluginsDirectory string + + // Environment Variables Store + EnvironmentVariables []EnvironmentVariable +} + +type EnvironmentVariable struct { + Name string + Value string } func New() *EnvSettings { - return &EnvSettings{ - PluginsDirectory: helmpath.DataPath("plugins"), - RegistryConfig: helmpath.ConfigPath("registry.json"), - RepositoryConfig: helmpath.ConfigPath("repositories.yaml"), - RepositoryCache: helmpath.CachePath("repository"), + envSettings := EnvSettings{ + PluginsDirectory: helmpath.DataPath("plugins"), + RegistryConfig: helmpath.ConfigPath("registry.json"), + RepositoryConfig: helmpath.ConfigPath("repositories.yaml"), + RepositoryCache: helmpath.CachePath("repository"), + EnvironmentVariables: []EnvironmentVariable{}, } + envSettings.setHelmEnvVars() + return &envSettings } // AddFlags binds flags to the given flagset. @@ -72,13 +85,6 @@ func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&s.RepositoryCache, "repository-cache", s.RepositoryCache, "path to the repositories config file") } -// Init sets values from the environment. -func (s *EnvSettings) Init(fs *pflag.FlagSet) { - for name, envar := range envMap { - setFlagFromEnv(name, envar, fs) - } -} - // envMap maps flag names to envvars var envMap = map[string]string{ "debug": "HELM_DEBUG", @@ -95,3 +101,34 @@ func setFlagFromEnv(name, envar string, fs *pflag.FlagSet) { fs.Set(name, v) } } + +func (s *EnvSettings) setHelmEnvVars() { + for key, val := range map[string]string{ + "HELM_HOME": helmpath.DataPath(), + "HELM_PATH_STARTER": helmpath.DataPath("starters"), + "HELM_DEBUG": fmt.Sprint(s.Debug), + "HELM_REGISTRY_CONFIG": s.RegistryConfig, + "HELM_PATH_REPOSITORY_FILE": s.RepositoryConfig, + "HELM_PATH_REPOSITORY_CACHE": s.RepositoryCache, + "HELM_PLUGIN": s.PluginsDirectory, + xdg.CacheHomeEnvVar: helmpath.CachePath(), + xdg.ConfigHomeEnvVar: helmpath.ConfigPath(), + xdg.DataHomeEnvVar: helmpath.DataPath(), + } { + if eVal := os.Getenv(key); len(eVal) > 0 { + val = eVal + } + s.EnvironmentVariables = append(s.EnvironmentVariables, + EnvironmentVariable{ + Name: key, + Value: val, + }) + } +} + +// Init sets values from the environment. +func (s *EnvSettings) Init(fs *pflag.FlagSet) { + for name, envar := range envMap { + setFlagFromEnv(name, envar, fs) + } +}