github.com/valdemarpavesi/helm@v2.9.1+incompatible/cmd/helm/repo_update.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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)
    44  	home   helmpath.Home
    45  	out    io.Writer
    46  }
    47  
    48  func newRepoUpdateCmd(out io.Writer) *cobra.Command {
    49  	u := &repoUpdateCmd{
    50  		out:    out,
    51  		update: updateCharts,
    52  	}
    53  	cmd := &cobra.Command{
    54  		Use:     "update",
    55  		Aliases: []string{"up"},
    56  		Short:   "update information of available charts locally from chart repositories",
    57  		Long:    updateDesc,
    58  		RunE: func(cmd *cobra.Command, args []string) error {
    59  			u.home = settings.Home
    60  			return u.run()
    61  		},
    62  	}
    63  	return cmd
    64  }
    65  
    66  func (u *repoUpdateCmd) run() error {
    67  	f, err := repo.LoadRepositoriesFile(u.home.RepositoryFile())
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	if len(f.Repositories) == 0 {
    73  		return errNoRepositories
    74  	}
    75  	var repos []*repo.ChartRepository
    76  	for _, cfg := range f.Repositories {
    77  		r, err := repo.NewChartRepository(cfg, getter.All(settings))
    78  		if err != nil {
    79  			return err
    80  		}
    81  		repos = append(repos, r)
    82  	}
    83  
    84  	u.update(repos, u.out, u.home)
    85  	return nil
    86  }
    87  
    88  func updateCharts(repos []*repo.ChartRepository, out io.Writer, home helmpath.Home) {
    89  	fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...")
    90  	var wg sync.WaitGroup
    91  	for _, re := range repos {
    92  		wg.Add(1)
    93  		go func(re *repo.ChartRepository) {
    94  			defer wg.Done()
    95  			if re.Config.Name == localRepository {
    96  				fmt.Fprintf(out, "...Skip %s chart repository\n", re.Config.Name)
    97  				return
    98  			}
    99  			err := re.DownloadIndexFile(home.Cache())
   100  			if err != nil {
   101  				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)
   102  			} else {
   103  				fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name)
   104  			}
   105  		}(re)
   106  	}
   107  	wg.Wait()
   108  	fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ")
   109  }