feat(helm): add chart url to index file entries

pull/745/head
Michelle Noorali 9 years ago
parent 4bb36c89ab
commit 7e2964c549

@ -44,7 +44,7 @@ var repoRemoveCmd = &cobra.Command{
} }
var repoIndexCmd = &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", Short: "generate an index file for a chart repository given a directory",
RunE: runRepoIndex, RunE: runRepoIndex,
} }
@ -96,7 +96,7 @@ func runRepoRemove(cmd *cobra.Command, args []string) error {
} }
func runRepoIndex(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 return err
} }
@ -105,15 +105,15 @@ func runRepoIndex(cmd *cobra.Command, args []string) error {
return err return err
} }
if err := index(path); err != nil { if err := index(path, args[1]); err != nil {
return err return err
} }
return nil return nil
} }
func index(dir string) error { func index(dir, url string) error {
chartRepo, err := repo.LoadChartRepository(dir) chartRepo, err := repo.LoadChartRepository(dir, url)
if err != nil { if err != nil {
return err return err
} }

@ -6,8 +6,12 @@ import (
"strings" "strings"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/kubernetes/helm/pkg/chart"
) )
var indexPath = "index.yaml"
// IndexFile represents the index file in a chart repository // IndexFile represents the index file in a chart repository
type IndexFile struct { type IndexFile struct {
Entries map[string]*ChartRef Entries map[string]*ChartRef
@ -15,10 +19,11 @@ type IndexFile struct {
// ChartRef represents a chart entry in the IndexFile // ChartRef represents a chart entry in the IndexFile
type ChartRef struct { type ChartRef struct {
Name string `yaml:"name"` Name string `yaml:"name"`
URL string `yaml:"url"` URL string `yaml:"url"`
Keywords []string `yaml:"keywords"` Created string `yaml:"created,omitempty"`
Removed bool `yaml:"removed,omitempty"` Removed bool `yaml:"removed,omitempty"`
Chartfile chart.Chartfile `yaml:"chartfile"`
} }
// DownloadIndexFile uses // DownloadIndexFile uses
@ -49,3 +54,45 @@ func DownloadIndexFile(repoName, url, indexFileName string) error {
return nil 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
}

@ -7,11 +7,10 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/kubernetes/helm/pkg/chart"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
)
var indexPath = "index.yaml" "github.com/kubernetes/helm/pkg/chart"
)
// ChartRepository represents a chart repository // ChartRepository represents a chart repository
type ChartRepository struct { type ChartRepository struct {
@ -21,20 +20,6 @@ type ChartRepository struct {
IndexFile *IndexFile 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 // RepoFile represents the repositories.yaml file in $HELM_HOME
type RepoFile struct { type RepoFile struct {
Repositories map[string]string 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 // This function evaluates the contents of the directory and
// returns a ChartRepository // returns a ChartRepository
func LoadChartRepository(dir string) (*ChartRepository, error) { func LoadChartRepository(dir, url string) (*ChartRepository, error) {
dirInfo, err := os.Stat(dir) dirInfo, err := os.Stat(dir)
if err != nil { if err != nil {
return nil, err return nil, err
@ -83,7 +68,7 @@ func LoadChartRepository(dir string) (*ChartRepository, error) {
return nil, errors.New(dir + "is not a directory") 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 { filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() { if !f.IsDir() {
@ -104,46 +89,17 @@ func LoadChartRepository(dir string) (*ChartRepository, error) {
return r, nil return r, nil
} }
// UnmarshalYAML unmarshals the index file func (r *ChartRepository) saveIndexFile() error {
func (i *IndexFile) UnmarshalYAML(unmarshal func(interface{}) error) error { index, err := yaml.Marshal(&r.IndexFile.Entries)
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 { if err != nil {
return nil, err return err
} }
var indexfile IndexFile if err = ioutil.WriteFile(filepath.Join(r.RootPath, indexPath), index, 0644); err != nil {
err = yaml.Unmarshal(b, &indexfile) return err
if err != nil {
return nil, err
} }
return &indexfile, nil return nil
} }
func (r *ChartRepository) Index() error { func (r *ChartRepository) Index() error {
@ -163,7 +119,7 @@ func (r *ChartRepository) Index() error {
r.IndexFile.Entries = make(map[string]*ChartRef) 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: generate hash of contents of chart and add to the entry
//TODO: Set created timestamp //TODO: Set created timestamp
@ -178,16 +134,3 @@ func (r *ChartRepository) Index() error {
return nil 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
}

@ -10,6 +10,7 @@ import (
const testfile = "testdata/local-index.yaml" const testfile = "testdata/local-index.yaml"
const testRepositoriesFile = "testdata/repositories.yaml" const testRepositoriesFile = "testdata/repositories.yaml"
const testRepository = "testdata/repository" const testRepository = "testdata/repository"
const testURL = "http://example-charts.com"
func TestLoadIndexFile(t *testing.T) { func TestLoadIndexFile(t *testing.T) {
cf, err := LoadIndexFile(testfile) cf, err := LoadIndexFile(testfile)
@ -72,7 +73,7 @@ func TestLoadRepositoriesFile(t *testing.T) {
} }
func TestLoadChartRepository(t *testing.T) { func TestLoadChartRepository(t *testing.T) {
cr, err := LoadChartRepository(testRepository) cr, err := LoadChartRepository(testRepository, testURL)
if err != nil { if err != nil {
t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) 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) { if !reflect.DeepEqual(cr.ChartPaths, paths) {
t.Errorf("Expected %#v but got %#v\n", paths, cr.ChartPaths) 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) { func TestIndex(t *testing.T) {
cr, err := LoadChartRepository(testRepository) cr, err := LoadChartRepository(testRepository, testURL)
if err != nil { if err != nil {
t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) t.Errorf("Problem loading chart repository from %s: %v", testRepository, err)
} }

Loading…
Cancel
Save