Skip to content

Commit

Permalink
feat(helm): update repo by default, added --no-update flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nicr9 committed Oct 14, 2016
1 parent 1a7373e commit 16436b5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
44 changes: 39 additions & 5 deletions cmd/helm/repo_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (
)

type repoAddCmd struct {
name string
url string
home helmpath.Home
out io.Writer
name string
url string
home helmpath.Home
out io.Writer
noupdate bool
}

func newRepoAddCmd(out io.Writer) *cobra.Command {
Expand All @@ -54,11 +55,19 @@ func newRepoAddCmd(out io.Writer) *cobra.Command {
return add.run()
},
}
f := cmd.Flags()
f.BoolVar(&add.noupdate, "no-update", false, "raise error if repo is already registered")
return cmd
}

func (a *repoAddCmd) run() error {
if err := addRepository(a.name, a.url, a.home); err != nil {
var err error
if a.noupdate {
err = addRepository(a.name, a.url, a.home)
} else {
err = updateRepository(a.name, a.url, a.home)
}
if err != nil {
return err
}
fmt.Fprintf(a.out, "%q has been added to your repositories\n", a.name)
Expand Down Expand Up @@ -91,3 +100,28 @@ func insertRepoLine(name, url string, home helmpath.Home) error {
})
return f.WriteFile(home.RepositoryFile(), 0644)
}

func updateRepository(name, url string, home helmpath.Home) error {
cif := home.CacheIndex(name)
if err := repo.DownloadIndexFile(name, url, cif); err != nil {
return err
}

return updateRepoLine(name, url, home)
}

func updateRepoLine(name, url string, home helmpath.Home) error {
cif := home.CacheIndex(name)
f, err := repo.LoadRepositoriesFile(home.RepositoryFile())
if err != nil {
return err
}

f.Update(&repo.Entry{
Name: name,
URL: url,
Cache: filepath.Base(cif),
})

return f.WriteFile(home.RepositoryFile(), 0666)
}
8 changes: 8 additions & 0 deletions cmd/helm/repo_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ func TestRepoAdd(t *testing.T) {
if !f.Has(testName) {
t.Errorf("%s was not successfully inserted into %s", testName, hh.RepositoryFile())
}

if err := updateRepository(testName, ts.URL(), hh); err != nil {
t.Errorf("Repository was not updated: %s", err)
}

if err := addRepository(testName, ts.URL(), hh); err == nil {
t.Errorf("Duplicate repository name was added")
}
}
14 changes: 14 additions & 0 deletions pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ func (r *RepoFile) Add(re ...*Entry) {
r.Repositories = append(r.Repositories, re...)
}

// Update attempts to replace one or more repo entries in a repo file. If an
// entry with the same name doesn't exist in the repo file it will add it.
func (r *RepoFile) Update(re ...*Entry) {
for _, target := range re {
for j, repo := range r.Repositories {
if repo.Name == target.Name {
r.Repositories[j] = target
break
}
}
r.Add(target)
}
}

// Has returns true if the given name is already a repository name.
func (r *RepoFile) Has(name string) bool {
for _, rf := range r.Repositories {
Expand Down

0 comments on commit 16436b5

Please sign in to comment.