mirror of https://github.com/helm/helm
Ensure proper cleanup of `HELM_HOME` and `HELM_DEBUG`pull/4048/head
parent
34da765eff
commit
a02a598c33
@ -0,0 +1,102 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main // import "k8s.io/helm/cmd/helm"
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/helm/pkg/helm"
|
||||
)
|
||||
|
||||
var globalUsage = `The Kubernetes package manager
|
||||
|
||||
To begin working with Helm, run the 'helm init' command:
|
||||
|
||||
$ helm init
|
||||
|
||||
This will set up any necessary local configuration.
|
||||
|
||||
Common actions from this point include:
|
||||
|
||||
- helm search: search for charts
|
||||
- helm fetch: download a chart to your local directory to view
|
||||
- helm install: upload the chart to Kubernetes
|
||||
- helm list: list releases of charts
|
||||
|
||||
Environment:
|
||||
$HELM_HOME set an alternative location for Helm files. By default, these are stored in ~/.helm
|
||||
$HELM_NO_PLUGINS disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.
|
||||
$KUBECONFIG set an alternative Kubernetes configuration file (default "~/.kube/config")
|
||||
`
|
||||
|
||||
func newRootCmd(c helm.Interface, out io.Writer, args []string) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "helm",
|
||||
Short: "The Helm package manager for Kubernetes.",
|
||||
Long: globalUsage,
|
||||
SilenceUsage: true,
|
||||
}
|
||||
flags := cmd.PersistentFlags()
|
||||
|
||||
settings.AddFlags(flags)
|
||||
|
||||
cmd.AddCommand(
|
||||
// chart commands
|
||||
newCreateCmd(out),
|
||||
newDependencyCmd(out),
|
||||
newFetchCmd(out),
|
||||
newInspectCmd(out),
|
||||
newLintCmd(out),
|
||||
newPackageCmd(out),
|
||||
newRepoCmd(out),
|
||||
newSearchCmd(out),
|
||||
newVerifyCmd(out),
|
||||
|
||||
// release commands
|
||||
newDeleteCmd(c, out),
|
||||
newGetCmd(c, out),
|
||||
newHistoryCmd(c, out),
|
||||
newInstallCmd(c, out),
|
||||
newListCmd(c, out),
|
||||
newReleaseTestCmd(c, out),
|
||||
newRollbackCmd(c, out),
|
||||
newStatusCmd(c, out),
|
||||
newUpgradeCmd(c, out),
|
||||
|
||||
newCompletionCmd(out),
|
||||
newHomeCmd(out),
|
||||
newInitCmd(out),
|
||||
newPluginCmd(out),
|
||||
newTemplateCmd(out),
|
||||
newVersionCmd(out),
|
||||
|
||||
// Hidden documentation generator command: 'helm docs'
|
||||
newDocsCmd(out),
|
||||
)
|
||||
|
||||
flags.Parse(args)
|
||||
|
||||
// set defaults from environment
|
||||
settings.Init(flags)
|
||||
|
||||
// Find and add plugins
|
||||
loadPlugins(cmd, out)
|
||||
|
||||
return cmd
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRootCmd(t *testing.T) {
|
||||
defer resetEnv()()
|
||||
|
||||
tests := []struct {
|
||||
name, args, home string
|
||||
envars map[string]string
|
||||
}{
|
||||
{
|
||||
name: "defaults",
|
||||
args: "home",
|
||||
home: filepath.Join(os.Getenv("HOME"), "/.helm"),
|
||||
},
|
||||
{
|
||||
name: "with --home set",
|
||||
args: "--home /foo",
|
||||
home: "/foo",
|
||||
},
|
||||
{
|
||||
name: "subcommands with --home set",
|
||||
args: "home --home /foo",
|
||||
home: "/foo",
|
||||
},
|
||||
{
|
||||
name: "with $HELM_HOME set",
|
||||
args: "home",
|
||||
envars: map[string]string{"HELM_HOME": "/bar"},
|
||||
home: "/bar",
|
||||
},
|
||||
{
|
||||
name: "subcommands with $HELM_HOME set",
|
||||
args: "home",
|
||||
envars: map[string]string{"HELM_HOME": "/bar"},
|
||||
home: "/bar",
|
||||
},
|
||||
{
|
||||
name: "with $HELM_HOME and --home set",
|
||||
args: "home --home /foo",
|
||||
envars: map[string]string{"HELM_HOME": "/bar"},
|
||||
home: "/foo",
|
||||
},
|
||||
}
|
||||
|
||||
// ensure not set locally
|
||||
os.Unsetenv("HELM_HOME")
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
defer os.Unsetenv("HELM_HOME")
|
||||
|
||||
for k, v := range tt.envars {
|
||||
os.Setenv(k, v)
|
||||
}
|
||||
|
||||
cmd, _, err := executeCommandC(nil, tt.args)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if settings.Home.String() != tt.home {
|
||||
t.Errorf("expected home %q, got %q", tt.home, settings.Home)
|
||||
}
|
||||
homeFlag := cmd.Flag("home").Value.String()
|
||||
homeFlag = os.ExpandEnv(homeFlag)
|
||||
if homeFlag != tt.home {
|
||||
t.Errorf("expected home %q, got %q", tt.home, homeFlag)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue