feat(helm): add structure.go to hold paths

pull/613/head
Michelle Noorali 9 years ago
parent 7fde06438f
commit 5251344318

@ -1,8 +1,6 @@
package main package main
import ( import (
"os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -25,7 +23,3 @@ func init() {
func home(cmd *cobra.Command, args []string) { func home(cmd *cobra.Command, args []string) {
cmd.Printf(homePath() + "\n") cmd.Printf(homePath() + "\n")
} }
func homePath() string {
return os.ExpandEnv(helmHome)
}

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"github.com/deis/tiller/pkg/client" "github.com/deis/tiller/pkg/client"
"github.com/deis/tiller/pkg/kubectl" "github.com/deis/tiller/pkg/kubectl"
@ -17,13 +16,10 @@ This command installs Tiller (the helm server side component) onto your
Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.helm/) Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.helm/)
` `
var repositoriesFilePath string var (
var cachePath string tillerImg string
var localRepoPath string defaultRepo = map[string]string{"default-name": "default-url"}
var localCacheFilePath string )
var tillerImg string
var defaultRepo = map[string]string{"default-name": "default-url"}
func init() { func init() {
initCmd.Flags().StringVarP(&tillerImg, "tiller-image", "i", "", "override tiller image") initCmd.Flags().StringVarP(&tillerImg, "tiller-image", "i", "", "override tiller image")
@ -43,7 +39,7 @@ func runInit(cmd *cobra.Command, args []string) error {
return errors.New("This command does not accept arguments. \n") return errors.New("This command does not accept arguments. \n")
} }
if err := ensureHome(homePath()); err != nil { if err := ensureHome(); err != nil {
return err return err
} }
@ -81,14 +77,8 @@ func buildKubectlRunner(kubectlPath string) kubectl.Runner {
// ensureHome checks to see if $HELM_HOME exists // ensureHome checks to see if $HELM_HOME exists
// //
// If $HELM_HOME does not exist, this function will create it. // If $HELM_HOME does not exist, this function will create it.
func ensureHome(home string) error { func ensureHome() error {
repositoriesFilePath = filepath.Join(home, "repositories.yaml") configDirectories := []string{homePath(), cacheDirectory(), localRepoDirectory()}
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 { for _, p := range configDirectories {
if fi, err := os.Stat(p); err != nil { if fi, err := os.Stat(p); err != nil {
@ -101,28 +91,30 @@ func ensureHome(home string) error {
} }
} }
if fi, err := os.Stat(repositoriesFilePath); err != nil { repoFile := repositoriesFile()
fmt.Printf("Creating %s \n", repositoriesFilePath) if fi, err := os.Stat(repoFile); err != nil {
if err := ioutil.WriteFile(repositoriesFilePath, []byte("local: localhost:8879/charts\n"), 0644); err != nil { fmt.Printf("Creating %s \n", repoFile)
if err := ioutil.WriteFile(repoFile, []byte("local: localhost:8879/charts\n"), 0644); err != nil {
return err return err
} }
} else if fi.IsDir() { } else if fi.IsDir() {
return fmt.Errorf("%s must be a file, not a directory", repositoriesFilePath) return fmt.Errorf("%s must be a file, not a directory", repoFile)
} }
if fi, err := os.Stat(localCacheFilePath); err != nil { localRepoCacheFile := localRepoDirectory(localRepoCacheFilePath)
fmt.Printf("Creating %s \n", localCacheFilePath) if fi, err := os.Stat(localRepoCacheFile); err != nil {
_, err := os.Create(localCacheFilePath) fmt.Printf("Creating %s \n", localRepoCacheFile)
_, err := os.Create(localRepoCacheFile)
if err != nil { if err != nil {
return err return err
} }
//TODO: take this out and replace with helm update functionality //TODO: take this out and replace with helm update functionality
os.Symlink(localCacheFilePath, filepath.Join(cachePath, "local-cache.yaml")) os.Symlink(localRepoCacheFile, cacheDirectory("local-cache.yaml"))
} else if fi.IsDir() { } else if fi.IsDir() {
return fmt.Errorf("%s must be a file, not a directory", localCacheFilePath) return fmt.Errorf("%s must be a file, not a directory", localRepoCacheFile)
} }
fmt.Printf("$HELM_HOME has also been configured at %s.\n", helmHome) fmt.Printf("$HELM_HOME has been configured at %s.\n", helmHome)
return nil return nil
} }

@ -8,11 +8,12 @@ import (
func TestEnsureHome(t *testing.T) { func TestEnsureHome(t *testing.T) {
home := createTmpHome() home := createTmpHome()
if err := ensureHome(home); err != nil { helmHome = home
if err := ensureHome(); err != nil {
t.Errorf("%s", err) t.Errorf("%s", err)
} }
expectedDirs := []string{home, cachePath, localRepoPath} expectedDirs := []string{homePath(), cacheDirectory(), localRepoDirectory()}
for _, dir := range expectedDirs { for _, dir := range expectedDirs {
if fi, err := os.Stat(dir); err != nil { if fi, err := os.Stat(dir); err != nil {
t.Errorf("%s", err) t.Errorf("%s", err)
@ -21,13 +22,13 @@ func TestEnsureHome(t *testing.T) {
} }
} }
if fi, err := os.Stat(repositoriesFilePath); err != nil { if fi, err := os.Stat(repositoriesFile()); err != nil {
t.Errorf("%s", err) t.Errorf("%s", err)
} else if fi.IsDir() { } else if fi.IsDir() {
t.Errorf("%s should not be a directory", fi) t.Errorf("%s should not be a directory", fi)
} }
if fi, err := os.Stat(localCacheFilePath); err != nil { if fi, err := os.Stat(localRepoDirectory(localRepoCacheFilePath)); err != nil {
t.Errorf("%s", err) t.Errorf("%s", err)
} else if fi.IsDir() { } else if fi.IsDir() {
t.Errorf("%s should not be a directory", fi) t.Errorf("%s should not be a directory", fi)

@ -56,7 +56,7 @@ func runPackage(cmd *cobra.Command, args []string) error {
// Save to $HELM_HOME/local directory. // Save to $HELM_HOME/local directory.
if save { if save {
if err := repo.AddChartToLocalRepo(ch, localRepoPath); err != nil { if err := repo.AddChartToLocalRepo(ch, localRepoDirectory()); err != nil {
return err return err
} }
} }

@ -40,7 +40,7 @@ func search(cmd *cobra.Command, args []string) error {
func searchCacheForPattern(name string) ([]string, error) { func searchCacheForPattern(name string) ([]string, error) {
fileList := []string{} fileList := []string{}
filepath.Walk(cachePath, func(path string, f os.FileInfo, err error) error { filepath.Walk(cacheDirectory(), func(path string, f os.FileInfo, err error) error {
if !f.IsDir() { if !f.IsDir() {
fileList = append(fileList, path) fileList = append(fileList, path)
} }

@ -22,5 +22,5 @@ var serveCmd = &cobra.Command{
} }
func serve(cmd *cobra.Command, args []string) { func serve(cmd *cobra.Command, args []string) {
repo.StartLocalRepo(localRepoPath) repo.StartLocalRepo(localRepoDirectory())
} }

@ -0,0 +1,31 @@
package main
import (
"os"
"path/filepath"
)
const (
repositoriesFilePath string = "repositories.yaml"
cachePath string = "cache"
localRepoPath string = "local"
localRepoCacheFilePath string = "cache.yaml"
)
func homePath() string {
return os.ExpandEnv(helmHome)
}
func cacheDirectory(paths ...string) string {
fragments := append([]string{homePath(), cachePath}, paths...)
return filepath.Join(fragments...)
}
func localRepoDirectory(paths ...string) string {
fragments := append([]string{homePath(), localRepoPath}, paths...)
return filepath.Join(fragments...)
}
func repositoriesFile() string {
return filepath.Join(homePath(), repositoriesFilePath)
}
Loading…
Cancel
Save