github.com/sri09kanth/helm@v3.0.0-beta.3+incompatible/pkg/cli/environment.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /*Package cli describes the operating environment for the Helm CLI. 18 19 Helm's environment encapsulates all of the service dependencies Helm has. 20 These dependencies are expressed as interfaces so that alternate implementations 21 (mocks, etc.) can be easily generated. 22 */ 23 package cli 24 25 import ( 26 "fmt" 27 "os" 28 29 "github.com/spf13/pflag" 30 31 "helm.sh/helm/pkg/helmpath" 32 ) 33 34 // EnvSettings describes all of the environment settings. 35 type EnvSettings struct { 36 // Namespace is the namespace scope. 37 Namespace string 38 // KubeConfig is the path to the kubeconfig file. 39 KubeConfig string 40 // KubeContext is the name of the kubeconfig context. 41 KubeContext string 42 // Debug indicates whether or not Helm is running in Debug mode. 43 Debug bool 44 45 // RegistryConfig is the path to the registry config file. 46 RegistryConfig string 47 // RepositoryConfig is the path to the repositories file. 48 RepositoryConfig string 49 // Repositoryache is the path to the repository cache directory. 50 RepositoryCache string 51 // PluginsDirectory is the path to the plugins directory. 52 PluginsDirectory string 53 54 // Environment Variables Store. 55 EnvironmentVariables map[string]string 56 } 57 58 func New() *EnvSettings { 59 envSettings := EnvSettings{ 60 PluginsDirectory: helmpath.DataPath("plugins"), 61 RegistryConfig: helmpath.ConfigPath("registry.json"), 62 RepositoryConfig: helmpath.ConfigPath("repositories.yaml"), 63 RepositoryCache: helmpath.CachePath("repository"), 64 EnvironmentVariables: make(map[string]string), 65 } 66 envSettings.setHelmEnvVars() 67 return &envSettings 68 } 69 70 // AddFlags binds flags to the given flagset. 71 func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) { 72 fs.StringVarP(&s.Namespace, "namespace", "n", "", "namespace scope for this request") 73 fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file") 74 fs.StringVar(&s.KubeContext, "kube-context", "", "name of the kubeconfig context to use") 75 fs.BoolVar(&s.Debug, "debug", false, "enable verbose output") 76 77 fs.StringVar(&s.RegistryConfig, "registry-config", s.RegistryConfig, "path to the registry config file") 78 fs.StringVar(&s.RepositoryConfig, "repository-config", s.RepositoryConfig, "path to the file containing repository names and URLs") 79 fs.StringVar(&s.RepositoryCache, "repository-cache", s.RepositoryCache, "path to the file containing cached repository indexes") 80 } 81 82 // envMap maps flag names to envvars 83 var envMap = map[string]string{ 84 "debug": "HELM_DEBUG", 85 "namespace": "HELM_NAMESPACE", 86 "registry-config": "HELM_REGISTRY_CONFIG", 87 "repository-config": "HELM_REPOSITORY_CONFIG", 88 } 89 90 func setFlagFromEnv(name, envar string, fs *pflag.FlagSet) { 91 if fs.Changed(name) { 92 return 93 } 94 if v, ok := os.LookupEnv(envar); ok { 95 fs.Set(name, v) 96 } 97 } 98 99 func (s *EnvSettings) setHelmEnvVars() { 100 for key, val := range map[string]string{ 101 "HELM_HOME": helmpath.DataPath(), 102 "HELM_PATH_STARTER": helmpath.DataPath("starters"), 103 "HELM_DEBUG": fmt.Sprint(s.Debug), 104 "HELM_REGISTRY_CONFIG": s.RegistryConfig, 105 "HELM_REPOSITORY_CONFIG": s.RepositoryConfig, 106 "HELM_REPOSITORY_CACHE": s.RepositoryCache, 107 "HELM_PLUGIN": s.PluginsDirectory, 108 } { 109 if eVal := os.Getenv(key); len(eVal) > 0 { 110 val = eVal 111 } 112 s.EnvironmentVariables[key] = val 113 } 114 } 115 116 // Init sets values from the environment. 117 func (s *EnvSettings) Init(fs *pflag.FlagSet) { 118 for name, envar := range envMap { 119 setFlagFromEnv(name, envar, fs) 120 } 121 }