-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package helm | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"github.com/ghodss/yaml" | ||
"k8s.io/helm/pkg/repo" | ||
) | ||
|
||
type ( | ||
// Index represents the index file in a chart repository | ||
Index struct { | ||
*repo.IndexFile | ||
ContextPath string `json:"contextPath"` | ||
} | ||
|
||
// IndexDownloader is a function to download the index | ||
IndexDownloader func() ([]byte, error) | ||
) | ||
|
||
// GetIndexByRepo returns index by repository | ||
func GetIndexByRepo(repo *Repo, downloadIndex IndexDownloader) (*Index, error) { | ||
if repo.Cache != "" { | ||
return GetIndexByDownloader(func() ([]byte, error) { | ||
return ioutil.ReadFile(repo.Cache) | ||
}) | ||
} | ||
return GetIndexByDownloader(downloadIndex) | ||
} | ||
|
||
// GetIndexByDownloader takes binary data from IndexDownloader and returns an Index object | ||
func GetIndexByDownloader(downloadIndex IndexDownloader) (*Index, error) { | ||
b, err := downloadIndex() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return LoadIndex(b) | ||
} | ||
|
||
// LoadIndex loads an index file | ||
func LoadIndex(data []byte) (*Index, error) { | ||
i := &Index{} | ||
if err := yaml.Unmarshal(data, i); err != nil { | ||
return i, err | ||
} | ||
i.SortEntries() | ||
return i, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package helm | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestLoadIndex(t *testing.T) { | ||
// No context path | ||
index, err := LoadIndex([]byte("apiVersion: v1\nentries: {}\ngenerated: \"2018-08-08T08:21:33Z\"\n")) | ||
if err != nil { | ||
t.Error("unexpected error loading index", err) | ||
} | ||
if index.ContextPath != "" { | ||
t.Errorf("expexted empty context path, instead got %s", index.ContextPath) | ||
} | ||
|
||
// Has context path | ||
index, err = LoadIndex([]byte("apiVersion: v1\ncontextPath: /helm/v1\nentries: {}\ngenerated: \"2018-08-08T08:21:33Z\"\n")) | ||
if err != nil { | ||
t.Error("unexpected error loading index", err) | ||
} | ||
if index.ContextPath != "/helm/v1" { | ||
t.Errorf("expexted context path to be /helm/v1, instead got %s", index.ContextPath) | ||
} | ||
} |