github.com/monostream/helm@v3.0.0-beta.3+incompatible/cmd/helm/root.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 main // import "helm.sh/helm/cmd/helm" 18 19 import ( 20 "io" 21 22 "github.com/spf13/cobra" 23 24 "helm.sh/helm/cmd/helm/require" 25 "helm.sh/helm/internal/experimental/registry" 26 "helm.sh/helm/pkg/action" 27 ) 28 29 const ( 30 bashCompletionFunc = ` 31 __helm_override_flag_list=(--kubeconfig --kube-context --namespace -n) 32 __helm_override_flags() 33 { 34 local ${__helm_override_flag_list[*]##*-} two_word_of of var 35 for w in "${words[@]}"; do 36 if [ -n "${two_word_of}" ]; then 37 eval "${two_word_of##*-}=\"${two_word_of}=\${w}\"" 38 two_word_of= 39 continue 40 fi 41 for of in "${__helm_override_flag_list[@]}"; do 42 case "${w}" in 43 ${of}=*) 44 eval "${of##*-}=\"${w}\"" 45 ;; 46 ${of}) 47 two_word_of="${of}" 48 ;; 49 esac 50 done 51 done 52 for var in "${__helm_override_flag_list[@]##*-}"; do 53 if eval "test -n \"\$${var}\""; then 54 eval "echo \${${var}}" 55 fi 56 done 57 } 58 __helm_list_releases() 59 { 60 __helm_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}" 61 local out filter 62 # Use ^ to map from the start of the release name 63 filter="^${words[c]}" 64 if out=$(helm list $(__helm_override_flags) -a -q -m 1000 -f ${filter} 2>/dev/null); then 65 COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) 66 fi 67 } 68 __helm_list_repos() 69 { 70 __helm_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}" 71 local out 72 if out=$(helm repo list | tail +2 | cut -f1 2>/dev/null); then 73 COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) 74 fi 75 } 76 __helm_list_plugins() 77 { 78 __helm_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}" 79 local out 80 if out=$(helm plugin list | tail +2 | cut -f1 2>/dev/null); then 81 COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) 82 fi 83 } 84 __helm_custom_func() 85 { 86 __helm_debug "${FUNCNAME[0]}: last_command is $last_command" 87 case ${last_command} in 88 helm_uninstall | helm_history | helm_status | helm_test_run |\ 89 helm_upgrade | helm_rollback | helm_get_*) 90 __helm_list_releases 91 return 92 ;; 93 helm_repo_remove) 94 __helm_list_repos 95 return 96 ;; 97 helm_plugin_remove | helm_plugin_update) 98 __helm_list_plugins 99 return 100 ;; 101 *) 102 ;; 103 esac 104 } 105 ` 106 ) 107 108 var globalUsage = `The Kubernetes package manager 109 110 Common actions for Helm: 111 112 - helm search: search for charts 113 - helm fetch: download a chart to your local directory to view 114 - helm install: upload the chart to Kubernetes 115 - helm list: list releases of charts 116 117 Environment: 118 $XDG_CACHE_HOME set an alternative location for storing cached files. 119 $XDG_CONFIG_HOME set an alternative location for storing Helm configuration. 120 $XDG_DATA_HOME set an alternative location for storing Helm data. 121 $HELM_DRIVER set the backend storage driver. Values are: configmap, secret, memory 122 $HELM_NO_PLUGINS disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins. 123 $KUBECONFIG set an alternative Kubernetes configuration file (default "~/.kube/config") 124 125 Helm stores configuration based on the XDG base directory specification, so 126 127 - cached files are stored in $XDG_CACHE_HOME/helm 128 - configuration is stored in $XDG_CONFIG_HOME/helm 129 - data is stored in $XDG_DATA_HOME/helm 130 131 By default, the default directories depend on the Operating System. The defaults are listed below: 132 133 +------------------+---------------------------+--------------------------------+-------------------------+ 134 | Operating System | Cache Path | Configuration Path | Data Path | 135 +------------------+---------------------------+--------------------------------+-------------------------+ 136 | Linux | $HOME/.cache/helm | $HOME/.config/helm | $HOME/.local/share/helm | 137 | macOS | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm | 138 | Windows | %TEMP%\helm | %APPDATA%\helm | %APPDATA%\helm | 139 +------------------+---------------------------+--------------------------------+-------------------------+ 140 ` 141 142 func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string) *cobra.Command { 143 cmd := &cobra.Command{ 144 Use: "helm", 145 Short: "The Helm package manager for Kubernetes.", 146 Long: globalUsage, 147 SilenceUsage: true, 148 Args: require.NoArgs, 149 BashCompletionFunction: bashCompletionFunc, 150 } 151 flags := cmd.PersistentFlags() 152 153 settings.AddFlags(flags) 154 155 // We can safely ignore any errors that flags.Parse encounters since 156 // those errors will be caught later during the call to cmd.Execution. 157 // This call is required to gather configuration information prior to 158 // execution. 159 flags.ParseErrorsWhitelist.UnknownFlags = true 160 flags.Parse(args) 161 162 // set defaults from environment 163 settings.Init(flags) 164 165 // Add subcommands 166 cmd.AddCommand( 167 // chart commands 168 newCreateCmd(out), 169 newDependencyCmd(out), 170 newPullCmd(out), 171 newShowCmd(out), 172 newLintCmd(out), 173 newPackageCmd(out), 174 newRepoCmd(out), 175 newSearchCmd(out), 176 newVerifyCmd(out), 177 178 // release commands 179 newGetCmd(actionConfig, out), 180 newHistoryCmd(actionConfig, out), 181 newInstallCmd(actionConfig, out), 182 newListCmd(actionConfig, out), 183 newReleaseTestCmd(actionConfig, out), 184 newRollbackCmd(actionConfig, out), 185 newStatusCmd(actionConfig, out), 186 newTemplateCmd(actionConfig, out), 187 newUninstallCmd(actionConfig, out), 188 newUpgradeCmd(actionConfig, out), 189 190 newCompletionCmd(out), 191 newEnvCmd(out), 192 newPluginCmd(out), 193 newVersionCmd(out), 194 195 // Hidden documentation generator command: 'helm docs' 196 newDocsCmd(out), 197 ) 198 199 // Add *experimental* subcommands 200 registryClient, err := registry.NewClient( 201 registry.ClientOptDebug(settings.Debug), 202 registry.ClientOptWriter(out), 203 ) 204 if err != nil { 205 // TODO: dont panic here, refactor newRootCmd to return error 206 panic(err) 207 } 208 actionConfig.RegistryClient = registryClient 209 cmd.AddCommand( 210 newRegistryCmd(actionConfig, out), 211 newChartCmd(actionConfig, out), 212 ) 213 214 // Find and add plugins 215 loadPlugins(cmd, out) 216 217 return cmd 218 }