Commit b39a0848 authored by vaikas-google's avatar vaikas-google

first cut of fetch

parent eba4c59a
...@@ -4,8 +4,11 @@ import ( ...@@ -4,8 +4,11 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/url"
"os" "os"
"strings"
"github.com/kubernetes/helm/pkg/repo"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -14,23 +17,37 @@ func init() { ...@@ -14,23 +17,37 @@ func init() {
} }
var fetchCmd = &cobra.Command{ var fetchCmd = &cobra.Command{
Use: "fetch", Use: "fetch [chart URL | repo/chartname]",
Short: "Download a chart from a repository and unpack it in local directory.", Short: "Download a chart from a repository and (optionally) unpack it in local directory.",
Long: "", Long: "",
RunE: fetch, RunE: fetch,
} }
func fetch(cmd *cobra.Command, args []string) error { func fetch(cmd *cobra.Command, args []string) error {
// parse args if len(args) == 0 {
return fmt.Errorf("This command needs at least one argument, url or repo/name of the chart.")
}
f, err := repo.LoadRepositoriesFile(repositoriesFile())
if err != nil {
return err
}
// get download url // get download url
// call download url u, err := mapRepoArg(args[0], f.Repositories)
out, err := os.Create("nginx-2.0.0.tgz") if err != nil {
return err
}
// Grab the package name that we'll use for the name of the file to download to.
p := strings.Split(u.String(), "/")
chartName := p[len(p)-1]
out, err := os.Create(chartName)
if err != nil { if err != nil {
return err return err
} }
defer out.Close() defer out.Close()
resp, err := http.Get("http://localhost:8879/charts/nginx-2.0.0.tgz") resp, err := http.Get(u)
fmt.Println("after req")
// unpack file // unpack file
if err != nil { if err != nil {
return err return err
...@@ -44,3 +61,32 @@ func fetch(cmd *cobra.Command, args []string) error { ...@@ -44,3 +61,32 @@ func fetch(cmd *cobra.Command, args []string) error {
} }
return nil return nil
} }
// mapRepoArg figures out which format the argument is given, and creates a fetchable
// url from it.
func mapRepoArg(arg string, r map[string]string) (*url.URL, error) {
// See if it's already a full URL.
u, err := url.ParseRequestURI(arg)
if err == nil {
// If it has a scheme and host and path, it's a full URL
if u.IsAbs() && len(u.Host) > 0 && len(u.Path) > 0 {
return u, nil
} else {
return nil, fmt.Errorf("Invalid chart url format: %s", arg)
}
}
// See if it's of the form: repo/path_to_chart
p := strings.Split(arg, "/")
if len(p) > 1 {
if baseUrl, ok := r[p[0]]; ok {
if !strings.HasSuffix(baseUrl, "/") {
baseUrl = baseUrl + "/"
}
return url.ParseRequestURI(baseUrl + strings.Join(p[1:], "/"))
} else {
return nil, fmt.Errorf("No such repo: %s", p[0])
}
} else {
return nil, fmt.Errorf("Invalid chart url format: %s", arg)
}
}
package main
import (
"fmt"
// "io"
// "net/http"
//"net/url"
// "os"
"testing"
)
type testCase struct {
in string
expectedErr error
expectedOut string
}
var repos = map[string]string{
"local": "http://localhost:8879/charts",
"someother": "http://storage.googleapis.com/mycharts",
}
var testCases = []testCase{
{"bad", fmt.Errorf("Invalid chart url format: bad"), ""},
{"http://", fmt.Errorf("Invalid chart url format: http://"), ""},
{"http://example.com", fmt.Errorf("Invalid chart url format: http://example.com"), ""},
{"http://example.com/foo/bar", nil, "http://example.com/foo/bar"},
{"local/nginx-2.0.0.tgz", nil, "http://localhost:8879/charts/nginx-2.0.0.tgz"},
{"nonexistentrepo/nginx-2.0.0.tgz", fmt.Errorf("No such repo: nonexistentrepo"), ""},
}
func testRunner(t *testing.T, tc testCase) {
u, err := mapRepoArg(tc.in, repos)
if (tc.expectedErr == nil && err != nil) ||
(tc.expectedErr != nil && err == nil) ||
(tc.expectedErr != nil && err != nil && tc.expectedErr.Error() != err.Error()) {
t.Errorf("Expected mapRepoArg to fail with input %s %v but got %v", tc.in, tc.expectedErr, err)
}
if (u == nil && len(tc.expectedOut) != 0) ||
(u != nil && len(tc.expectedOut) == 0) ||
(u != nil && tc.expectedOut != u.String()) {
t.Errorf("Expected %s to map to fetch url %v but got %v", tc.in, tc.expectedOut, u)
}
}
func TestMappings(t *testing.T) {
for _, tc := range testCases {
testRunner(t, tc)
}
}
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