<commit_before><commit_msg>feat(helm): add helm update command<commit_after>package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
	"sync"

	"github.com/spf13/cobra"

	"github.com/kubernetes/helm/pkg/repo"
)

var verboseUpdate bool

var updateCommand = &cobra.Command{
	Use:   "update",
	Short: "Update information on available charts in the chart repositories.",
	RunE:  update,
}

func init() {
	updateCommand.Flags().BoolVar(&verboseUpdate, "verbose", false, "verbose error messages")
	RootCommand.AddCommand(updateCommand)
}

func update(cmd *cobra.Command, args []string) error {

	f, err := repo.LoadRepositoriesFile(repositoriesFile())
	if err != nil {
		return err
	}

	updateCharts(f.Repositories, verboseUpdate)
	return nil
}

func updateCharts(repos map[string]string, verbose bool) {
	fmt.Println("Hang tight while we grab the latest from your chart repositories...")
	var wg sync.WaitGroup
	for name, url := range repos {
		wg.Add(1)
		go func(n, u string) {
			defer wg.Done()
			err := downloadCacheFile(n, u)
			if err != nil {
				updateErr := "...Unable to get an update from the " + n + " chart repository"
				if verbose {
					updateErr = updateErr + ": " + err.Error()
				}
				fmt.Println(updateErr)
			} else {
				fmt.Println("...Successfully got an update from the " + n + " chart repository")
			}
		}(name, url)
	}
	wg.Wait()
	fmt.Println("Update Complete. Happy Helming!")
}

func downloadCacheFile(name, url string) error {
	var cacheURL string

	cacheURL = strings.TrimSuffix(url, "/") + "/cache.yaml"
	resp, err := http.Get(cacheURL)
	if err != nil {
		return err
	}

	var cacheFile *os.File
	defer resp.Body.Close()
	cacheFile, err = os.Create(cacheDirectory(name + "-cache.yaml"))
	if err != nil {
		return err
	}

	if _, err := io.Copy(cacheFile, resp.Body); err != nil {
		return err
	}

	return nil
}
{'fraction_non_alphanumeric': 0.13744588744588745, 'fraction_numerical': 0.0016233766233766235, 'mean_word_length': 3.704834605597964, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2835766', 'n_tokens_mistral': 698, 'n_tokens_neox': 634, 'n_words': 176}