github.com/defensepoint-snyk-test/helm-new@v0.0.0-20211130153739-c57ea64d6603/cmd/helm/repo_update.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 main
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  	"sync"
    24  
    25  	"github.com/spf13/cobra"
    26  
    27  	"k8s.io/helm/pkg/getter"
    28  	"k8s.io/helm/pkg/helm/helmpath"
    29  	"k8s.io/helm/pkg/repo"
    30  )
    31  
    32  const updateDesc = `
    33  Update gets the latest information about charts from the respective chart repositories.
    34  Information is cached locally, where it is used by commands like 'helm search'.
    35  
    36  'helm update' is the deprecated form of 'helm repo update'. It will be removed in
    37  future releases.
    38  `
    39  
    40  var errNoRepositories = errors.New("no repositories found. You must add one before updating")
    41  
    42  type repoUpdateCmd struct {
    43  	update func([]*repo.ChartRepository, io.Writer, helmpath.Home, bool) error
    44  	home   helmpath.Home
    45  	out    io.Writer
    46  	strict bool
    47  }
    48  
    49  func newRepoUpdateCmd(out io.Writer) *cobra.Command {
    50  	u := &repoUpdateCmd{
    51  		out:    out,
    52  		update: updateCharts,
    53  	}
    54  	cmd := &cobra.Command{
    55  		Use:     "update",
    56  		Aliases: []string{"up"},
    57  		Short:   "update information of available charts locally from chart repositories",
    58  		Long:    updateDesc,
    59  		RunE: func(cmd *cobra.Command, args []string) error {
    60  			u.home = settings.Home
    61  			return u.run()
    62  		},
    63  	}
    64  
    65  	f := cmd.Flags()
    66  	f.BoolVar(&u.strict, "strict", false, "fail on update warnings")
    67  
    68  	return cmd
    69  }
    70  
    71  func (u *repoUpdateCmd) run() error {
    72  	f, err := repo.LoadRepositoriesFile(u.home.RepositoryFile())
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	if len(f.Repositories) == 0 {
    78  		return errNoRepositories
    79  	}
    80  	var repos []*repo.ChartRepository
    81  	for _, cfg := range f.Repositories {
    82  		r, err := repo.NewChartRepository(cfg, getter.All(settings))
    83  		if err != nil {
    84  			return err
    85  		}
    86  		repos = append(repos, r)
    87  	}
    88  	return u.update(repos, u.out, u.home, u.strict)
    89  }
    90  
    91  func updateCharts(repos []*repo.ChartRepository, out io.Writer, home helmpath.Home, strict bool) error {
    92  	fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...")
    93  	var (
    94  		errorCounter int
    95  		wg           sync.WaitGroup
    96  	)
    97  	for _, re := range repos {
    98  		wg.Add(1)
    99  		go func(re *repo.ChartRepository) {
   100  			defer wg.Done()
   101  			if re.Config.Name == localRepository {
   102  				fmt.Fprintf(out, "...Skip %s chart repository\n", re.Config.Name)
   103  				return
   104  			}
   105  			err := re.DownloadIndexFile(home.Cache())
   106  			if err != nil {
   107  				errorCounter++
   108  				fmt.Fprintf(out, "...Unable to get an update from the %q chart repository (%s):\n\t%s\n", re.Config.Name, re.Config.URL, err)
   109  			} else {
   110  				fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name)
   111  			}
   112  		}(re)
   113  	}
   114  	wg.Wait()
   115  
   116  	if errorCounter != 0 && strict {
   117  		return errors.New("Update Failed. Check log for details")
   118  	}
   119  
   120  	fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ")
   121  	return nil
   122  }