From 415e52bf555f28d0262f9d75ce8a38982c23f714 Mon Sep 17 00:00:00 2001
From: Amanda Cameron <amanda@darkdna.net>
Date: Sun, 19 Feb 2017 16:19:38 -0500
Subject: [PATCH] Add support for sub-directory scanning as in issue #1401

---
 pkg/chartutil/save.go                         |   8 ++++
 pkg/repo/chartrepo_test.go                    |  13 +++++-
 pkg/repo/index.go                             |  21 +++++++++-
 pkg/repo/index_test.go                        |  38 +++++++++++-------
 .../repository/universe/zarthal-1.0.0.tgz     | Bin 0 -> 1121 bytes
 5 files changed, 61 insertions(+), 19 deletions(-)
 create mode 100644 pkg/repo/testdata/repository/universe/zarthal-1.0.0.tgz

diff --git a/pkg/chartutil/save.go b/pkg/chartutil/save.go
index e3b5c1afd..b89917d96 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 f6d6df74e..9f1bc995a 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 4e59b8d58..174ceea01 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 17a1cc209..ba426b174 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
GIT binary patch
literal 1121
zcmV-n1fKgJiwFR8Z?Rbb1MQs8ZsSB8$7fgK5{X>|Jiw41SM|TK<0R5btPr9cKu8q0
z>}6t48mo3DvYr0fW#I)la^c9GJ7?a27vK#zz#H&$5+{w*B)c?qx9#_%{xr77o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