diff --git a/helm/init.go b/helm/init.go index 380fff4..7432e43 100644 --- a/helm/init.go +++ b/helm/init.go @@ -4,16 +4,26 @@ import ( "fmt" "os" + "k8s.io/helm/pkg/getter" "k8s.io/helm/pkg/helm/helmpath" "k8s.io/helm/pkg/repo" ) +const ( + stableRepository = "stable" + stableRepositoryURL = "https://kubernetes-charts.storage.googleapis.com" +) + // Init makes sure the Helm home path exists and the required subfolders. func Init() error { if err := ensureDirectories(settings.Home); err != nil { return err } + if err := ensureDefaultRepos(settings.Home); err != nil { + return err + } + if err := ensureRepoFileFormat(settings.Home.RepositoryFile()); err != nil { return err } @@ -27,6 +37,7 @@ func ensureDirectories(home helmpath.Home) error { home.Repository(), home.Cache(), home.Plugins(), + home.Starters(), } for _, p := range configDirectories { @@ -52,3 +63,43 @@ func ensureRepoFileFormat(file string) error { return nil } + +func ensureDefaultRepos(home helmpath.Home) error { + repoFile := home.RepositoryFile() + if fi, err := os.Stat(repoFile); err != nil { + f := repo.NewRepoFile() + sr, err := initStableRepo(home.CacheIndex(stableRepository)) + if err != nil { + return err + } + + f.Add(sr) + + if err := f.WriteFile(repoFile, 0644); err != nil { + return err + } + } else if fi.IsDir() { + return fmt.Errorf("%s must be a file, not a directory", repoFile) + } + return nil +} + +func initStableRepo(cacheFile string) (*repo.Entry, error) { + c := repo.Entry{ + Name: stableRepository, + URL: stableRepositoryURL, + Cache: cacheFile, + } + r, err := repo.NewChartRepository(&c, getter.All(settings)) + if err != nil { + return nil, err + } + + // In this case, the cacheFile is always absolute. So passing empty string + // is safe. + if err := r.DownloadIndexFile(""); err != nil { + return nil, fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", stableRepositoryURL, err.Error()) + } + + return &c, nil +} diff --git a/main.go b/main.go index b48c6cd..9a03296 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,16 @@ func main() { os.Exit(1) } + if err = helm.Init(); err != nil { + fmt.Fprintf(os.Stderr, "error initialising helm: \n\n%s\n", err) + os.Exit(1) + } + + if err = helm.AddRepository("stable", "https://kubernetes-charts.storage.googleapis.com"); err != nil { + fmt.Fprintf(os.Stderr, "error adding repository: \n\n%s\n", err) + os.Exit(1) + } + if cli["--repo"] != nil { for _, r := range strings.Split(cli["--repo"].(string), ",") { p := strings.SplitN(r, "=", 2) @@ -38,16 +48,6 @@ func main() { os.Exit(1) } - if err = helm.Init(); err != nil { - fmt.Fprintf(os.Stderr, "error initialising helm: \n\n%s\n", err) - os.Exit(1) - } - - if err = helm.AddRepository("stable", "https://kubernetes-charts.storage.googleapis.com"); err != nil { - fmt.Fprintf(os.Stderr, "error adding repository: \n\n%s\n", err) - os.Exit(1) - } - cc, err := chartsconfig.NewChartsConfiguration(cfg) if err != nil { fmt.Fprintf(os.Stderr, "charts config parsing error: \n\n%s\n", err)