diff --git a/cmd/helm/repo.go b/cmd/helm/repo.go index df3205120..cc89a4825 100644 --- a/cmd/helm/repo.go +++ b/cmd/helm/repo.go @@ -31,7 +31,7 @@ import ( ) func init() { - repoCmd.AddCommand(repoAddCmd) + repoCmd.AddCommand(repoAddCmd()) repoCmd.AddCommand(repoListCmd) repoCmd.AddCommand(repoRemoveCmd) repoCmd.AddCommand(repoIndexCmd) @@ -43,10 +43,42 @@ var repoCmd = &cobra.Command{ Short: "add, list, or remove chart repositories", } -var repoAddCmd = &cobra.Command{ - Use: "add [flags] [NAME] [URL]", - Short: "add a chart repository", - RunE: runRepoAdd, +type addCmd struct { + update bool +} + +func repoAddCmd() *cobra.Command { + add := &addCmd{} + cmd := &cobra.Command{ + Use: "add [flags] [NAME] [URL]", + Short: "add a chart repository", + RunE: func(cmd *cobra.Command, args []string) error { + if err := checkArgsLength(2, len(args), "name for the chart repository", "the url of the chart repository"); err != nil { + return err + } + name, url := args[0], args[1] + + var err error + if add.update { + err = updateRepository(name, url) + } else { + err = addRepository(name, url) + } + if err != nil { + return err + } + + if add.update { + fmt.Println(name + " has been updated") + } else { + fmt.Println(name + " has been added to your repositories") + } + return nil + }, + } + f := cmd.Flags() + f.BoolVarP(&add.update, "update", "u", false, "update old url if it exists") + return cmd } var repoListCmd = &cobra.Command{ @@ -68,20 +100,6 @@ var repoIndexCmd = &cobra.Command{ RunE: runRepoIndex, } -func runRepoAdd(cmd *cobra.Command, args []string) error { - if err := checkArgsLength(2, len(args), "name for the chart repository", "the url of the chart repository"); err != nil { - return err - } - name, url := args[0], args[1] - - if err := addRepository(name, url); err != nil { - return err - } - - fmt.Println(name + " has been added to your repositories") - return nil -} - func runRepoList(cmd *cobra.Command, args []string) error { f, err := repo.LoadRepositoriesFile(repositoriesFile()) if err != nil { @@ -141,6 +159,14 @@ func addRepository(name, url string) error { return insertRepoLine(name, url) } +func updateRepository(name, url string) error { + if err := repo.DownloadIndexFile(name, url, cacheIndexFile(name)); err != nil { + return errors.New("Looks like " + url + " is not a valid chart repository or cannot be reached: " + err.Error()) + } + + return updateRepoLine(name, url) +} + func removeRepoLine(name string) error { r, err := repo.LoadRepositoriesFile(repositoriesFile()) if err != nil { @@ -197,3 +223,19 @@ func insertRepoLine(name, url string) error { b, _ := yaml.Marshal(&f.Repositories) return ioutil.WriteFile(repositoriesFile(), b, 0666) } + +func updateRepoLine(name, url string) error { + f, err := repo.LoadRepositoriesFile(repositoriesFile()) + if err != nil { + return err + } + + if f.Repositories == nil { + f.Repositories = make(map[string]string) + } + + f.Repositories[name] = url + + b, _ := yaml.Marshal(&f.Repositories) + return ioutil.WriteFile(repositoriesFile(), b, 0666) +} diff --git a/cmd/helm/repo_test.go b/cmd/helm/repo_test.go index d24f6f594..124914a3a 100644 --- a/cmd/helm/repo_test.go +++ b/cmd/helm/repo_test.go @@ -65,6 +65,10 @@ func TestRepoAdd(t *testing.T) { t.Errorf("Duplicate repository name was added") } + if err := updateRepository(testName, testURL); err == nil { + t.Errorf("Repository was not updated: %s", err) + } + } func TestRepoRemove(t *testing.T) {