From 8404d743a05ed9d1e498f78f4bac49740dc132a6 Mon Sep 17 00:00:00 2001 From: Michelle Noorali <michellemolu@gmail.com> Date: Wed, 27 Apr 2016 09:38:26 -0600 Subject: [PATCH] ref(init): save helm paths to variables --- cmd/helm/home.go | 6 ++++- cmd/helm/init.go | 61 ++++++++++++++++++------------------------- cmd/helm/init_test.go | 11 +++++--- cmd/helm/package.go | 2 +- cmd/helm/search.go | 3 +-- cmd/helm/serve.go | 4 +-- 6 files changed, 42 insertions(+), 45 deletions(-) diff --git a/cmd/helm/home.go b/cmd/helm/home.go index 78d3473f1..1e9e63b64 100644 --- a/cmd/helm/home.go +++ b/cmd/helm/home.go @@ -23,5 +23,9 @@ func init() { } func home(cmd *cobra.Command, args []string) { - cmd.Printf(os.ExpandEnv(helmHome) + "\n") + cmd.Printf(homePath() + "\n") +} + +func homePath() string { + return os.ExpandEnv(helmHome) } diff --git a/cmd/helm/init.go b/cmd/helm/init.go index 674e0d904..26688f6de 100644 --- a/cmd/helm/init.go +++ b/cmd/helm/init.go @@ -17,13 +17,13 @@ This command installs Tiller (the helm server side component) onto your Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.helm/) ` -const repositoriesPath = ".repositories" -const cachePath = "cache" -const localPath = "local" -const localCacheFilePath = localPath + "/cache.yaml" +var repositoriesFilePath string +var cachePath string +var localRepoPath string +var localCacheFilePath string +var tillerImg string var defaultRepo = map[string]string{"default-name": "default-url"} -var tillerImg string func init() { initCmd.Flags().StringVarP(&tillerImg, "tiller-image", "i", "", "override tiller image") @@ -43,7 +43,7 @@ func runInit(cmd *cobra.Command, args []string) error { return errors.New("This command does not accept arguments. \n") } - if err := ensureHome(os.ExpandEnv(helmHome)); err != nil { + if err := ensureHome(homePath()); err != nil { return err } @@ -51,7 +51,7 @@ func runInit(cmd *cobra.Command, args []string) error { return err } - fmt.Printf("Tiller (the helm server side component) has been installed into your Kubernetes Cluster.\n$HELM_HOME has also been configured at %s.\nHappy Helming!\n", helmHome) + fmt.Println("Happy Helming!") return nil } @@ -66,6 +66,7 @@ func installTiller() error { if err != nil { return fmt.Errorf("error installing %s %s", string(out), err) } + fmt.Println("\nTiller (the helm server side component) has been installed into your Kubernetes Cluster.") return nil } @@ -81,7 +82,13 @@ func buildKubectlRunner(kubectlPath string) kubectl.Runner { // // If $HELM_HOME does not exist, this function will create it. func ensureHome(home string) error { - configDirectories := []string{home, cacheDirectory(home), localDirectory(home)} + repositoriesFilePath = filepath.Join(home, "repositories.yaml") + cachePath = filepath.Join(home, "cache") + localRepoPath = filepath.Join(home, "local") + localCacheFilePath = filepath.Join(home, "cache.yaml") + + fmt.Println("home path: " + home) + configDirectories := []string{home, cachePath, localRepoPath} for _, p := range configDirectories { if fi, err := os.Stat(p); err != nil { @@ -94,44 +101,28 @@ func ensureHome(home string) error { } } - repoPath := repositoriesFile(home) - if fi, err := os.Stat(repoPath); err != nil { - fmt.Printf("Creating %s \n", repoPath) - if err := ioutil.WriteFile(repoPath, []byte("local: localhost:8879/charts\n"), 0644); err != nil { + if fi, err := os.Stat(repositoriesFilePath); err != nil { + fmt.Printf("Creating %s \n", repositoriesFilePath) + if err := ioutil.WriteFile(repositoriesFilePath, []byte("local: localhost:8879/charts\n"), 0644); err != nil { return err } } else if fi.IsDir() { - return fmt.Errorf("%s must be a file, not a directory", repoPath) + return fmt.Errorf("%s must be a file, not a directory", repositoriesFilePath) } - localCacheFile := localDirCacheFile(home) - if fi, err := os.Stat(localCacheFile); err != nil { - fmt.Printf("Creating %s \n", localCacheFile) - _, err := os.Create(localCacheFile) + if fi, err := os.Stat(localCacheFilePath); err != nil { + fmt.Printf("Creating %s \n", localCacheFilePath) + _, err := os.Create(localCacheFilePath) if err != nil { return err } //TODO: take this out and replace with helm update functionality - os.Symlink(localCacheFile, cacheDirectory(home)+"/local-cache.yaml") + os.Symlink(localCacheFilePath, filepath.Join(cachePath, "local-cache.yaml")) } else if fi.IsDir() { - return fmt.Errorf("%s must be a file, not a directory", repoPath) + return fmt.Errorf("%s must be a file, not a directory", localCacheFilePath) } - return nil -} - -func cacheDirectory(home string) string { - return filepath.Join(home, cachePath) -} - -func repositoriesFile(home string) string { - return filepath.Join(home, repositoriesPath) -} -func localDirectory(home string) string { - return filepath.Join(home, localPath) -} - -func localDirCacheFile(home string) string { - return filepath.Join(home, localCacheFilePath) + fmt.Printf("$HELM_HOME has also been configured at %s.\n", helmHome) + return nil } diff --git a/cmd/helm/init_test.go b/cmd/helm/init_test.go index 067d6ffc2..2b3728e6a 100644 --- a/cmd/helm/init_test.go +++ b/cmd/helm/init_test.go @@ -12,8 +12,8 @@ func TestEnsureHome(t *testing.T) { t.Errorf("%s", err) } - dirs := []string{home, cacheDirectory(home), localDirectory(home)} - for _, dir := range dirs { + expectedDirs := []string{home, cachePath, localRepoPath} + for _, dir := range expectedDirs { if fi, err := os.Stat(dir); err != nil { t.Errorf("%s", err) } else if !fi.IsDir() { @@ -21,12 +21,17 @@ func TestEnsureHome(t *testing.T) { } } - if fi, err := os.Stat(repositoriesFile(home)); err != nil { + if fi, err := os.Stat(repositoriesFilePath); err != nil { t.Errorf("%s", err) } else if fi.IsDir() { t.Errorf("%s should not be a directory", fi) } + if fi, err := os.Stat(localCacheFilePath); err != nil { + t.Errorf("%s", err) + } else if fi.IsDir() { + t.Errorf("%s should not be a directory", fi) + } } func createTmpHome() string { diff --git a/cmd/helm/package.go b/cmd/helm/package.go index 6b3dc0348..66678a184 100644 --- a/cmd/helm/package.go +++ b/cmd/helm/package.go @@ -56,7 +56,7 @@ func runPackage(cmd *cobra.Command, args []string) error { // Save to $HELM_HOME/local directory. if save { - if err := repo.AddChartToLocalRepo(ch, localDirectory(os.ExpandEnv(helmHome))); err != nil { + if err := repo.AddChartToLocalRepo(ch, localRepoPath); err != nil { return err } } diff --git a/cmd/helm/search.go b/cmd/helm/search.go index e1276a58c..d3b451a2d 100644 --- a/cmd/helm/search.go +++ b/cmd/helm/search.go @@ -39,9 +39,8 @@ func search(cmd *cobra.Command, args []string) error { } func searchCacheForPattern(name string) ([]string, error) { - dir := cacheDirectory(os.ExpandEnv(helmHome)) fileList := []string{} - filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { + filepath.Walk(cachePath, func(path string, f os.FileInfo, err error) error { if !f.IsDir() { fileList = append(fileList, path) } diff --git a/cmd/helm/serve.go b/cmd/helm/serve.go index b69f18f13..7c046e139 100644 --- a/cmd/helm/serve.go +++ b/cmd/helm/serve.go @@ -1,8 +1,6 @@ package main import ( - "os" - "github.com/deis/tiller/pkg/repo" "github.com/spf13/cobra" ) @@ -24,5 +22,5 @@ var serveCmd = &cobra.Command{ } func serve(cmd *cobra.Command, args []string) { - repo.StartLocalRepo(localDirectory(os.ExpandEnv(helmHome))) + repo.StartLocalRepo(localRepoPath) } -- GitLab