Commit 45932547 authored by Michelle Noorali's avatar Michelle Noorali

Merge pull request #631 from michelleN/helm-repo

feat(helm): add helm repo remove cmd
parents 2c71fb9f 6dd87645
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
)
......@@ -44,3 +46,14 @@ func init() {
func main() {
RootCommand.Execute()
}
func checkArgsLength(expectedNum, actualNum int, requiredArgs ...string) error {
if actualNum != expectedNum {
arg := "arguments"
if expectedNum == 1 {
arg = "argument"
}
return fmt.Errorf("This command needs %v %s: %s", expectedNum, arg, strings.Join(requiredArgs, ", "))
}
return nil
}
......@@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/kubernetes/helm/pkg/client"
......@@ -17,7 +16,6 @@ Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.he
var (
tillerImg string
defaultRepo = map[string]string{"default-name": "default-url"}
)
func init() {
......@@ -83,7 +81,10 @@ func ensureHome() error {
repoFile := repositoriesFile()
if fi, err := os.Stat(repoFile); err != nil {
fmt.Printf("Creating %s \n", repoFile)
if err := ioutil.WriteFile(repoFile, []byte("local: localhost:8879/charts\n"), 0644); err != nil {
if _, err := os.Create(repoFile); err != nil {
return err
}
if err := insertRepoLine("local", "localhost:8879/charts"); err != nil {
return err
}
} else if fi.IsDir() {
......
......@@ -2,7 +2,7 @@ package main
import (
"fmt"
"os"
"io/ioutil"
"github.com/gosuri/uitable"
"github.com/kubernetes/helm/pkg/repo"
......@@ -13,6 +13,7 @@ import (
func init() {
repoCmd.AddCommand(repoAddCmd)
repoCmd.AddCommand(repoListCmd)
repoCmd.AddCommand(repoRemoveCmd)
RootCommand.AddCommand(repoCmd)
}
......@@ -33,9 +34,15 @@ var repoListCmd = &cobra.Command{
RunE: runRepoList,
}
var repoRemoveCmd = &cobra.Command{
Use: "remove [flags] [NAME]",
Short: "remove a chart repository",
RunE: runRepoRemove,
}
func runRepoAdd(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("This command needs two argument, a name for the chart repository and the url of the chart repository")
if err := checkArgsLength(2, len(args), "name for the chart repository", "the url of the chart repository"); err != nil {
return err
}
err := insertRepoLine(args[0], args[1])
......@@ -65,35 +72,60 @@ func runRepoList(cmd *cobra.Command, args []string) error {
return nil
}
func insertRepoLine(name, url string) error {
err := checkUniqueName(name)
if err != nil {
func runRepoRemove(cmd *cobra.Command, args []string) error {
if err := checkArgsLength(1, len(args), "name of chart repository"); err != nil {
return err
}
if err := removeRepoLine(args[0]); err != nil {
return err
}
return nil
}
b, _ := yaml.Marshal(map[string]string{name: url})
f, err := os.OpenFile(repositoriesFile(), os.O_APPEND|os.O_WRONLY, 0666)
func removeRepoLine(name string) error {
r, err := repo.LoadRepositoriesFile(repositoriesFile())
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(b)
_, ok := r.Repositories[name]
if ok {
delete(r.Repositories, name)
b, err := yaml.Marshal(&r.Repositories)
if err != nil {
return err
}
if err := ioutil.WriteFile(repositoriesFile(), b, 0666); err != nil {
return err
}
} else {
return fmt.Errorf("The repository, %s, does not exist in your repositories list", name)
}
return nil
}
func checkUniqueName(name string) error {
file, err := repo.LoadRepositoriesFile(repositoriesFile())
func insertRepoLine(name, url string) error {
f, err := repo.LoadRepositoriesFile(repositoriesFile())
if err != nil {
return err
}
_, ok := file.Repositories[name]
_, ok := f.Repositories[name]
if ok {
return fmt.Errorf("The repository name you provided (%s) already exists. Please specifiy a different name.", name)
}
if f.Repositories == nil {
f.Repositories = make(map[string]string)
}
f.Repositories[name] = url
b, _ := yaml.Marshal(&f.Repositories)
if err := ioutil.WriteFile(repositoriesFile(), b, 0666); err != nil {
return err
}
return nil
}
......@@ -6,6 +6,11 @@ import (
"github.com/kubernetes/helm/pkg/repo"
)
var (
testName = "test-name"
testURL = "test-url"
)
func TestRepoAdd(t *testing.T) {
home := createTmpHome()
helmHome = home
......@@ -13,8 +18,6 @@ func TestRepoAdd(t *testing.T) {
t.Errorf("%s", err)
}
testName := "test-name"
testURL := "test-url"
if err := insertRepoLine(testName, testURL); err != nil {
t.Errorf("%s", err)
}
......@@ -33,3 +36,32 @@ func TestRepoAdd(t *testing.T) {
}
}
func TestRepoRemove(t *testing.T) {
home := createTmpHome()
helmHome = home
if err := ensureHome(); err != nil {
t.Errorf("%s", err)
}
if err := removeRepoLine(testName); err == nil {
t.Errorf("Expected error removing %s, but did not get one.", testName)
}
if err := insertRepoLine(testName, testURL); err != nil {
t.Errorf("%s", err)
}
if err := removeRepoLine(testName); err != nil {
t.Errorf("Error removing %s from repositories", testName)
}
f, err := repo.LoadRepositoriesFile(repositoriesFile())
if err != nil {
t.Errorf("%s", err)
}
if _, ok := f.Repositories[testName]; ok {
t.Errorf("%s was not successfully removed from repositories list", testName)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment