github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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 "github.com/stefanmcshane/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  	entry := r.Get(name)
    85  	return entry != nil
    86  }
    87  
    88  // Get returns an entry with the given name if it exists, otherwise returns nil
    89  func (r *File) Get(name string) *Entry {
    90  	for _, entry := range r.Repositories {
    91  		if entry.Name == name {
    92  			return entry
    93  		}
    94  	}
    95  	return nil
    96  }
    97  
    98  // Remove removes the entry from the list of repositories.
    99  func (r *File) Remove(name string) bool {
   100  	cp := []*Entry{}
   101  	found := false
   102  	for _, rf := range r.Repositories {
   103  		if rf.Name == name {
   104  			found = true
   105  			continue
   106  		}
   107  		cp = append(cp, rf)
   108  	}
   109  	r.Repositories = cp
   110  	return found
   111  }
   112  
   113  // WriteFile writes a repositories file to the given path.
   114  func (r *File) WriteFile(path string, perm os.FileMode) error {
   115  	data, err := yaml.Marshal(r)
   116  	if err != nil {
   117  		return err
   118  	}
   119  	if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
   120  		return err
   121  	}
   122  	return ioutil.WriteFile(path, data, perm)
   123  }