1 // Copyright The Helm Authors. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 6 // http://www.apache.org/licenses/LICENSE-2.0 7 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package helmpath 15 16 import ( 17 "os" 18 "path/filepath" 19 20 "helm.sh/helm/v3/pkg/helmpath/xdg" 21 ) 22 23 const ( 24 // CacheHomeEnvVar is the environment variable used by Helm 25 // for the cache directory. When no value is set a default is used. 26 CacheHomeEnvVar = "HELM_CACHE_HOME" 27 28 // ConfigHomeEnvVar is the environment variable used by Helm 29 // for the config directory. When no value is set a default is used. 30 ConfigHomeEnvVar = "HELM_CONFIG_HOME" 31 32 // DataHomeEnvVar is the environment variable used by Helm 33 // for the data directory. When no value is set a default is used. 34 DataHomeEnvVar = "HELM_DATA_HOME" 35 ) 36 37 // lazypath is an lazy-loaded path buffer for the XDG base directory specification. 38 type lazypath string 39 40 func (l lazypath) path(helmEnvVar, xdgEnvVar string, defaultFn func() string, elem ...string) string { 41 42 // There is an order to checking for a path. 43 // 1. See if a Helm specific environment variable has been set. 44 // 2. Check if an XDG environment variable is set 45 // 3. Fall back to a default 46 base := os.Getenv(helmEnvVar) 47 if base != "" { 48 return filepath.Join(base, filepath.Join(elem...)) 49 } 50 base = os.Getenv(xdgEnvVar) 51 if base == "" { 52 base = defaultFn() 53 } 54 return filepath.Join(base, string(l), filepath.Join(elem...)) 55 } 56 57 // cachePath defines the base directory relative to which user specific non-essential data files 58 // should be stored. 59 func (l lazypath) cachePath(elem ...string) string { 60 return l.path(CacheHomeEnvVar, xdg.CacheHomeEnvVar, cacheHome, filepath.Join(elem...)) 61 } 62 63 // configPath defines the base directory relative to which user specific configuration files should 64 // be stored. 65 func (l lazypath) configPath(elem ...string) string { 66 return l.path(ConfigHomeEnvVar, xdg.ConfigHomeEnvVar, configHome, filepath.Join(elem...)) 67 } 68 69 // dataPath defines the base directory relative to which user specific data files should be stored. 70 func (l lazypath) dataPath(elem ...string) string { 71 return l.path(DataHomeEnvVar, xdg.DataHomeEnvVar, dataHome, filepath.Join(elem...)) 72 } 73