From 9201a6171cadaf3e15f4b81478d94fb27ff2a848 Mon Sep 17 00:00:00 2001 From: Jonathon Day Date: Fri, 3 Jan 2020 16:27:24 -0500 Subject: [PATCH] =?UTF-8?q?Ports=20`=E2=80=94strict`=20flag=20for=20repo?= =?UTF-8?q?=20update=20command=20from=20v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This ports the changes from #4348 and #5183 to helm v3 Signed-off-by: Jonathon Day --- cmd/helm/repo_update.go | 30 +++++++++++++++++++++++++----- cmd/helm/repo_update_test.go | 25 +++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/cmd/helm/repo_update.go b/cmd/helm/repo_update.go index 027f18518..0aa794391 100644 --- a/cmd/helm/repo_update.go +++ b/cmd/helm/repo_update.go @@ -37,8 +37,9 @@ Information is cached locally, where it is used by commands like 'helm search'. var errNoRepositories = errors.New("no repositories found. You must add one before updating") type repoUpdateOptions struct { - update func([]*repo.ChartRepository, io.Writer) + update func([]*repo.ChartRepository, io.Writer, bool) error repoFile string + strict bool } func newRepoUpdateCmd(out io.Writer) *cobra.Command { @@ -55,6 +56,10 @@ func newRepoUpdateCmd(out io.Writer) *cobra.Command { return o.run(out) }, } + + f := cmd.Flags() + f.BoolVar(&o.strict, "strict", false, "fail on update warnings") + return cmd } @@ -72,24 +77,39 @@ func (o *repoUpdateOptions) run(out io.Writer) error { repos = append(repos, r) } - o.update(repos, out) - return nil + return o.update(repos, out, o.strict) } -func updateCharts(repos []*repo.ChartRepository, out io.Writer) { +func updateCharts(repos []*repo.ChartRepository, out io.Writer, strict bool) error { fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...") - var wg sync.WaitGroup + var ( + errorCounter int + wg sync.WaitGroup + mu sync.Mutex + ) + for _, re := range repos { wg.Add(1) go func(re *repo.ChartRepository) { defer wg.Done() if _, err := re.DownloadIndexFile(); err != nil { + mu.Lock() + errorCounter++ 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) + mu.Unlock() } else { + mu.Lock() fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name) + mu.Unlock() } }(re) } wg.Wait() + + if errorCounter != 0 && strict { + return errors.New("Update Failed. Check log for details") + } + fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ") + return nil } diff --git a/cmd/helm/repo_update_test.go b/cmd/helm/repo_update_test.go index 6ddce0637..524482f14 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -32,10 +32,12 @@ func TestUpdateCmd(t *testing.T) { var out bytes.Buffer // Instead of using the HTTP updater, we provide our own for this test. // The TestUpdateCharts test verifies the HTTP behavior independently. - updater := func(repos []*repo.ChartRepository, out io.Writer) { + + updater := func(repos []*repo.ChartRepository, out io.Writer, strict bool) error { for _, re := range repos { fmt.Fprintln(out, re.Config.Name) } + return nil } o := &repoUpdateOptions{ update: updater, @@ -64,14 +66,16 @@ func TestUpdateCharts(t *testing.T) { Name: "charts", URL: ts.URL(), }, getter.All(settings)) + if err != nil { t.Error(err) } b := bytes.NewBuffer(nil) - updateCharts([]*repo.ChartRepository{r}, b) + updateCharts([]*repo.ChartRepository{r}, b, false) got := b.String() + if strings.Contains(got, "Unable to get an update") { t.Errorf("Failed to get a repo: %q", got) } @@ -79,3 +83,20 @@ func TestUpdateCharts(t *testing.T) { t.Error("Update was not successful") } } + +func TestUpdateCmdStrictFlag(t *testing.T) { + defer resetEnv()() + defer ensure.HelmHome(t)() + + out := bytes.NewBuffer(nil) + cmd := newRepoUpdateCmd(out) + cmd.ParseFlags([]string{"--strict"}) + + if err := cmd.RunE(cmd, []string{}); err == nil { + t.Fatal("expected error due to strict flag") + } + + if got := out.String(); !strings.Contains(got, "Unable to get an update") { + t.Errorf("Expected 'Unable to get an update', got %q", got) + } +}