feat(helm): update repo by default, added `--no-update` flag

pull/1142/head
Nic Roland 8 years ago
parent 1a7373e584
commit 16436b565d

@ -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 {
@ -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)
@ -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)
}

@ -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")
}
}

@ -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 {

Loading…
Cancel
Save