From 459338264e67bd2794580dd0b51cc556c8c35890 Mon Sep 17 00:00:00 2001 From: Michelle Noorali Date: Mon, 2 May 2016 16:21:31 -0600 Subject: [PATCH 1/3] feat(helm): add helm update command This resolves issue #640. --- cmd/helm/update.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 cmd/helm/update.go diff --git a/cmd/helm/update.go b/cmd/helm/update.go new file mode 100644 index 000000000..d14054031 --- /dev/null +++ b/cmd/helm/update.go @@ -0,0 +1,84 @@ +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 +} From c7e571918d43e23c3073bf27c017f7bb50fb7dcc Mon Sep 17 00:00:00 2001 From: Michelle Noorali Date: Thu, 5 May 2016 15:58:00 -0600 Subject: [PATCH 2/3] feat(helm): download cache file when adding repo --- cmd/helm/repo.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/helm/repo.go b/cmd/helm/repo.go index fe093a385..86909c11e 100644 --- a/cmd/helm/repo.go +++ b/cmd/helm/repo.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io/ioutil" @@ -44,11 +45,16 @@ func runRepoAdd(cmd *cobra.Command, args []string) error { if err := checkArgsLength(2, len(args), "name for the chart repository", "the url of the chart repository"); err != nil { return err } + name, url := args[0], args[1] - err := insertRepoLine(args[0], args[1]) - if err != nil { + if err := downloadCacheFile(name, url); err != nil { + return errors.New("Oops! Looks like " + url + " is not a valid chart repository or cannot be reached\n") + } + + if err := insertRepoLine(name, url); err != nil { return err } + fmt.Println(args[0] + " has been added to your repositories") return nil } From bf4b15c01d05b84f0609ca616ceddddf1fdbc084 Mon Sep 17 00:00:00 2001 From: Michelle Noorali Date: Fri, 6 May 2016 14:58:32 -0600 Subject: [PATCH 3/3] feat(helm): validate cache file before updating --- cmd/helm/update.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cmd/helm/update.go b/cmd/helm/update.go index d14054031..4528d8f09 100644 --- a/cmd/helm/update.go +++ b/cmd/helm/update.go @@ -3,12 +3,14 @@ package main import ( "fmt" "io" + "io/ioutil" "net/http" "os" "strings" "sync" "github.com/spf13/cobra" + "gopkg.in/yaml.v2" "github.com/kubernetes/helm/pkg/repo" ) @@ -18,7 +20,7 @@ var verboseUpdate bool var updateCommand = &cobra.Command{ Use: "update", Short: "Update information on available charts in the chart repositories.", - RunE: update, + RunE: runUpdate, } func init() { @@ -26,7 +28,7 @@ func init() { RootCommand.AddCommand(updateCommand) } -func update(cmd *cobra.Command, args []string) error { +func runUpdate(cmd *cobra.Command, args []string) error { f, err := repo.LoadRepositoriesFile(repositoriesFile()) if err != nil { @@ -68,9 +70,20 @@ func downloadCacheFile(name, url string) error { if err != nil { return err } + defer resp.Body.Close() var cacheFile *os.File - defer resp.Body.Close() + var r repo.RepoFile + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + if err := yaml.Unmarshal(b, &r); err != nil { + return err + } + cacheFile, err = os.Create(cacheDirectory(name + "-cache.yaml")) if err != nil { return err