github.com/aaronmell/helm@v3.0.0-beta.2+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  
    28  	"github.com/spf13/pflag"
    29  
    30  	"helm.sh/helm/pkg/helmpath"
    31  )
    32  
    33  // EnvSettings describes all of the environment settings.
    34  type EnvSettings struct {
    35  	// Namespace is the namespace scope.
    36  	Namespace string
    37  	// KubeConfig is the path to the kubeconfig file.
    38  	KubeConfig string
    39  	// KubeContext is the name of the kubeconfig context.
    40  	KubeContext string
    41  	// Debug indicates whether or not Helm is running in Debug mode.
    42  	Debug bool
    43  
    44  	// RegistryConfig is the path to the registry config file.
    45  	RegistryConfig string
    46  	// RepositoryConfig is the path to the repositories file.
    47  	RepositoryConfig string
    48  	// Repositoryache is the path to the repository cache directory.
    49  	RepositoryCache string
    50  	// PluginsDirectory is the path to the plugins directory.
    51  	PluginsDirectory string
    52  }
    53  
    54  func New() *EnvSettings {
    55  	return &EnvSettings{
    56  		PluginsDirectory: helmpath.DataPath("plugins"),
    57  		RegistryConfig:   helmpath.ConfigPath("registry.json"),
    58  		RepositoryConfig: helmpath.ConfigPath("repositories.yaml"),
    59  		RepositoryCache:  helmpath.CachePath("repository"),
    60  	}
    61  }
    62  
    63  // AddFlags binds flags to the given flagset.
    64  func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) {
    65  	fs.StringVarP(&s.Namespace, "namespace", "n", "", "namespace scope for this request")
    66  	fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file")
    67  	fs.StringVar(&s.KubeContext, "kube-context", "", "name of the kubeconfig context to use")
    68  	fs.BoolVar(&s.Debug, "debug", false, "enable verbose output")
    69  
    70  	fs.StringVar(&s.RegistryConfig, "registry-config", s.RegistryConfig, "path to the registry config file")
    71  	fs.StringVar(&s.RepositoryConfig, "repository-config", s.RepositoryConfig, "path to the repositories config file")
    72  	fs.StringVar(&s.RepositoryCache, "repository-cache", s.RepositoryCache, "path to the repositories config file")
    73  }
    74  
    75  // Init sets values from the environment.
    76  func (s *EnvSettings) Init(fs *pflag.FlagSet) {
    77  	for name, envar := range envMap {
    78  		setFlagFromEnv(name, envar, fs)
    79  	}
    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  }