Move the logic for checking env in pkg/cli and store all envs in a central place

Signed-off-by: Vibhav Bobade <vibhav.bobde@gmail.com>
pull/6244/head
Vibhav Bobade 5 years ago
parent 586780d034
commit 66b037f6be

@ -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)
}
}
}

@ -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{
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)
}
}

Loading…
Cancel
Save