Commit 9b732da7 authored by Michelle Noorali's avatar Michelle Noorali

ref(helm): rename cache file to index file

* also change additional references to cache to index
* see issue #665
parent 613ce7e1
......@@ -100,18 +100,18 @@ func ensureHome() error {
return fmt.Errorf("%s must be a file, not a directory", repoFile)
}
localRepoCacheFile := localRepoDirectory(localRepoCacheFilePath)
if fi, err := os.Stat(localRepoCacheFile); err != nil {
fmt.Printf("Creating %s \n", localRepoCacheFile)
_, err := os.Create(localRepoCacheFile)
localRepoIndexFile := localRepoDirectory(localRepoIndexFilePath)
if fi, err := os.Stat(localRepoIndexFile); err != nil {
fmt.Printf("Creating %s \n", localRepoIndexFile)
_, err := os.Create(localRepoIndexFile)
if err != nil {
return err
}
//TODO: take this out and replace with helm update functionality
os.Symlink(localRepoCacheFile, cacheDirectory("local-cache.yaml"))
os.Symlink(localRepoIndexFile, cacheDirectory("local-index.yaml"))
} else if fi.IsDir() {
return fmt.Errorf("%s must be a file, not a directory", localRepoCacheFile)
return fmt.Errorf("%s must be a file, not a directory", localRepoIndexFile)
}
fmt.Printf("$HELM_HOME has been configured at %s.\n", helmHome)
......
......@@ -28,7 +28,7 @@ func TestEnsureHome(t *testing.T) {
t.Errorf("%s should not be a directory", fi)
}
if fi, err := os.Stat(localRepoDirectory(localRepoCacheFilePath)); err != nil {
if fi, err := os.Stat(localRepoDirectory(localRepoIndexFilePath)); err != nil {
t.Errorf("%s", err)
} else if fi.IsDir() {
t.Errorf("%s should not be a directory", fi)
......
......@@ -47,7 +47,7 @@ func runRepoAdd(cmd *cobra.Command, args []string) error {
}
name, url := args[0], args[1]
if err := downloadCacheFile(name, url); err != nil {
if err := downloadIndexFile(name, url); err != nil {
return errors.New("Oops! Looks like " + url + " is not a valid chart repository or cannot be reached\n")
}
......
......@@ -64,9 +64,9 @@ func searchCacheForPattern(dir string, search string) ([]string, error) {
})
matches := []string{}
for _, f := range fileList {
cache, _ := repo.LoadCacheFile(f)
m := searchChartRefsForPattern(search, cache.Entries)
repoName := strings.TrimSuffix(filepath.Base(f), "-cache.yaml")
index, _ := repo.LoadIndexFile(f)
m := searchChartRefsForPattern(search, index.Entries)
repoName := strings.TrimSuffix(filepath.Base(f), "-index.yaml")
for _, c := range m {
matches = append(matches, repoName+"/"+c)
}
......
......@@ -7,7 +7,7 @@ import (
)
const testDir = "testdata/"
const testFile = "testdata/local-cache.yaml"
const testFile = "testdata/local-index.yaml"
type searchTestCase struct {
in string
......@@ -49,9 +49,9 @@ func validateEntries(t *testing.T, in string, found []string, expected []string)
}
func searchTestRunner(t *testing.T, tc searchTestCase) {
cf, err := repo.LoadCacheFile(testFile)
cf, err := repo.LoadIndexFile(testFile)
if err != nil {
t.Errorf("Failed to load cache file : %s : %s", testFile, err)
t.Errorf("Failed to load index file : %s : %s", testFile, err)
}
u := searchChartRefsForPattern(tc.in, cf.Entries)
......
......@@ -9,7 +9,7 @@ const (
repositoriesFilePath string = "repositories.yaml"
cachePath string = "cache"
localRepoPath string = "local"
localRepoCacheFilePath string = "cache.yaml"
localRepoIndexFilePath string = "index.yaml"
)
func homePath() string {
......
......@@ -46,7 +46,7 @@ func updateCharts(repos map[string]string, verbose bool) {
wg.Add(1)
go func(n, u string) {
defer wg.Done()
err := downloadCacheFile(n, u)
err := downloadIndexFile(n, u)
if err != nil {
updateErr := "...Unable to get an update from the " + n + " chart repository"
if verbose {
......@@ -62,17 +62,17 @@ func updateCharts(repos map[string]string, verbose bool) {
fmt.Println("Update Complete. Happy Helming!")
}
func downloadCacheFile(name, url string) error {
var cacheURL string
func downloadIndexFile(name, url string) error {
var indexURL string
cacheURL = strings.TrimSuffix(url, "/") + "/cache.yaml"
resp, err := http.Get(cacheURL)
indexURL = strings.TrimSuffix(url, "/") + "/index.yaml"
resp, err := http.Get(indexURL)
if err != nil {
return err
}
defer resp.Body.Close()
var cacheFile *os.File
var indexFile *os.File
var r repo.RepoFile
b, err := ioutil.ReadAll(resp.Body)
......@@ -84,12 +84,12 @@ func downloadCacheFile(name, url string) error {
return err
}
cacheFile, err = os.Create(cacheDirectory(name + "-cache.yaml"))
indexFile, err = os.Create(cacheDirectory(name + "-index.yaml"))
if err != nil {
return err
}
if _, err := io.Copy(cacheFile, resp.Body); err != nil {
if _, err := io.Copy(indexFile, resp.Body); err != nil {
return err
}
......
......@@ -13,12 +13,12 @@ import (
var localRepoPath string
// CacheFile represents the cache file in a chart repository
type CacheFile struct {
// IndexFile represents the index file in a chart repository
type IndexFile struct {
Entries map[string]*ChartRef
}
// ChartRef represents a chart entry in the CacheFile
// ChartRef represents a chart entry in the IndexFile
type ChartRef struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
......@@ -43,8 +43,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
serveFile(w, r, file)
} else if file == "" {
fmt.Fprintf(w, "list of charts should be here at some point")
} else if file == "cache" {
fmt.Fprintf(w, "cache file data should be here at some point")
} else if file == "index" {
fmt.Fprintf(w, "index file data should be here at some point")
} else {
fmt.Fprintf(w, "Ummm... Nothing to see here folks")
}
......@@ -54,13 +54,13 @@ func serveFile(w http.ResponseWriter, r *http.Request, file string) {
http.ServeFile(w, r, filepath.Join(localRepoPath, file))
}
// AddChartToLocalRepo saves a chart in the given path and then reindexes the cache file
// AddChartToLocalRepo saves a chart in the given path and then reindexes the index file
func AddChartToLocalRepo(ch *chart.Chart, path string) error {
name, err := chart.Save(ch, path)
if err != nil {
return err
}
err = ReindexCacheFile(ch, path+"/cache.yaml")
err = Reindex(ch, path+"/index.yaml")
if err != nil {
return err
}
......@@ -68,15 +68,15 @@ func AddChartToLocalRepo(ch *chart.Chart, path string) error {
return nil
}
// LoadCacheFile takes a file at the given path and returns a CacheFile object
func LoadCacheFile(path string) (*CacheFile, error) {
// 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
}
//TODO: change variable name - y is not helpful :P
var y CacheFile
var y IndexFile
err = yaml.Unmarshal(b, &y)
if err != nil {
return nil, err
......@@ -84,10 +84,10 @@ func LoadCacheFile(path string) (*CacheFile, error) {
return &y, nil
}
// ReindexCacheFile adds an entry to the cache file at the given path
func ReindexCacheFile(ch *chart.Chart, path string) error {
// Reindex adds an entry to the index file at the given path
func Reindex(ch *chart.Chart, path string) error {
name := ch.Chartfile().Name + "-" + ch.Chartfile().Version
y, err := LoadCacheFile(path)
y, err := LoadIndexFile(path)
if err != nil {
return err
}
......@@ -111,25 +111,25 @@ func ReindexCacheFile(ch *chart.Chart, path string) error {
return nil
}
// UnmarshalYAML unmarshals the cache file
func (c *CacheFile) UnmarshalYAML(unmarshal func(interface{}) error) error {
// 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
}
}
c.Entries = refs
i.Entries = refs
return nil
}
func (c *CacheFile) insertChartEntry(name string, url string) ([]byte, error) {
if c.Entries == nil {
c.Entries = make(map[string]*ChartRef)
func (i *IndexFile) insertChartEntry(name string, url string) ([]byte, error) {
if i.Entries == nil {
i.Entries = make(map[string]*ChartRef)
}
entry := ChartRef{Name: name, URL: url}
c.Entries[name] = &entry
out, err := yaml.Marshal(&c.Entries)
i.Entries[name] = &entry
out, err := yaml.Marshal(&i.Entries)
if err != nil {
return nil, err
}
......
......@@ -4,15 +4,15 @@ import (
"testing"
)
const testfile = "testdata/local-cache.yaml"
const testfile = "testdata/local-index.yaml"
func TestLoadCacheFile(t *testing.T) {
cf, err := LoadCacheFile(testfile)
func TestLoadIndexFile(t *testing.T) {
cf, err := LoadIndexFile(testfile)
if err != nil {
t.Errorf("Failed to load cachefile: %s", err)
t.Errorf("Failed to load index file: %s", err)
}
if len(cf.Entries) != 2 {
t.Errorf("Expected 2 entries in the cache file, but got %d", len(cf.Entries))
t.Errorf("Expected 2 entries in the index file, but got %d", len(cf.Entries))
}
nginx := false
alpine := false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment