github.com/umeshredd/helm@v3.0.0-alpha.1+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  	"os"
    27  	"path/filepath"
    28  
    29  	"github.com/spf13/pflag"
    30  	"k8s.io/client-go/util/homedir"
    31  
    32  	"helm.sh/helm/pkg/helmpath"
    33  )
    34  
    35  // defaultHelmHome is the default HELM_HOME.
    36  var defaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm")
    37  
    38  // EnvSettings describes all of the environment settings.
    39  type EnvSettings struct {
    40  	// Home is the local path to the Helm home directory.
    41  	Home helmpath.Home
    42  	// Namespace is the namespace scope.
    43  	Namespace string
    44  	// KubeConfig is the path to the kubeconfig file.
    45  	KubeConfig string
    46  	// KubeContext is the name of the kubeconfig context.
    47  	KubeContext string
    48  	// Debug indicates whether or not Helm is running in Debug mode.
    49  	Debug bool
    50  }
    51  
    52  // AddFlags binds flags to the given flagset.
    53  func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) {
    54  	fs.StringVar((*string)(&s.Home), "home", defaultHelmHome, "location of your Helm config. Overrides $HELM_HOME")
    55  	fs.StringVarP(&s.Namespace, "namespace", "n", "", "namespace scope for this request")
    56  	fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file")
    57  	fs.StringVar(&s.KubeContext, "kube-context", "", "name of the kubeconfig context to use")
    58  	fs.BoolVar(&s.Debug, "debug", false, "enable verbose output")
    59  }
    60  
    61  // Init sets values from the environment.
    62  func (s *EnvSettings) Init(fs *pflag.FlagSet) {
    63  	for name, envar := range envMap {
    64  		setFlagFromEnv(name, envar, fs)
    65  	}
    66  }
    67  
    68  // PluginDirs is the path to the plugin directories.
    69  func (s EnvSettings) PluginDirs() string {
    70  	if d, ok := os.LookupEnv("HELM_PLUGIN"); ok {
    71  		return d
    72  	}
    73  	return s.Home.Plugins()
    74  }
    75  
    76  // envMap maps flag names to envvars
    77  var envMap = map[string]string{
    78  	"debug":     "HELM_DEBUG",
    79  	"home":      "HELM_HOME",
    80  	"namespace": "HELM_NAMESPACE",
    81  }
    82  
    83  func setFlagFromEnv(name, envar string, fs *pflag.FlagSet) {
    84  	if fs.Changed(name) {
    85  		return
    86  	}
    87  	if v, ok := os.LookupEnv(envar); ok {
    88  		fs.Set(name, v)
    89  	}
    90  }