github.com/sri09kanth/helm@v3.0.0-beta.3+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  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"time"
    24  
    25  	"github.com/pkg/errors"
    26  	"sigs.k8s.io/yaml"
    27  )
    28  
    29  // File represents the repositories.yaml file
    30  type File struct {
    31  	APIVersion   string    `json:"apiVersion"`
    32  	Generated    time.Time `json:"generated"`
    33  	Repositories []*Entry  `json:"repositories"`
    34  }
    35  
    36  // NewFile generates an empty repositories file.
    37  //
    38  // Generated and APIVersion are automatically set.
    39  func NewFile() *File {
    40  	return &File{
    41  		APIVersion:   APIVersionV1,
    42  		Generated:    time.Now(),
    43  		Repositories: []*Entry{},
    44  	}
    45  }
    46  
    47  // LoadFile takes a file at the given path and returns a File object
    48  func LoadFile(path string) (*File, error) {
    49  	r := new(File)
    50  	b, err := ioutil.ReadFile(path)
    51  	if err != nil {
    52  		return r, errors.Wrapf(err, "couldn't load repositories file (%s)", path)
    53  	}
    54  
    55  	err = yaml.Unmarshal(b, r)
    56  	return r, err
    57  }
    58  
    59  // Add adds one or more repo entries to a repo file.
    60  func (r *File) Add(re ...*Entry) {
    61  	r.Repositories = append(r.Repositories, re...)
    62  }
    63  
    64  // Update attempts to replace one or more repo entries in a repo file. If an
    65  // entry with the same name doesn't exist in the repo file it will add it.
    66  func (r *File) Update(re ...*Entry) {
    67  	for _, target := range re {
    68  		r.update(target)
    69  	}
    70  }
    71  
    72  func (r *File) update(e *Entry) {
    73  	for j, repo := range r.Repositories {
    74  		if repo.Name == e.Name {
    75  			r.Repositories[j] = e
    76  			return
    77  		}
    78  	}
    79  	r.Add(e)
    80  }
    81  
    82  // Has returns true if the given name is already a repository name.
    83  func (r *File) Has(name string) bool {
    84  	for _, rf := range r.Repositories {
    85  		if rf.Name == name {
    86  			return true
    87  		}
    88  	}
    89  	return false
    90  }
    91  
    92  // Remove removes the entry from the list of repositories.
    93  func (r *File) Remove(name string) bool {
    94  	cp := []*Entry{}
    95  	found := false
    96  	for _, rf := range r.Repositories {
    97  		if rf.Name == name {
    98  			found = true
    99  			continue
   100  		}
   101  		cp = append(cp, rf)
   102  	}
   103  	r.Repositories = cp
   104  	return found
   105  }
   106  
   107  // WriteFile writes a repositories file to the given path.
   108  func (r *File) WriteFile(path string, perm os.FileMode) error {
   109  	data, err := yaml.Marshal(r)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
   114  		return err
   115  	}
   116  	return ioutil.WriteFile(path, data, perm)
   117  }