Merge pull request #6244 from waveywaves/feature/helmenv

(feature/helm-env) `helm env`
This commit is contained in:
Matt Farina
2019-09-04 13:38:12 -04:00
committed by GitHub
3 changed files with 112 additions and 12 deletions

View File

@@ -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 file containing cached repository indexes")
}
// 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)
}
}