From 223c89e6aac873a8fe9b0228d39364f566881831 Mon Sep 17 00:00:00 2001 From: Arash Deshmeh Date: Sat, 14 Apr 2018 15:37:42 -0400 Subject: [PATCH] feat(helm): support removing multiple repositories with repo remove command Signed-off-by: Arash Deshmeh --- cmd/helm/repo_remove.go | 15 ++++++--- cmd/helm/repo_remove_test.go | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/cmd/helm/repo_remove.go b/cmd/helm/repo_remove.go index 201ee9ca8..728852fa1 100644 --- a/cmd/helm/repo_remove.go +++ b/cmd/helm/repo_remove.go @@ -41,13 +41,18 @@ func newRepoRemoveCmd(out io.Writer) *cobra.Command { Aliases: []string{"rm"}, Short: "remove a chart repository", RunE: func(cmd *cobra.Command, args []string) error { - if err := checkArgsLength(len(args), "name of chart repository"); err != nil { - return err + if len(args) == 0 { + return fmt.Errorf("need at least one argument, name of chart repository") } - remove.name = args[0] - remove.home = settings.Home - return remove.run() + remove.home = settings.Home + for i := 0; i < len(args); i++ { + remove.name = args[i] + if err := remove.run(); err != nil { + return err + } + } + return nil }, } diff --git a/cmd/helm/repo_remove_test.go b/cmd/helm/repo_remove_test.go index 174a44495..bc071b989 100644 --- a/cmd/helm/repo_remove_test.go +++ b/cmd/helm/repo_remove_test.go @@ -18,6 +18,7 @@ package main import ( "bytes" + "io/ioutil" "os" "strings" "testing" @@ -79,3 +80,66 @@ func TestRepoRemove(t *testing.T) { t.Errorf("%s was not successfully removed from repositories list", testName) } } + +func TestRepoRemove_NoArguments(t *testing.T) { + cmd := newRepoRemoveCmd(ioutil.Discard) + if err := cmd.RunE(cmd, []string{}); err == nil { + t.Errorf("Expected an error since no repo names were provided") + } +} + +func TestRepoRemove_MultipleRepos(t *testing.T) { + ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*") + if err != nil { + t.Fatal(err) + } + + hh := helmpath.Home(thome) + cleanup := resetEnv() + defer func() { + ts.Stop() + os.RemoveAll(thome.String()) + cleanup() + }() + if err := ensureTestHome(hh, t); err != nil { + t.Fatal(err) + } + + settings.Home = thome + + repoFoo := testName + "foo" + repoBar := testName + "bar" + + if err := addRepository(repoFoo, ts.URL(), "", "", hh, "", "", "", true); err != nil { + t.Error(err) + } + if err := addRepository(repoBar, ts.URL(), "", "", hh, "", "", "", true); err != nil { + t.Error(err) + } + + b := bytes.NewBuffer(nil) + + cmd := newRepoRemoveCmd(b) + if err := cmd.RunE(cmd, []string{repoFoo, repoBar}); err != nil { + t.Error(err) + } + + if !strings.Contains(b.String(), repoFoo) { + t.Errorf("Expected %q in output, found: %q", repoFoo, b.String()) + } + if !strings.Contains(b.String(), repoBar) { + t.Errorf("Expected %q in output, found: %q", repoBar, b.String()) + } + + f, err := repo.LoadRepositoriesFile(hh.RepositoryFile()) + if err != nil { + t.Error(err) + } + + if f.Has(repoFoo) { + t.Errorf("%s was not successfully removed from repositories list", repoFoo) + } + if f.Has(repoBar) { + t.Errorf("%s was not successfully removed from repositories list", repoBar) + } +}