github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/repo/index.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package repo
    18  
    19  import (
    20  	"io/ioutil"
    21  	"net/http"
    22  	"strings"
    23  
    24  	"gopkg.in/yaml.v2"
    25  
    26  	"k8s.io/helm/pkg/proto/hapi/chart"
    27  )
    28  
    29  var indexPath = "index.yaml"
    30  
    31  // IndexFile represents the index file in a chart repository
    32  type IndexFile struct {
    33  	Entries map[string]*ChartRef
    34  }
    35  
    36  // ChartRef represents a chart entry in the IndexFile
    37  type ChartRef struct {
    38  	Name      string          `yaml:"name"`
    39  	URL       string          `yaml:"url"`
    40  	Created   string          `yaml:"created,omitempty"`
    41  	Removed   bool            `yaml:"removed,omitempty"`
    42  	Checksum  string          `yaml:"checksum,omitempty"`
    43  	Chartfile *chart.Metadata `yaml:"chartfile"`
    44  }
    45  
    46  // DownloadIndexFile uses
    47  func DownloadIndexFile(repoName, url, indexFilePath string) error {
    48  	var indexURL string
    49  
    50  	indexURL = strings.TrimSuffix(url, "/") + "/index.yaml"
    51  	resp, err := http.Get(indexURL)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	defer resp.Body.Close()
    56  
    57  	var r IndexFile
    58  
    59  	b, err := ioutil.ReadAll(resp.Body)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	if err := yaml.Unmarshal(b, &r); err != nil {
    65  		return err
    66  	}
    67  
    68  	return ioutil.WriteFile(indexFilePath, b, 0644)
    69  }
    70  
    71  // UnmarshalYAML unmarshals the index file
    72  func (i *IndexFile) UnmarshalYAML(unmarshal func(interface{}) error) error {
    73  	var refs map[string]*ChartRef
    74  	if err := unmarshal(&refs); err != nil {
    75  		if _, ok := err.(*yaml.TypeError); !ok {
    76  			return err
    77  		}
    78  	}
    79  	i.Entries = refs
    80  	return nil
    81  }
    82  
    83  func (i *IndexFile) addEntry(name string, url string) ([]byte, error) {
    84  	if i.Entries == nil {
    85  		i.Entries = make(map[string]*ChartRef)
    86  	}
    87  	entry := ChartRef{Name: name, URL: url}
    88  	i.Entries[name] = &entry
    89  	out, err := yaml.Marshal(&i.Entries)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	return out, nil
    95  }
    96  
    97  // LoadIndexFile takes a file at the given path and returns an IndexFile object
    98  func LoadIndexFile(path string) (*IndexFile, error) {
    99  	b, err := ioutil.ReadFile(path)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	var indexfile IndexFile
   105  	err = yaml.Unmarshal(b, &indexfile)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	return &indexfile, nil
   111  }