github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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/helm/helmpath"
    28  	"k8s.io/helm/pkg/repo"
    29  )
    30  
    31  const updateDesc = `
    32  Update gets the latest information about charts from the respective chart repositories.
    33  Information is cached locally, where it is used by commands like 'helm search'.
    34  
    35  'helm update' is the deprecated form of 'helm repo update'. It will be removed in
    36  future releases.
    37  `
    38  
    39  var (
    40  	errNoRepositories = errors.New("no repositories found. You must add one before updating")
    41  )
    42  
    43  type repoUpdateCmd struct {
    44  	update func([]*repo.ChartRepository, io.Writer, helmpath.Home)
    45  	home   helmpath.Home
    46  	out    io.Writer
    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 on available charts in the chart repositories",
    58  		Long:    updateDesc,
    59  		RunE: func(cmd *cobra.Command, args []string) error {
    60  			u.home = helmpath.Home(homePath())
    61  			return u.run()
    62  		},
    63  	}
    64  	return cmd
    65  }
    66  
    67  func (u *repoUpdateCmd) run() error {
    68  	f, err := repo.LoadRepositoriesFile(u.home.RepositoryFile())
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	if len(f.Repositories) == 0 {
    74  		return errNoRepositories
    75  	}
    76  	var repos []*repo.ChartRepository
    77  	for _, cfg := range f.Repositories {
    78  		r, err := repo.NewChartRepository(cfg)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		repos = append(repos, r)
    83  	}
    84  
    85  	u.update(repos, u.out, u.home)
    86  	return nil
    87  }
    88  
    89  func updateCharts(repos []*repo.ChartRepository, out io.Writer, home helmpath.Home) {
    90  	fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...")
    91  	var wg sync.WaitGroup
    92  	for _, re := range repos {
    93  		wg.Add(1)
    94  		go func(re *repo.ChartRepository) {
    95  			defer wg.Done()
    96  			if re.Config.Name == localRepository {
    97  				fmt.Fprintf(out, "...Skip %s chart repository\n", re.Config.Name)
    98  				return
    99  			}
   100  			err := re.DownloadIndexFile(home.Cache())
   101  			if err != nil {
   102  				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)
   103  			} else {
   104  				fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name)
   105  			}
   106  		}(re)
   107  	}
   108  	wg.Wait()
   109  	fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ")
   110  }