github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/cmd/helm/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/repo"
    28  )
    29  
    30  const updateDesc = `
    31  Update gets the latest information about charts from the respective chart repositories.
    32  Information is cached locally, where it is used by commands like 'helm search'.
    33  `
    34  
    35  type updateCmd struct {
    36  	repoFile string
    37  	update   func(map[string]string, bool, io.Writer)
    38  	out      io.Writer
    39  }
    40  
    41  func newUpdateCmd(out io.Writer) *cobra.Command {
    42  	u := &updateCmd{
    43  		out:      out,
    44  		update:   updateCharts,
    45  		repoFile: repositoriesFile(),
    46  	}
    47  	cmd := &cobra.Command{
    48  		Use:     "update",
    49  		Aliases: []string{"up"},
    50  		Short:   "update information on available charts in the chart repositories",
    51  		Long:    updateDesc,
    52  		RunE: func(cmd *cobra.Command, args []string) error {
    53  			return u.run()
    54  		},
    55  	}
    56  	return cmd
    57  }
    58  
    59  func (u *updateCmd) run() error {
    60  	f, err := repo.LoadRepositoriesFile(u.repoFile)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if len(f.Repositories) == 0 {
    66  		return errors.New("no repositories found. You must add one before updating")
    67  	}
    68  
    69  	u.update(f.Repositories, flagDebug, u.out)
    70  	return nil
    71  }
    72  
    73  func updateCharts(repos map[string]string, verbose bool, out io.Writer) {
    74  	fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...")
    75  	var wg sync.WaitGroup
    76  	for name, url := range repos {
    77  		wg.Add(1)
    78  		go func(n, u string) {
    79  			defer wg.Done()
    80  			err := repo.DownloadIndexFile(n, u, cacheIndexFile(n))
    81  			if err != nil {
    82  				updateErr := fmt.Sprintf("...Unable to get an update from the %q chart repository", n)
    83  				if verbose {
    84  					updateErr = updateErr + ": " + err.Error()
    85  				}
    86  				fmt.Fprintln(out, updateErr)
    87  			} else {
    88  				fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", n)
    89  			}
    90  		}(name, url)
    91  	}
    92  	wg.Wait()
    93  	fmt.Fprintln(out, "Update Complete. Happy Helming!")
    94  }