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 package main
import ( import (
"fmt"
"os" "os"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -44,3 +46,14 @@ func init() { ...@@ -44,3 +46,14 @@ func init() {
func main() { func main() {
RootCommand.Execute() 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 ...@@ -3,7 +3,6 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/kubernetes/helm/pkg/client" "github.com/kubernetes/helm/pkg/client"
...@@ -17,7 +16,6 @@ Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.he ...@@ -17,7 +16,6 @@ Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.he
var ( var (
tillerImg string tillerImg string
defaultRepo = map[string]string{"default-name": "default-url"}
) )
func init() { func init() {
...@@ -83,7 +81,10 @@ func ensureHome() error { ...@@ -83,7 +81,10 @@ func ensureHome() error {
repoFile := repositoriesFile() repoFile := repositoriesFile()
if fi, err := os.Stat(repoFile); err != nil { if fi, err := os.Stat(repoFile); err != nil {
fmt.Printf("Creating %s \n", repoFile) 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 return err
} }
} else if fi.IsDir() { } else if fi.IsDir() {
......
...@@ -2,7 +2,7 @@ package main ...@@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "io/ioutil"
"github.com/gosuri/uitable" "github.com/gosuri/uitable"
"github.com/kubernetes/helm/pkg/repo" "github.com/kubernetes/helm/pkg/repo"
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
func init() { func init() {
repoCmd.AddCommand(repoAddCmd) repoCmd.AddCommand(repoAddCmd)
repoCmd.AddCommand(repoListCmd) repoCmd.AddCommand(repoListCmd)
repoCmd.AddCommand(repoRemoveCmd)
RootCommand.AddCommand(repoCmd) RootCommand.AddCommand(repoCmd)
} }
...@@ -33,9 +34,15 @@ var repoListCmd = &cobra.Command{ ...@@ -33,9 +34,15 @@ var repoListCmd = &cobra.Command{
RunE: runRepoList, 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 { func runRepoAdd(cmd *cobra.Command, args []string) error {
if len(args) != 2 { if err := checkArgsLength(2, len(args), "name for the chart repository", "the url of the chart repository"); err != nil {
return fmt.Errorf("This command needs two argument, a name for the chart repository and the url of the chart repository") return err
} }
err := insertRepoLine(args[0], args[1]) err := insertRepoLine(args[0], args[1])
...@@ -65,35 +72,60 @@ func runRepoList(cmd *cobra.Command, args []string) error { ...@@ -65,35 +72,60 @@ func runRepoList(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func insertRepoLine(name, url string) error { func runRepoRemove(cmd *cobra.Command, args []string) error {
err := checkUniqueName(name) if err := checkArgsLength(1, len(args), "name of chart repository"); err != nil {
if err != nil { return err
}
if err := removeRepoLine(args[0]); err != nil {
return err return err
} }
return nil
}
b, _ := yaml.Marshal(map[string]string{name: url}) func removeRepoLine(name string) error {
f, err := os.OpenFile(repositoriesFile(), os.O_APPEND|os.O_WRONLY, 0666) r, err := repo.LoadRepositoriesFile(repositoriesFile())
if err != nil { if err != nil {
return err 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 { if err != nil {
return err 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 return nil
} }
func checkUniqueName(name string) error { func insertRepoLine(name, url string) error {
file, err := repo.LoadRepositoriesFile(repositoriesFile()) f, err := repo.LoadRepositoriesFile(repositoriesFile())
if err != nil { if err != nil {
return err return err
} }
_, ok := f.Repositories[name]
_, ok := file.Repositories[name]
if ok { if ok {
return fmt.Errorf("The repository name you provided (%s) already exists. Please specifiy a different name.", name) 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 return nil
} }
...@@ -6,6 +6,11 @@ import ( ...@@ -6,6 +6,11 @@ import (
"github.com/kubernetes/helm/pkg/repo" "github.com/kubernetes/helm/pkg/repo"
) )
var (
testName = "test-name"
testURL = "test-url"
)
func TestRepoAdd(t *testing.T) { func TestRepoAdd(t *testing.T) {
home := createTmpHome() home := createTmpHome()
helmHome = home helmHome = home
...@@ -13,8 +18,6 @@ func TestRepoAdd(t *testing.T) { ...@@ -13,8 +18,6 @@ func TestRepoAdd(t *testing.T) {
t.Errorf("%s", err) t.Errorf("%s", err)
} }
testName := "test-name"
testURL := "test-url"
if err := insertRepoLine(testName, testURL); err != nil { if err := insertRepoLine(testName, testURL); err != nil {
t.Errorf("%s", err) t.Errorf("%s", err)
} }
...@@ -33,3 +36,32 @@ func TestRepoAdd(t *testing.T) { ...@@ -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