...

Source file src/helm.sh/helm/v3/pkg/repo/repo.go

Documentation: helm.sh/helm/v3/pkg/repo

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

View as plain text