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

first cut of fetch

parent eba4c59a
......@@ -4,8 +4,11 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"github.com/kubernetes/helm/pkg/repo"
"github.com/spf13/cobra"
)
......@@ -14,23 +17,37 @@ func init() {
}
var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "Download a chart from a repository and unpack it in local directory.",
Use: "fetch [chart URL | repo/chartname]",
Short: "Download a chart from a repository and (optionally) unpack it in local directory.",
Long: "",
RunE: fetch,
}
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
// call download url
out, err := os.Create("nginx-2.0.0.tgz")
u, err := mapRepoArg(args[0], f.Repositories)
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 {
return err
}
defer out.Close()
resp, err := http.Get("http://localhost:8879/charts/nginx-2.0.0.tgz")
fmt.Println("after req")
resp, err := http.Get(u)
// unpack file
if err != nil {
return err
......@@ -44,3 +61,32 @@ func fetch(cmd *cobra.Command, args []string) error {
}
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