(feat/6232) Lazy create config and cache directories

Signed-off-by: Akash Shinde <akashshinde159@gmail.com>
pull/6268/head
Akash Shinde 6 years ago
parent fba311ba23
commit 036245490f

@ -22,6 +22,8 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"helm.sh/helm/pkg/repo"
"github.com/Masterminds/semver" "github.com/Masterminds/semver"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -31,7 +33,6 @@ import (
"helm.sh/helm/pkg/helmpath" "helm.sh/helm/pkg/helmpath"
"helm.sh/helm/pkg/plugin" "helm.sh/helm/pkg/plugin"
"helm.sh/helm/pkg/plugin/installer" "helm.sh/helm/pkg/plugin/installer"
"helm.sh/helm/pkg/repo"
) )
const initDesc = ` const initDesc = `
@ -144,10 +145,10 @@ func ensureReposFile(out io.Writer, skipRefresh bool) error {
repoFile := helmpath.RepositoryFile() repoFile := helmpath.RepositoryFile()
if fi, err := os.Stat(repoFile); err != nil { if fi, err := os.Stat(repoFile); err != nil {
fmt.Fprintf(out, "Creating %s \n", repoFile) fmt.Fprintf(out, "Creating %s \n", repoFile)
f := repo.NewFile() //f := repo.NewFile()
if err := f.WriteFile(repoFile, 0644); err != nil { //if err := f.WriteFile(repoFile, 0644); err != nil {
return err // return err
} //}
} else if fi.IsDir() { } else if fi.IsDir() {
return errors.Errorf("%s must be a file, not a directory", repoFile) return errors.Errorf("%s must be a file, not a directory", repoFile)
} }

@ -40,20 +40,20 @@ func TestRootCmd(t *testing.T) {
{ {
name: "with $XDG_CACHE_HOME set", name: "with $XDG_CACHE_HOME set",
args: "home", args: "home",
envars: map[string]string{xdg.CacheHomeEnvVar: "/bar"}, envars: map[string]string{xdg.CacheHomeEnvVar: os.TempDir() + "bar"},
cachePath: "/bar/helm", cachePath: os.TempDir() + "bar/helm",
}, },
{ {
name: "with $XDG_CONFIG_HOME set", name: "with $XDG_CONFIG_HOME set",
args: "home", args: "home",
envars: map[string]string{xdg.ConfigHomeEnvVar: "/bar"}, envars: map[string]string{xdg.ConfigHomeEnvVar: os.TempDir() + "bar"},
configPath: "/bar/helm", configPath: os.TempDir() + "bar/helm",
}, },
{ {
name: "with $XDG_DATA_HOME set", name: "with $XDG_DATA_HOME set",
args: "home", args: "home",
envars: map[string]string{xdg.DataHomeEnvVar: "/bar"}, envars: map[string]string{xdg.DataHomeEnvVar: os.TempDir() + "bar"},
dataPath: "/bar/helm", dataPath: os.TempDir() + "bar/helm",
}, },
} }

@ -43,7 +43,9 @@ func Registry() string {
// RepositoryFile returns the path to the repositories.yaml file. // RepositoryFile returns the path to the repositories.yaml file.
func RepositoryFile() string { func RepositoryFile() string {
return lp.configPath("repositories.yaml") configPath := lp.configPath("")
repoFile := filepath.Join(configPath, "repositories.yaml")
return repoFile
} }
// RepositoryCache returns the cache path for repository metadata. // RepositoryCache returns the cache path for repository metadata.

@ -24,9 +24,9 @@ import (
) )
func TestHelmHome(t *testing.T) { func TestHelmHome(t *testing.T) {
os.Setenv(xdg.CacheHomeEnvVar, "/cache") os.Setenv(xdg.CacheHomeEnvVar, os.TempDir()+"cache")
os.Setenv(xdg.ConfigHomeEnvVar, "/config") os.Setenv(xdg.ConfigHomeEnvVar, os.TempDir()+"config")
os.Setenv(xdg.DataHomeEnvVar, "/data") os.Setenv(xdg.DataHomeEnvVar, os.TempDir()+"data")
isEq := func(t *testing.T, got, expected string) { isEq := func(t *testing.T, got, expected string) {
t.Helper() t.Helper()
if expected != got { if expected != got {
@ -35,18 +35,18 @@ func TestHelmHome(t *testing.T) {
} }
} }
isEq(t, CachePath(), "/cache/helm") isEq(t, CachePath(), os.TempDir()+"cache/helm")
isEq(t, ConfigPath(), "/config/helm") isEq(t, ConfigPath(), os.TempDir()+"config/helm")
isEq(t, DataPath(), "/data/helm") isEq(t, DataPath(), os.TempDir()+"data/helm")
isEq(t, RepositoryFile(), "/config/helm/repositories.yaml") isEq(t, RepositoryFile(), os.TempDir()+"config/helm/repositories.yaml")
isEq(t, RepositoryCache(), "/cache/helm/repository") isEq(t, RepositoryCache(), os.TempDir()+"cache/helm/repository")
isEq(t, CacheIndex("t"), "/cache/helm/repository/t-index.yaml") isEq(t, CacheIndex("t"), os.TempDir()+"cache/helm/repository/t-index.yaml")
isEq(t, CacheIndex(""), "/cache/helm/repository/index.yaml") isEq(t, CacheIndex(""), os.TempDir()+"cache/helm/repository/index.yaml")
isEq(t, Starters(), "/data/helm/starters") isEq(t, Starters(), os.TempDir()+"data/helm/starters")
isEq(t, Archive(), "/cache/helm/archive") isEq(t, Archive(), os.TempDir()+"cache/helm/archive")
// test to see if lazy-loading environment variables at runtime works // test to see if lazy-loading environment variables at runtime works
os.Setenv(xdg.CacheHomeEnvVar, "/cache2") os.Setenv(xdg.CacheHomeEnvVar, os.TempDir()+"cache2")
isEq(t, CachePath(), "/cache2/helm") isEq(t, CachePath(), os.TempDir()+"cache2/helm")
} }

@ -13,6 +13,7 @@
package helmpath package helmpath
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -27,7 +28,10 @@ func (l lazypath) path(envVar string, defaultFn func() string, file string) stri
if base == "" { if base == "" {
base = defaultFn() base = defaultFn()
} }
return filepath.Join(base, string(l), file) path := filepath.Join(base, string(l), file)
// Create directory if not exist.
l.ensurePathExist(path)
return path
} }
// cachePath defines the base directory relative to which user specific non-essential data files // cachePath defines the base directory relative to which user specific non-essential data files
@ -46,3 +50,18 @@ func (l lazypath) configPath(file string) string {
func (l lazypath) dataPath(file string) string { func (l lazypath) dataPath(file string) string {
return l.path(xdg.DataHomeEnvVar, dataHome, file) return l.path(xdg.DataHomeEnvVar, dataHome, file)
} }
func (l lazypath) ensurePathExist(path string) {
if fi, err := os.Stat(path); err != nil {
if err := os.MkdirAll(path, 0755); err != nil {
// FIXME - Need to discuss best way to log error message in this package,
// otherwise would need to refactor path() method. That would be huge refactor :(
fmt.Printf("creation of directory %s failed with error %s \n", path, err.Error())
// FIXME - os.Exit() seems like anti-pattern to exit, any suggestion here ?
os.Exit(1)
}
} else if !fi.IsDir() {
fmt.Printf("%s must be a directory. \n", path)
os.Exit(1)
}
}

@ -55,9 +55,17 @@ func LoadFile(path string) (*File, error) {
b, err := ioutil.ReadFile(path) b, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, errors.Errorf("couldn't load repositories file (%s).\nYou might need to run `helm init`", path) f := NewFile()
if err := f.WriteFile(path, 0644); err != nil {
return nil, err
}
b, err = ioutil.ReadFile(path)
if err != nil {
return nil, err
}
} else {
return nil, err
} }
return nil, err
} }
r := &File{} r := &File{}

@ -19,7 +19,6 @@ package repo
import "testing" import "testing"
import "io/ioutil" import "io/ioutil"
import "os" import "os"
import "strings"
const testRepositoriesFile = "testdata/repositories.yaml" const testRepositoriesFile = "testdata/repositories.yaml"
@ -199,11 +198,11 @@ func TestWriteFile(t *testing.T) {
} }
} }
func TestRepoNotExists(t *testing.T) { //func TestRepoNotExists(t *testing.T) {
_, err := LoadFile("/this/path/does/not/exist.yaml") // _, err := LoadFile("/this/path/does/not/exist.yaml")
if err == nil { // if err == nil {
t.Errorf("expected err to be non-nil when path does not exist") // t.Errorf("expected err to be non-nil when path does not exist")
} else if !strings.Contains(err.Error(), "You might need to run `helm init`") { // } else if !strings.Contains(err.Error(), "You might need to run `helm init`") {
t.Errorf("expected prompt to run `helm init` when repositories file does not exist") // t.Errorf("expected prompt to run `helm init` when repositories file does not exist")
} // }
} //}

Loading…
Cancel
Save