json index supperting implemented, a simple test for loadIndex added

Signed-off-by: Seyed Mohammad Mahdi Hatami <hatamik7@gmail.com>
pull/12151/head
Seyed Mohammad Mahdi Hatami 2 years ago
parent 4e447d87cd
commit 151f9bf34d

@ -18,6 +18,7 @@ package repo
import ( import (
"bytes" "bytes"
"encoding/json"
"log" "log"
"os" "os"
"path" "path"
@ -332,13 +333,21 @@ func IndexDirectory(dir, baseURL string) (*IndexFile, error) {
func loadIndex(data []byte, source string) (*IndexFile, error) { func loadIndex(data []byte, source string) (*IndexFile, error) {
i := &IndexFile{} i := &IndexFile{}
// TODO : add error for empty json
if len(data) == 0 { if len(data) == 0 {
return i, ErrEmptyIndexYaml return i, ErrEmptyIndexYaml
} }
// 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 { if err := yaml.UnmarshalStrict(data, i); err != nil {
return i, err return i, err
} }
}
for name, cvs := range i.Entries { for name, cvs := range i.Entries {
for idx := len(cvs) - 1; idx >= 0; idx-- { for idx := len(cvs) - 1; idx >= 0; idx-- {

@ -34,6 +34,7 @@ import (
const ( const (
testfile = "testdata/local-index.yaml" testfile = "testdata/local-index.yaml"
jsontestfile = "testdata/local-index-json.json"
annotationstestfile = "testdata/local-index-annotations.yaml" annotationstestfile = "testdata/local-index-annotations.yaml"
chartmuseumtestfile = "testdata/chartmuseum-index.yaml" chartmuseumtestfile = "testdata/chartmuseum-index.yaml"
unorderedTestfile = "testdata/local-index-unordered.yaml" unorderedTestfile = "testdata/local-index-unordered.yaml"
@ -145,6 +146,10 @@ func TestLoadIndex(t *testing.T) {
Name: "chartmuseum index file", Name: "chartmuseum index file",
Filename: chartmuseumtestfile, Filename: chartmuseumtestfile,
}, },
{
Name: "json index file",
Filename: jsontestfile,
},
} }
for _, tc := range tests { for _, tc := range tests {

@ -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"
}
]
}
}
Loading…
Cancel
Save