@@ -4,16 +4,26 @@ import (
44 "fmt"
55 "os"
66
7+ "k8s.io/helm/pkg/getter"
78 "k8s.io/helm/pkg/helm/helmpath"
89 "k8s.io/helm/pkg/repo"
910)
1011
12+ const (
13+ stableRepository = "stable"
14+ stableRepositoryURL = "https://kubernetes-charts.storage.googleapis.com"
15+ )
16+
1117// Init makes sure the Helm home path exists and the required subfolders.
1218func Init () error {
1319 if err := ensureDirectories (settings .Home ); err != nil {
1420 return err
1521 }
1622
23+ if err := ensureDefaultRepos (settings .Home ); err != nil {
24+ return err
25+ }
26+
1727 if err := ensureRepoFileFormat (settings .Home .RepositoryFile ()); err != nil {
1828 return err
1929 }
@@ -27,6 +37,7 @@ func ensureDirectories(home helmpath.Home) error {
2737 home .Repository (),
2838 home .Cache (),
2939 home .Plugins (),
40+ home .Starters (),
3041 }
3142
3243 for _ , p := range configDirectories {
@@ -52,3 +63,43 @@ func ensureRepoFileFormat(file string) error {
5263
5364 return nil
5465}
66+
67+ func ensureDefaultRepos (home helmpath.Home ) error {
68+ repoFile := home .RepositoryFile ()
69+ if fi , err := os .Stat (repoFile ); err != nil {
70+ f := repo .NewRepoFile ()
71+ sr , err := initStableRepo (home .CacheIndex (stableRepository ))
72+ if err != nil {
73+ return err
74+ }
75+
76+ f .Add (sr )
77+
78+ if err := f .WriteFile (repoFile , 0644 ); err != nil {
79+ return err
80+ }
81+ } else if fi .IsDir () {
82+ return fmt .Errorf ("%s must be a file, not a directory" , repoFile )
83+ }
84+ return nil
85+ }
86+
87+ func initStableRepo (cacheFile string ) (* repo.Entry , error ) {
88+ c := repo.Entry {
89+ Name : stableRepository ,
90+ URL : stableRepositoryURL ,
91+ Cache : cacheFile ,
92+ }
93+ r , err := repo .NewChartRepository (& c , getter .All (settings ))
94+ if err != nil {
95+ return nil , err
96+ }
97+
98+ // In this case, the cacheFile is always absolute. So passing empty string
99+ // is safe.
100+ if err := r .DownloadIndexFile ("" ); err != nil {
101+ return nil , fmt .Errorf ("Looks like %q is not a valid chart repository or cannot be reached: %s" , stableRepositoryURL , err .Error ())
102+ }
103+
104+ return & c , nil
105+ }
0 commit comments