github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/repo/repo.go (about)

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