diff --git a/pkg/chartutil/save.go b/pkg/chartutil/save.go index e3b5c1afd3925e2fc935fc6e29fc472e2d61f908..b89917d96334a93558ddcdf563f31d9b7fb8a7b3 100644 --- a/pkg/chartutil/save.go +++ b/pkg/chartutil/save.go @@ -115,6 +115,14 @@ func Save(c *chart.Chart, outDir string) (string, error) { filename := fmt.Sprintf("%s-%s.tgz", cfile.Name, cfile.Version) filename = filepath.Join(outDir, filename) + if stat, err := os.Stat(filepath.Dir(filename)); os.IsNotExist(err) { + if err := os.MkdirAll(filepath.Dir(filename), 0755); !os.IsExist(err) { + return "", err + } + } else if !stat.IsDir() { + return "", fmt.Errorf("is not a directory: %s", filepath.Dir(filename)) + } + f, err := os.Create(filename) if err != nil { return "", err diff --git a/pkg/repo/chartrepo_test.go b/pkg/repo/chartrepo_test.go index f6d6df74e9a43ed86eeef68d2adaf26cdbd66150..9f1bc995a4bfdf8a58ba9095acd7823d7b128e04 100644 --- a/pkg/repo/chartrepo_test.go +++ b/pkg/repo/chartrepo_test.go @@ -54,6 +54,7 @@ func TestLoadChartRepository(t *testing.T) { filepath.Join(testRepository, "frobnitz-1.2.3.tgz"), filepath.Join(testRepository, "sprocket-1.1.0.tgz"), filepath.Join(testRepository, "sprocket-1.2.0.tgz"), + filepath.Join(testRepository, "universe/zarthal-1.0.0.tgz"), } if r.Config.Name != testRepository { @@ -118,8 +119,8 @@ func verifyIndex(t *testing.T, actual *IndexFile) { } entries := actual.Entries - if numEntries := len(entries); numEntries != 2 { - t.Errorf("Expected 2 charts to be listed in index file but got %v", numEntries) + if numEntries := len(entries); numEntries != 3 { + t.Errorf("Expected 3 charts to be listed in index file but got %v", numEntries) } expects := map[string]ChartVersions{ @@ -145,6 +146,14 @@ func verifyIndex(t *testing.T, actual *IndexFile) { }, }, }, + "zarthal": { + { + Metadata: &chart.Metadata{ + Name: "zarthal", + Version: "1.0.0", + }, + }, + }, } for name, versions := range expects { diff --git a/pkg/repo/index.go b/pkg/repo/index.go index 4e59b8d589912fce0945bd94b263d0b811afc272..174ceea018b9734dd4c05990094be9f85c1ceaf6 100644 --- a/pkg/repo/index.go +++ b/pkg/repo/index.go @@ -231,9 +231,26 @@ func IndexDirectory(dir, baseURL string) (*IndexFile, error) { if err != nil { return nil, err } + moreArchives, err := filepath.Glob(filepath.Join(dir, "**/*.tgz")) + if err != nil { + return nil, err + } + archives = append(archives, moreArchives...) + index := NewIndexFile() for _, arch := range archives { - fname := filepath.Base(arch) + fname, err := filepath.Rel(dir, arch) + if err != nil { + return index, err + } + + var parentDir string + parentDir, fname = filepath.Split(fname) + parentURL, err := urlutil.URLJoin(baseURL, parentDir) + if err != nil { + parentURL = filepath.Join(baseURL, parentDir) + } + c, err := chartutil.Load(arch) if err != nil { // Assume this is not a chart. @@ -243,7 +260,7 @@ func IndexDirectory(dir, baseURL string) (*IndexFile, error) { if err != nil { return index, err } - index.Add(c.Metadata, fname, baseURL, hash) + index.Add(c.Metadata, fname, parentURL, hash) } return index, nil } diff --git a/pkg/repo/index_test.go b/pkg/repo/index_test.go index 17a1cc209308f7650ed3cdcb2019a4ad0eb3d3f4..ba426b17482727f97821fffc3a52a644dbc89a78 100644 --- a/pkg/repo/index_test.go +++ b/pkg/repo/index_test.go @@ -278,27 +278,35 @@ func TestIndexDirectory(t *testing.T) { t.Fatal(err) } - if l := len(index.Entries); l != 2 { - t.Fatalf("Expected 2 entries, got %d", l) + if l := len(index.Entries); l != 3 { + t.Fatalf("Expected 3 entries, got %d", l) } // Other things test the entry generation more thoroughly. We just test a // few fields. - cname := "frobnitz" - frobs, ok := index.Entries[cname] - if !ok { - t.Fatalf("Could not read chart %s", cname) - } - frob := frobs[0] - if len(frob.Digest) == 0 { - t.Errorf("Missing digest of file %s.", frob.Name) + corpus := []struct{ chartName, downloadLink string }{ + {"frobnitz", "http://localhost:8080/frobnitz-1.2.3.tgz"}, + {"zarthal", "http://localhost:8080/universe/zarthal-1.0.0.tgz"}, } - if frob.URLs[0] != "http://localhost:8080/frobnitz-1.2.3.tgz" { - t.Errorf("Unexpected URLs: %v", frob.URLs) - } - if frob.Name != "frobnitz" { - t.Errorf("Expected frobnitz, got %q", frob.Name) + + for _, test := range corpus { + cname := test.chartName + frobs, ok := index.Entries[cname] + if !ok { + t.Fatalf("Could not read chart %s", cname) + } + + frob := frobs[0] + if len(frob.Digest) == 0 { + t.Errorf("Missing digest of file %s.", frob.Name) + } + if frob.URLs[0] != test.downloadLink { + t.Errorf("Unexpected URLs: %v", frob.URLs) + } + if frob.Name != cname { + t.Errorf("Expected %q, got %q", cname, frob.Name) + } } } diff --git a/pkg/repo/testdata/repository/universe/zarthal-1.0.0.tgz b/pkg/repo/testdata/repository/universe/zarthal-1.0.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..90cb34bd5f36439ee2e986c21c766b55a4442ec8 Binary files /dev/null and b/pkg/repo/testdata/repository/universe/zarthal-1.0.0.tgz differ