From 20e2aec79365d0ec6c3230a7d0288d9a386bbc6a Mon Sep 17 00:00:00 2001 From: yxxhero Date: Fri, 4 Jun 2021 22:49:24 +0800 Subject: [PATCH] Add a more friendly prompt when index.yaml is empty. Signed-off-by: yxxhero --- pkg/repo/index.go | 7 +++++++ pkg/repo/index_test.go | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/repo/index.go b/pkg/repo/index.go index e86b17349..1b65ac497 100644 --- a/pkg/repo/index.go +++ b/pkg/repo/index.go @@ -50,6 +50,8 @@ var ( ErrNoChartVersion = errors.New("no chart version found") // ErrNoChartName indicates that a chart with the given name is not found. ErrNoChartName = errors.New("no chart name found") + // ErrEmptyIndexYaml indicates that the content of index.yaml is empty. + ErrEmptyIndexYaml = errors.New("empty index.yaml file") ) // ChartVersions is a list of versioned chart references. @@ -326,6 +328,11 @@ func IndexDirectory(dir, baseURL string) (*IndexFile, error) { // This will fail if API Version is not set (ErrNoAPIVersion) or if the unmarshal fails. func loadIndex(data []byte, source string) (*IndexFile, error) { i := &IndexFile{} + + if len(data) == 0 { + return i, ErrEmptyIndexYaml + } + if err := yaml.UnmarshalStrict(data, i); err != nil { return i, err } diff --git a/pkg/repo/index_test.go b/pkg/repo/index_test.go index 47f3c6b2e..292856527 100644 --- a/pkg/repo/index_test.go +++ b/pkg/repo/index_test.go @@ -152,6 +152,12 @@ func TestLoadIndex_Duplicates(t *testing.T) { } } +func TestLoadIndex_Empty(t *testing.T) { + if _, err := loadIndex([]byte(""), "indexWithEmpty"); err == nil { + t.Errorf("Expected an error when index.yaml is empty.") + } +} + func TestLoadIndexFileAnnotations(t *testing.T) { i, err := LoadIndexFile(annotationstestfile) if err != nil {