added json supporting to DownloadIndexFile

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

@ -98,7 +98,7 @@ func (r *ChartRepository) Load() error {
// what repos to use?
filepath.Walk(r.Config.Name, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
if strings.Contains(f.Name(), "-index.yaml") {
if strings.Contains(f.Name(), "-index.json") || strings.Contains(f.Name(), "-index.yaml") {
i, err := LoadIndexFile(path)
if err != nil {
return err
@ -115,12 +115,16 @@ func (r *ChartRepository) Load() error {
// DownloadIndexFile fetches the index from a repository.
func (r *ChartRepository) DownloadIndexFile() (string, error) {
indexURL, err := ResolveReferenceURL(r.Config.URL, "index.yaml")
jsonIndexURL, err := ResolveReferenceURL(r.Config.URL, "index.json")
if err != nil {
return "", err
}
yamlIndexURL, err := ResolveReferenceURL(r.Config.URL, "index.yaml")
if err != nil {
return "", err
}
resp, err := r.Client.Get(indexURL,
resp, err := r.Client.Get(jsonIndexURL,
getter.WithURL(r.Config.URL),
getter.WithInsecureSkipVerifyTLS(r.Config.InsecureSkipTLSverify),
getter.WithTLSClientConfig(r.Config.CertFile, r.Config.KeyFile, r.Config.CAFile),
@ -136,11 +140,30 @@ func (r *ChartRepository) DownloadIndexFile() (string, error) {
return "", err
}
indexFile, err := loadIndex(index, r.Config.URL)
indexFile, err := loadIndex(index, jsonIndexURL)
if err != nil {
resp, err = r.Client.Get(yamlIndexURL,
getter.WithURL(r.Config.URL),
getter.WithInsecureSkipVerifyTLS(r.Config.InsecureSkipTLSverify),
getter.WithTLSClientConfig(r.Config.CertFile, r.Config.KeyFile, r.Config.CAFile),
getter.WithBasicAuth(r.Config.Username, r.Config.Password),
getter.WithPassCredentialsAll(r.Config.PassCredentialsAll),
)
if err != nil {
return "", err
}
index, err = io.ReadAll(resp)
if err != nil {
return "", err
}
indexFile, err = loadIndex(index, yamlIndexURL)
if err != nil {
return "", err
}
}
// Create the chart list file in the cache directory
var charts strings.Builder
for name := range indexFile.Entries {

@ -52,6 +52,8 @@ var (
ErrNoChartName = errors.New("no chart name found")
// ErrEmptyIndexYaml indicates that the content of index.yaml is empty.
ErrEmptyIndexYaml = errors.New("empty index.yaml file")
// ErrEmptyIndexJson indicates that the content of index.json is empty.
ErrEmptyIndexJson = errors.New("empty index.json file")
)
// ChartVersions is a list of versioned chart references.
@ -332,14 +334,18 @@ 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{}
isjson := strings.HasSuffix(source, ".json")
// TODO : add error for empty json
if len(data) == 0 {
if isjson {
return i, ErrEmptyIndexJson
} else {
return i, ErrEmptyIndexYaml
}
}
// TODO : check file type, if json, unmarshal with json.
if strings.HasSuffix(source, ".json") {
if isjson {
if err := json.Unmarshal(data, i); err != nil {
return i, err
}

Loading…
Cancel
Save