From 7e2964c549d1ef5cba6543a8b13de256741593b7 Mon Sep 17 00:00:00 2001 From: Michelle Noorali Date: Wed, 18 May 2016 15:00:35 -0400 Subject: [PATCH] feat(helm): add chart url to index file entries --- cmd/helm/repo.go | 10 +++--- pkg/repo/index.go | 55 +++++++++++++++++++++++++++--- pkg/repo/repo.go | 79 ++++++------------------------------------- pkg/repo/repo_test.go | 9 +++-- 4 files changed, 74 insertions(+), 79 deletions(-) diff --git a/cmd/helm/repo.go b/cmd/helm/repo.go index 71326f549..cb09b0046 100644 --- a/cmd/helm/repo.go +++ b/cmd/helm/repo.go @@ -44,7 +44,7 @@ var repoRemoveCmd = &cobra.Command{ } var repoIndexCmd = &cobra.Command{ - Use: "index [flags] [DIR]", + Use: "index [flags] [DIR] [REPO_URL]", Short: "generate an index file for a chart repository given a directory", RunE: runRepoIndex, } @@ -96,7 +96,7 @@ func runRepoRemove(cmd *cobra.Command, args []string) error { } func runRepoIndex(cmd *cobra.Command, args []string) error { - if err := checkArgsLength(1, len(args), "path to a directory"); err != nil { + if err := checkArgsLength(2, len(args), "path to a directory", "url of chart repository"); err != nil { return err } @@ -105,15 +105,15 @@ func runRepoIndex(cmd *cobra.Command, args []string) error { return err } - if err := index(path); err != nil { + if err := index(path, args[1]); err != nil { return err } return nil } -func index(dir string) error { - chartRepo, err := repo.LoadChartRepository(dir) +func index(dir, url string) error { + chartRepo, err := repo.LoadChartRepository(dir, url) if err != nil { return err } diff --git a/pkg/repo/index.go b/pkg/repo/index.go index 33f9c1dfe..feee9cdaa 100644 --- a/pkg/repo/index.go +++ b/pkg/repo/index.go @@ -6,8 +6,12 @@ import ( "strings" "gopkg.in/yaml.v2" + + "github.com/kubernetes/helm/pkg/chart" ) +var indexPath = "index.yaml" + // IndexFile represents the index file in a chart repository type IndexFile struct { Entries map[string]*ChartRef @@ -15,10 +19,11 @@ type IndexFile struct { // ChartRef represents a chart entry in the IndexFile type ChartRef struct { - Name string `yaml:"name"` - URL string `yaml:"url"` - Keywords []string `yaml:"keywords"` - Removed bool `yaml:"removed,omitempty"` + Name string `yaml:"name"` + URL string `yaml:"url"` + Created string `yaml:"created,omitempty"` + Removed bool `yaml:"removed,omitempty"` + Chartfile chart.Chartfile `yaml:"chartfile"` } // DownloadIndexFile uses @@ -49,3 +54,45 @@ func DownloadIndexFile(repoName, url, indexFileName string) error { return nil } + +// UnmarshalYAML unmarshals the index file +func (i *IndexFile) UnmarshalYAML(unmarshal func(interface{}) error) error { + var refs map[string]*ChartRef + if err := unmarshal(&refs); err != nil { + if _, ok := err.(*yaml.TypeError); !ok { + return err + } + } + i.Entries = refs + return nil +} + +func (i *IndexFile) addEntry(name string, url string) ([]byte, error) { + if i.Entries == nil { + i.Entries = make(map[string]*ChartRef) + } + entry := ChartRef{Name: name, URL: url} + i.Entries[name] = &entry + out, err := yaml.Marshal(&i.Entries) + if err != nil { + return nil, err + } + + return out, nil +} + +// LoadIndexFile takes a file at the given path and returns an IndexFile object +func LoadIndexFile(path string) (*IndexFile, error) { + b, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + + var indexfile IndexFile + err = yaml.Unmarshal(b, &indexfile) + if err != nil { + return nil, err + } + + return &indexfile, nil +} diff --git a/pkg/repo/repo.go b/pkg/repo/repo.go index 91af1c1e3..2ad6e3a94 100644 --- a/pkg/repo/repo.go +++ b/pkg/repo/repo.go @@ -7,11 +7,10 @@ import ( "path/filepath" "strings" - "github.com/kubernetes/helm/pkg/chart" "gopkg.in/yaml.v2" -) -var indexPath = "index.yaml" + "github.com/kubernetes/helm/pkg/chart" +) // ChartRepository represents a chart repository type ChartRepository struct { @@ -21,20 +20,6 @@ type ChartRepository struct { IndexFile *IndexFile } -// IndexFile represents the index file in a chart repository -type IndexFile struct { - Entries map[string]*ChartRef -} - -// ChartRef represents a chart entry in the IndexFile -type ChartRef struct { - Name string `yaml:"name"` - URL string `yaml:"url"` - Created string `yaml:"created,omitempty"` - Removed bool `yaml:"removed,omitempty"` - Chartfile chart.Chartfile `yaml:"chartfile"` -} - // RepoFile represents the repositories.yaml file in $HELM_HOME type RepoFile struct { Repositories map[string]string @@ -73,7 +58,7 @@ func (rf *RepoFile) UnmarshalYAML(unmarshal func(interface{}) error) error { // // This function evaluates the contents of the directory and // returns a ChartRepository -func LoadChartRepository(dir string) (*ChartRepository, error) { +func LoadChartRepository(dir, url string) (*ChartRepository, error) { dirInfo, err := os.Stat(dir) if err != nil { return nil, err @@ -83,7 +68,7 @@ func LoadChartRepository(dir string) (*ChartRepository, error) { return nil, errors.New(dir + "is not a directory") } - r := &ChartRepository{RootPath: dir} + r := &ChartRepository{RootPath: dir, URL: url} filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { if !f.IsDir() { @@ -104,46 +89,17 @@ func LoadChartRepository(dir string) (*ChartRepository, error) { return r, nil } -// UnmarshalYAML unmarshals the index file -func (i *IndexFile) UnmarshalYAML(unmarshal func(interface{}) error) error { - var refs map[string]*ChartRef - if err := unmarshal(&refs); err != nil { - if _, ok := err.(*yaml.TypeError); !ok { - return err - } - } - i.Entries = refs - return nil -} - -func (i *IndexFile) addEntry(name string, url string) ([]byte, error) { - if i.Entries == nil { - i.Entries = make(map[string]*ChartRef) - } - entry := ChartRef{Name: name, URL: url} - i.Entries[name] = &entry - out, err := yaml.Marshal(&i.Entries) - if err != nil { - return nil, err - } - - return out, nil -} - -// LoadIndexFile takes a file at the given path and returns an IndexFile object -func LoadIndexFile(path string) (*IndexFile, error) { - b, err := ioutil.ReadFile(path) +func (r *ChartRepository) saveIndexFile() error { + index, err := yaml.Marshal(&r.IndexFile.Entries) if err != nil { - return nil, err + return err } - var indexfile IndexFile - err = yaml.Unmarshal(b, &indexfile) - if err != nil { - return nil, err + if err = ioutil.WriteFile(filepath.Join(r.RootPath, indexPath), index, 0644); err != nil { + return err } - return &indexfile, nil + return nil } func (r *ChartRepository) Index() error { @@ -163,7 +119,7 @@ func (r *ChartRepository) Index() error { r.IndexFile.Entries = make(map[string]*ChartRef) } - entry := &ChartRef{Chartfile: *chartfile, Name: chartfile.Name, URL: "", Created: "", Removed: false} + entry := &ChartRef{Chartfile: *chartfile, Name: chartfile.Name, URL: r.URL, Created: "", Removed: false} //TODO: generate hash of contents of chart and add to the entry //TODO: Set created timestamp @@ -178,16 +134,3 @@ func (r *ChartRepository) Index() error { return nil } - -func (r *ChartRepository) saveIndexFile() error { - index, err := yaml.Marshal(&r.IndexFile.Entries) - if err != nil { - return err - } - - if err = ioutil.WriteFile(filepath.Join(r.RootPath, indexPath), index, 0644); err != nil { - return err - } - - return nil -} diff --git a/pkg/repo/repo_test.go b/pkg/repo/repo_test.go index d677484ed..2ab4dcf65 100644 --- a/pkg/repo/repo_test.go +++ b/pkg/repo/repo_test.go @@ -10,6 +10,7 @@ import ( const testfile = "testdata/local-index.yaml" const testRepositoriesFile = "testdata/repositories.yaml" const testRepository = "testdata/repository" +const testURL = "http://example-charts.com" func TestLoadIndexFile(t *testing.T) { cf, err := LoadIndexFile(testfile) @@ -72,7 +73,7 @@ func TestLoadRepositoriesFile(t *testing.T) { } func TestLoadChartRepository(t *testing.T) { - cr, err := LoadChartRepository(testRepository) + cr, err := LoadChartRepository(testRepository, testURL) if err != nil { t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) } @@ -86,10 +87,14 @@ func TestLoadChartRepository(t *testing.T) { if !reflect.DeepEqual(cr.ChartPaths, paths) { t.Errorf("Expected %#v but got %#v\n", paths, cr.ChartPaths) } + + if cr.URL != testURL { + t.Errorf("Expected url for chart repository to be %s but got %s", testURL, cr.URL) + } } func TestIndex(t *testing.T) { - cr, err := LoadChartRepository(testRepository) + cr, err := LoadChartRepository(testRepository, testURL) if err != nil { t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) }