github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/repo/repo.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 // import "k8s.io/helm/pkg/repo"
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"time"
    25  
    26  	"github.com/ghodss/yaml"
    27  )
    28  
    29  // ErrRepoOutOfDate indicates that the repository file is out of date, but
    30  // is fixable.
    31  var ErrRepoOutOfDate = errors.New("repository file is out of date")
    32  
    33  // RepoFile represents the repositories.yaml file in $HELM_HOME
    34  type RepoFile struct {
    35  	APIVersion   string    `json:"apiVersion"`
    36  	Generated    time.Time `json:"generated"`
    37  	Repositories []*Entry  `json:"repositories"`
    38  }
    39  
    40  // NewRepoFile generates an empty repositories file.
    41  //
    42  // Generated and APIVersion are automatically set.
    43  func NewRepoFile() *RepoFile {
    44  	return &RepoFile{
    45  		APIVersion:   APIVersionV1,
    46  		Generated:    time.Now(),
    47  		Repositories: []*Entry{},
    48  	}
    49  }
    50  
    51  // LoadRepositoriesFile takes a file at the given path and returns a RepoFile object
    52  //
    53  // If this returns ErrRepoOutOfDate, it also returns a recovered RepoFile that
    54  // can be saved as a replacement to the out of date file.
    55  func LoadRepositoriesFile(path string) (*RepoFile, error) {
    56  	b, err := ioutil.ReadFile(path)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	r := &RepoFile{}
    62  	err = yaml.Unmarshal(b, r)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	// File is either corrupt, or is from before v2.0.0-Alpha.5
    68  	if r.APIVersion == "" {
    69  		m := map[string]string{}
    70  		if err = yaml.Unmarshal(b, &m); err != nil {
    71  			return nil, err
    72  		}
    73  		r := NewRepoFile()
    74  		for k, v := range m {
    75  			r.Add(&Entry{
    76  				Name:  k,
    77  				URL:   v,
    78  				Cache: fmt.Sprintf("%s-index.yaml", k),
    79  			})
    80  		}
    81  		return r, ErrRepoOutOfDate
    82  	}
    83  
    84  	return r, nil
    85  }
    86  
    87  // Add adds one or more repo entries to a repo file.
    88  func (r *RepoFile) Add(re ...*Entry) {
    89  	r.Repositories = append(r.Repositories, re...)
    90  }
    91  
    92  // Update attempts to replace one or more repo entries in a repo file. If an
    93  // entry with the same name doesn't exist in the repo file it will add it.
    94  func (r *RepoFile) Update(re ...*Entry) {
    95  	for _, target := range re {
    96  		found := false
    97  		for j, repo := range r.Repositories {
    98  			if repo.Name == target.Name {
    99  				r.Repositories[j] = target
   100  				found = true
   101  				break
   102  			}
   103  		}
   104  		if !found {
   105  			r.Add(target)
   106  		}
   107  	}
   108  }
   109  
   110  // Has returns true if the given name is already a repository name.
   111  func (r *RepoFile) Has(name string) bool {
   112  	for _, rf := range r.Repositories {
   113  		if rf.Name == name {
   114  			return true
   115  		}
   116  	}
   117  	return false
   118  }
   119  
   120  // Remove removes the entry from the list of repositories.
   121  func (r *RepoFile) Remove(name string) bool {
   122  	cp := []*Entry{}
   123  	found := false
   124  	for _, rf := range r.Repositories {
   125  		if rf.Name == name {
   126  			found = true
   127  			continue
   128  		}
   129  		cp = append(cp, rf)
   130  	}
   131  	r.Repositories = cp
   132  	return found
   133  }
   134  
   135  // WriteFile writes a repositories file to the given path.
   136  func (r *RepoFile) WriteFile(path string, perm os.FileMode) error {
   137  	data, err := yaml.Marshal(r)
   138  	if err != nil {
   139  		return err
   140  	}
   141  	return ioutil.WriteFile(path, data, perm)
   142  }