github.com/diafour/helm@v3.0.0-beta.3+incompatible/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  	"fmt"
    21  	"io"
    22  	"sync"
    23  
    24  	"github.com/pkg/errors"
    25  	"github.com/spf13/cobra"
    26  
    27  	"helm.sh/helm/cmd/helm/require"
    28  	"helm.sh/helm/pkg/getter"
    29  	"helm.sh/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  
    37  var errNoRepositories = errors.New("no repositories found. You must add one before updating")
    38  
    39  type repoUpdateOptions struct {
    40  	update   func([]*repo.ChartRepository, io.Writer)
    41  	repoFile string
    42  }
    43  
    44  func newRepoUpdateCmd(out io.Writer) *cobra.Command {
    45  	o := &repoUpdateOptions{update: updateCharts}
    46  
    47  	cmd := &cobra.Command{
    48  		Use:     "update",
    49  		Aliases: []string{"up"},
    50  		Short:   "update information of available charts locally from chart repositories",
    51  		Long:    updateDesc,
    52  		Args:    require.NoArgs,
    53  		RunE: func(cmd *cobra.Command, args []string) error {
    54  			o.repoFile = settings.RepositoryConfig
    55  			return o.run(out)
    56  		},
    57  	}
    58  	return cmd
    59  }
    60  
    61  func (o *repoUpdateOptions) run(out io.Writer) error {
    62  	f, err := repo.LoadFile(o.repoFile)
    63  	if isNotExist(err) || len(f.Repositories) == 0 {
    64  		return errNoRepositories
    65  	}
    66  	var repos []*repo.ChartRepository
    67  	for _, cfg := range f.Repositories {
    68  		r, err := repo.NewChartRepository(cfg, getter.All(settings))
    69  		if err != nil {
    70  			return err
    71  		}
    72  		repos = append(repos, r)
    73  	}
    74  
    75  	o.update(repos, out)
    76  	return nil
    77  }
    78  
    79  func updateCharts(repos []*repo.ChartRepository, out io.Writer) {
    80  	fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...")
    81  	var wg sync.WaitGroup
    82  	for _, re := range repos {
    83  		wg.Add(1)
    84  		go func(re *repo.ChartRepository) {
    85  			defer wg.Done()
    86  			if _, err := re.DownloadIndexFile(); err != nil {
    87  				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)
    88  			} else {
    89  				fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name)
    90  			}
    91  		}(re)
    92  	}
    93  	wg.Wait()
    94  	fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ")
    95  }