From 151f9bf34dee23c90ae26d76c1628a2acea4d22b Mon Sep 17 00:00:00 2001 From: Seyed Mohammad Mahdi Hatami Date: Fri, 16 Jun 2023 14:48:44 +0330 Subject: [PATCH] json index supperting implemented, a simple test for loadIndex added Signed-off-by: Seyed Mohammad Mahdi Hatami --- pkg/repo/index.go | 13 ++++- pkg/repo/index_test.go | 5 ++ pkg/repo/testdata/local-index-json.json | 73 +++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 pkg/repo/testdata/local-index-json.json diff --git a/pkg/repo/index.go b/pkg/repo/index.go index ba2e365c8..2050dd575 100644 --- a/pkg/repo/index.go +++ b/pkg/repo/index.go @@ -18,6 +18,7 @@ package repo import ( "bytes" + "encoding/json" "log" "os" "path" @@ -332,12 +333,20 @@ func IndexDirectory(dir, baseURL string) (*IndexFile, error) { func loadIndex(data []byte, source string) (*IndexFile, error) { i := &IndexFile{} + // TODO : add error for empty json if len(data) == 0 { return i, ErrEmptyIndexYaml } - if err := yaml.UnmarshalStrict(data, i); err != nil { - return i, err + // TODO : check file type, if json, unmarshal with json. + if strings.HasSuffix(source, ".json") { + if err := json.Unmarshal(data, i); err != nil { + return i, err + } + } else { + if err := yaml.UnmarshalStrict(data, i); err != nil { + return i, err + } } for name, cvs := range i.Entries { diff --git a/pkg/repo/index_test.go b/pkg/repo/index_test.go index bbc48c97e..68752fa05 100644 --- a/pkg/repo/index_test.go +++ b/pkg/repo/index_test.go @@ -34,6 +34,7 @@ import ( const ( testfile = "testdata/local-index.yaml" + jsontestfile = "testdata/local-index-json.json" annotationstestfile = "testdata/local-index-annotations.yaml" chartmuseumtestfile = "testdata/chartmuseum-index.yaml" unorderedTestfile = "testdata/local-index-unordered.yaml" @@ -145,6 +146,10 @@ func TestLoadIndex(t *testing.T) { Name: "chartmuseum index file", Filename: chartmuseumtestfile, }, + { + Name: "json index file", + Filename: jsontestfile, + }, } for _, tc := range tests { diff --git a/pkg/repo/testdata/local-index-json.json b/pkg/repo/testdata/local-index-json.json new file mode 100644 index 000000000..14c8ab143 --- /dev/null +++ b/pkg/repo/testdata/local-index-json.json @@ -0,0 +1,73 @@ +{ + "apiVersion": "v1", + "entries": { + "nginx": [ + { + "urls": [ + "https://charts.helm.sh/stable/nginx-0.2.0.tgz" + ], + "name": "nginx", + "description": "string", + "version": "0.2.0", + "home": "https://github.com/something/else", + "digest": "sha256:1234567890abcdef", + "keywords": [ + "popular", + "web server", + "proxy" + ], + "apiVersion": "v2" + }, + { + "urls": [ + "https://charts.helm.sh/stable/nginx-0.1.0.tgz" + ], + "name": "nginx", + "description": "string", + "version": "0.1.0", + "home": "https://github.com/something", + "digest": "sha256:1234567890abcdef", + "keywords": [ + "popular", + "web server", + "proxy" + ], + "apiVersion": "v2" + } + ], + "alpine": [ + { + "urls": [ + "https://charts.helm.sh/stable/alpine-1.0.0.tgz", + "http://storage2.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz" + ], + "name": "alpine", + "description": "string", + "version": "1.0.0", + "home": "https://github.com/something", + "keywords": [ + "linux", + "alpine", + "small", + "sumtin" + ], + "digest": "sha256:1234567890abcdef", + "apiVersion": "v2" + } + ], + "chartWithNoURL": [ + { + "name": "chartWithNoURL", + "description": "string", + "version": "1.0.0", + "home": "https://github.com/something", + "keywords": [ + "small", + "sumtin" + ], + "digest": "sha256:1234567890abcdef", + "apiVersion": "v2" + } + ] + } +} \ No newline at end of file