Improve implementation

Make test be better designed to use interface implementation instead
of leaking variables with closures. The removed warning was concidered
as irrelevant, so it won't be needed in fact, because legacy version
is not goint to become deprecated

Signed-off-by: Alexander Nesterenko <nestorf250@gmail.com>
pull/5166/head
Alexander Nesterenko 7 years ago
parent 282f918cf0
commit 41a60ca915

@ -46,10 +46,9 @@ Common actions from this point include:
Environment: Environment:
$HELM_HOME set an alternative location for Helm files. By default, these are stored in $HELM_HOME set an alternative location for Helm files. By default, these are stored in
"$XDG_CONFIG_DIR/helm" (defaults to ~/.config/helm) on Linux, "$XDG_CONFIG_DIR/helm" (typically ~/.config/helm) on Linux,
"%APPDATA%\helm" on Windows and "$HOME/Library/Preferences" on OSX. "%APPDATA%\helm" on Windows and "$HOME/Library/Preferences" on OSX.
NOTE: if you have old-style "~/.helm" directory, it will be used, but consider NOTE: if you have old-style "~/.helm" directory, it will be used.
moving it to a new home.
$HELM_DRIVER set the backend storage driver. Values are: configmap, secret, memory $HELM_DRIVER set the backend storage driver. Values are: configmap, secret, memory
$HELM_NO_PLUGINS disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins. $HELM_NO_PLUGINS disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.
$KUBECONFIG set an alternative Kubernetes configuration file (default "~/.kube/config") $KUBECONFIG set an alternative Kubernetes configuration file (default "~/.kube/config")
@ -67,7 +66,6 @@ func newRootCmd(c helm.Interface, actionConfig *action.Configuration, out io.Wri
flags := cmd.PersistentFlags() flags := cmd.PersistentFlags()
settings.AddFlags(flags) settings.AddFlags(flags)
settings.AddHomeFlag(flags, helmpath.GetDefaultConfigHome(out))
flags.Parse(args) flags.Parse(args)

@ -17,8 +17,8 @@ limitations under the License.
package main package main
import ( import (
"k8s.io/helm/pkg/helm/helmpath"
"os" "os"
"path/filepath"
"testing" "testing"
"k8s.io/client-go/util/homedir" "k8s.io/client-go/util/homedir"
@ -34,7 +34,7 @@ func TestRootCmd(t *testing.T) {
{ {
name: "defaults", name: "defaults",
args: "home", args: "home",
home: filepath.Join(homedir.HomeDir(), ".config/helm"), home: helmpath.GetDefaultConfigHome(),
}, },
{ {
name: "with --home set", name: "with --home set",

@ -48,11 +48,7 @@ func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file") fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file")
fs.StringVar(&s.KubeContext, "kube-context", "", "name of the kubeconfig context to use") fs.StringVar(&s.KubeContext, "kube-context", "", "name of the kubeconfig context to use")
fs.BoolVar(&s.Debug, "debug", false, "enable verbose output") fs.BoolVar(&s.Debug, "debug", false, "enable verbose output")
} fs.StringVar((*string)(&s.Home), "home", helmpath.GetDefaultConfigHome(), "location of your Helm config. Overrides $HELM_HOME")
// Binds home flag.
func (s *EnvSettings) AddHomeFlag(fs *pflag.FlagSet, defaultHomePath string) {
fs.StringVar((*string)(&s.Home), "home", defaultHomePath, "location of your Helm config. Overrides $HELM_HOME")
} }
// Init sets values from the environment. // Init sets values from the environment.

@ -40,8 +40,8 @@ func TestEnvSettings(t *testing.T) {
}{ }{
{ {
name: "defaults", name: "defaults",
home: "~/.helm", home: helmpath.GetDefaultConfigHome(),
plugins: helmpath.Home("~/.helm").Plugins(), plugins: helmpath.Home(helmpath.GetDefaultConfigHome()).Plugins(),
ns: "", ns: "",
}, },
{ {
@ -83,7 +83,6 @@ func TestEnvSettings(t *testing.T) {
settings := &EnvSettings{} settings := &EnvSettings{}
settings.AddFlags(flags) settings.AddFlags(flags)
settings.AddHomeFlag(flags, "~/.helm")
flags.Parse(strings.Split(tt.args, " ")) flags.Parse(strings.Split(tt.args, " "))
settings.Init(flags) settings.Init(flags)

@ -14,7 +14,6 @@
package helmpath package helmpath
import ( import (
"os"
"runtime" "runtime"
"testing" "testing"
) )
@ -26,28 +25,40 @@ func StringEquals(t *testing.T, a, b string) {
} }
} }
func returns(what bool) func() bool { return func() bool { return what } } type WithNewHome struct{ DefaultConfigHomePath }
func (WithNewHome) xdgHomeExists() bool { return true }
func (WithNewHome) basicHomeExists() bool { return false }
type WithOldHome struct{ DefaultConfigHomePath }
func (WithOldHome) xdgHomeExists() bool { return false }
func (WithOldHome) basicHomeExists() bool { return true }
type WithNoHome struct{ DefaultConfigHomePath }
func (WithNoHome) xdgHomeExists() bool { return false }
func (WithNoHome) basicHomeExists() bool { return false }
type WithAllHomes struct{ DefaultConfigHomePath }
func (WithAllHomes) xdgHomeExists() bool { return true }
func (WithAllHomes) basicHomeExists() bool { return true }
func TestGetDefaultConfigHome(t *testing.T) { func TestGetDefaultConfigHome(t *testing.T) {
var _OldDefaultHelmHomeExists = OldDefaultHelmHomeExists oldConfig := ConfigPath
var _DefaultHelmHomeExists = DefaultHelmHomeExists
OldDefaultHelmHomeExists = returns(false) ConfigPath = WithNewHome{}
DefaultHelmHomeExists = returns(false) StringEquals(t, GetDefaultConfigHome(), defaultHelmHome)
StringEquals(t, GetDefaultConfigHome(os.Stdout), defaultHelmHome)
OldDefaultHelmHomeExists = returns(true) ConfigPath = WithOldHome{}
DefaultHelmHomeExists = returns(false) StringEquals(t, GetDefaultConfigHome(), oldDefaultHelmHome)
StringEquals(t, GetDefaultConfigHome(os.Stdout), oldDefaultHelmHome)
OldDefaultHelmHomeExists = returns(false) ConfigPath = WithNoHome{}
DefaultHelmHomeExists = returns(true) StringEquals(t, GetDefaultConfigHome(), defaultHelmHome)
StringEquals(t, GetDefaultConfigHome(os.Stdout), defaultHelmHome)
OldDefaultHelmHomeExists = returns(true) ConfigPath = WithAllHomes{}
DefaultHelmHomeExists = returns(true) StringEquals(t, GetDefaultConfigHome(), defaultHelmHome)
StringEquals(t, GetDefaultConfigHome(os.Stdout), defaultHelmHome)
OldDefaultHelmHomeExists = _OldDefaultHelmHomeExists ConfigPath = oldConfig
DefaultHelmHomeExists = _DefaultHelmHomeExists
} }

@ -17,47 +17,49 @@ limitations under the License.
package helmpath package helmpath
import ( import (
"fmt"
"github.com/casimir/xdg-go" "github.com/casimir/xdg-go"
"io"
"k8s.io/client-go/util/homedir" "k8s.io/client-go/util/homedir"
"os" "os"
"path/filepath" "path/filepath"
) )
// Old default helm home, it's old good ~/.helm
var oldDefaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm")
// New default helm home, with different paths for different OS: // New default helm home, with different paths for different OS:
// - %APPDATA%\helm on Windows // - %APPDATA%\helm on Windows
// - ~/Library/Preferences/helm on OSX // - ~/Library/Preferences/helm on OSX
// - $XDG_CONFIG_DIR/helm (typically ~/.config/helm for linux) // - $XDG_CONFIG_DIR/helm (typically ~/.config/helm for linux)
var defaultHelmHome = filepath.Join(xdg.ConfigHome(), "helm") var defaultHelmHome = filepath.Join(xdg.ConfigHome(), "helm")
func DirExists(path string) bool { // Old default helm home, it's old good ~/.helm
osStat, err := os.Stat(path) var oldDefaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm")
return err == nil && osStat.IsDir()
type DefaultConfigHomePath interface {
xdgHomeExists() bool
basicHomeExists() bool
} }
// Check whether new default helm home exists type FSConfigHomePath struct{ DefaultConfigHomePath }
// TODO: improve me
var DefaultHelmHomeExists = func() bool { // Checks whether $XDG_CONFIG_HOME/helm exists
func (FSConfigHomePath) xdgHomeExists() bool {
return DirExists(defaultHelmHome) return DirExists(defaultHelmHome)
} }
// Checks whether old-style ~/.helm exists // Checks whether ~/.helm exists
// TODO: improve me func (FSConfigHomePath) basicHomeExists() bool {
var OldDefaultHelmHomeExists = func() bool {
return DirExists(oldDefaultHelmHome) return DirExists(oldDefaultHelmHome)
} }
var ConfigPath DefaultConfigHomePath = FSConfigHomePath{}
func DirExists(path string) bool {
osStat, err := os.Stat(path)
return err == nil && osStat.IsDir()
}
// Get configuration home dir. // Get configuration home dir.
// func GetDefaultConfigHome() string {
// Note: Temporal until all migrate to XDG Base Directory spec if ConfigPath.xdgHomeExists() || !ConfigPath.basicHomeExists() {
func GetDefaultConfigHome(out io.Writer) string {
if DefaultHelmHomeExists() || !OldDefaultHelmHomeExists() {
return defaultHelmHome return defaultHelmHome
} }
fmt.Fprintf(out, "WARNING: using old-style configuration directory. Please, consider moving it to %s\n", defaultHelmHome)
return oldDefaultHelmHome return oldDefaultHelmHome
} }

Loading…
Cancel
Save