Ports `—strict` flag for repo update command from v2

This ports the changes from #4348 and #5183 to helm v3

Signed-off-by: Jonathon Day <jonathon.day@stablekernel.com>
pull/7334/head
Jonathon Day 6 years ago
parent 8dddb18de7
commit 9201a6171c

@ -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") var errNoRepositories = errors.New("no repositories found. You must add one before updating")
type repoUpdateOptions struct { type repoUpdateOptions struct {
update func([]*repo.ChartRepository, io.Writer) update func([]*repo.ChartRepository, io.Writer, bool) error
repoFile string repoFile string
strict bool
} }
func newRepoUpdateCmd(out io.Writer) *cobra.Command { func newRepoUpdateCmd(out io.Writer) *cobra.Command {
@ -55,6 +56,10 @@ func newRepoUpdateCmd(out io.Writer) *cobra.Command {
return o.run(out) return o.run(out)
}, },
} }
f := cmd.Flags()
f.BoolVar(&o.strict, "strict", false, "fail on update warnings")
return cmd return cmd
} }
@ -72,24 +77,39 @@ func (o *repoUpdateOptions) run(out io.Writer) error {
repos = append(repos, r) repos = append(repos, r)
} }
o.update(repos, out) return o.update(repos, out, o.strict)
return nil
} }
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...") 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 { for _, re := range repos {
wg.Add(1) wg.Add(1)
go func(re *repo.ChartRepository) { go func(re *repo.ChartRepository) {
defer wg.Done() defer wg.Done()
if _, err := re.DownloadIndexFile(); err != nil { 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) 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 { } else {
mu.Lock()
fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name) fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name)
mu.Unlock()
} }
}(re) }(re)
} }
wg.Wait() wg.Wait()
if errorCounter != 0 && strict {
return errors.New("Update Failed. Check log for details")
}
fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ") fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ")
return nil
} }

@ -32,10 +32,12 @@ func TestUpdateCmd(t *testing.T) {
var out bytes.Buffer var out bytes.Buffer
// Instead of using the HTTP updater, we provide our own for this test. // Instead of using the HTTP updater, we provide our own for this test.
// The TestUpdateCharts test verifies the HTTP behavior independently. // 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 { for _, re := range repos {
fmt.Fprintln(out, re.Config.Name) fmt.Fprintln(out, re.Config.Name)
} }
return nil
} }
o := &repoUpdateOptions{ o := &repoUpdateOptions{
update: updater, update: updater,
@ -64,14 +66,16 @@ func TestUpdateCharts(t *testing.T) {
Name: "charts", Name: "charts",
URL: ts.URL(), URL: ts.URL(),
}, getter.All(settings)) }, getter.All(settings))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
updateCharts([]*repo.ChartRepository{r}, b) updateCharts([]*repo.ChartRepository{r}, b, false)
got := b.String() got := b.String()
if strings.Contains(got, "Unable to get an update") { if strings.Contains(got, "Unable to get an update") {
t.Errorf("Failed to get a repo: %q", got) t.Errorf("Failed to get a repo: %q", got)
} }
@ -79,3 +83,20 @@ func TestUpdateCharts(t *testing.T) {
t.Error("Update was not successful") 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)
}
}

Loading…
Cancel
Save