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

Merge pull request #655 from vaikas-google/master

Add ability to untar charts after downloading them
parents 9a0c4a69 92039222
...@@ -13,8 +13,13 @@ import ( ...@@ -13,8 +13,13 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var untarFile bool
var untarDir string
func init() { func init() {
RootCommand.AddCommand(fetchCmd) RootCommand.AddCommand(fetchCmd)
fetchCmd.Flags().BoolVar(&untarFile, "untar", false, "If set to true, will untar the chart after downloading it.")
fetchCmd.Flags().StringVar(&untarDir, "untardir", ".", "If untar is specified, this flag specifies where to untar the chart.")
} }
var fetchCmd = &cobra.Command{ var fetchCmd = &cobra.Command{
...@@ -49,10 +54,8 @@ func fetch(cmd *cobra.Command, args []string) error { ...@@ -49,10 +54,8 @@ func fetch(cmd *cobra.Command, args []string) error {
} }
defer resp.Body.Close() defer resp.Body.Close()
// TODO(vaikas): Implement untar / flag if untarFile {
untar := false return chart.Expand(untarDir, resp.Body)
if untar {
return untarChart(resp.Body)
} }
p := strings.Split(u.String(), "/") p := strings.Split(u.String(), "/")
return saveChartFile(p[len(p)-1], resp.Body) return saveChartFile(p[len(p)-1], resp.Body)
...@@ -84,18 +87,6 @@ func mapRepoArg(arg string, r map[string]string) (*url.URL, error) { ...@@ -84,18 +87,6 @@ func mapRepoArg(arg string, r map[string]string) (*url.URL, error) {
return nil, fmt.Errorf("Invalid chart url format: %s", arg) return nil, fmt.Errorf("Invalid chart url format: %s", arg)
} }
func untarChart(r io.Reader) error {
c, err := chart.LoadDataFromReader(r)
if err != nil {
return err
}
if c == nil {
return fmt.Errorf("Failed to untar the chart")
}
return fmt.Errorf("Not implemented yee")
}
func saveChartFile(c string, r io.Reader) error { func saveChartFile(c string, r io.Reader) error {
// Grab the chart name that we'll use for the name of the file to download to. // Grab the chart name that we'll use for the name of the file to download to.
out, err := os.Create(c) out, err := os.Create(c)
......
...@@ -88,7 +88,7 @@ func (c *Chart) TemplatesDir() string { ...@@ -88,7 +88,7 @@ func (c *Chart) TemplatesDir() string {
return filepath.Join(c.loader.dir(), preTemplates) return filepath.Join(c.loader.dir(), preTemplates)
} }
// ChartsDir returns teh directory where dependency charts are stored. // ChartsDir returns the directory where dependency charts are stored.
func (c *Chart) ChartsDir() string { func (c *Chart) ChartsDir() string {
return filepath.Join(c.loader.dir(), preCharts) return filepath.Join(c.loader.dir(), preCharts)
} }
...@@ -214,6 +214,44 @@ func Create(chartfile *Chartfile, dir string) (*Chart, error) { ...@@ -214,6 +214,44 @@ func Create(chartfile *Chartfile, dir string) (*Chart, error) {
}, nil }, nil
} }
// Expand uncompresses and extracts a chart into the specified directory.
func Expand(dir string, r io.Reader) error {
gr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gr.Close()
tr := tar.NewReader(gr)
for {
header, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
path := filepath.Clean(filepath.Join(dir, header.Name))
info := header.FileInfo()
if info.IsDir() {
if err = os.MkdirAll(path, info.Mode()); err != nil {
return err
}
continue
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, tr)
if err != nil {
return err
}
}
return nil
}
// fname prepares names for the filesystem // fname prepares names for the filesystem
func fname(name string) string { func fname(name string) string {
// Right now, we don't do anything. Do we need to encode any particular // Right now, we don't do anything. Do we need to encode any particular
......
...@@ -26,10 +26,11 @@ import ( ...@@ -26,10 +26,11 @@ import (
) )
const ( const (
testfile = "testdata/frobnitz/Chart.yaml" testfile = "testdata/frobnitz/Chart.yaml"
testdir = "testdata/frobnitz/" testdir = "testdata/frobnitz/"
testarchive = "testdata/frobnitz-0.0.1.tgz" testarchive = "testdata/frobnitz-0.0.1.tgz"
testmember = "templates/template.tpl" testmember = "templates/template.tpl"
expectedTemplate = "Hello {{.Name | default \"world\"}}\n"
) )
// Type canaries. If these fail, they will fail at compile time. // Type canaries. If these fail, they will fail at compile time.
...@@ -270,3 +271,42 @@ func compareContent(filename string, content []byte) error { ...@@ -270,3 +271,42 @@ func compareContent(filename string, content []byte) error {
return nil return nil
} }
func TestExpand(t *testing.T) {
r, err := os.Open(testarchive)
if err != nil {
t.Errorf("Failed to read testarchive file: %s", err)
return
}
td, err := ioutil.TempDir("", "helm-unittest-chart-")
if err != nil {
t.Errorf("Failed to create tempdir: %s", err)
return
}
err = Expand(td, r)
if err != nil {
t.Errorf("Failed to expand testarchive file: %s", err)
}
fi, err := os.Lstat(td + "/frobnitz/Chart.yaml")
if err != nil {
t.Errorf("Failed to stat Chart.yaml from expanded archive: %s", err)
}
if fi.Name() != "Chart.yaml" {
t.Errorf("Didn't get the right file name from stat, expected Chart.yaml, got: %s", fi.Name())
}
tr, err := os.Open(td + "/frobnitz/templates/template.tpl")
if err != nil {
t.Errorf("Failed to open template.tpl from expanded archive: %s", err)
}
c, err := ioutil.ReadAll(tr)
if err != nil {
t.Errorf("Failed to read contents of template.tpl from expanded archive: %s", err)
}
if string(c) != expectedTemplate {
t.Errorf("Contents of the expanded template differ, wanted '%s' got '%s'", expectedTemplate, c)
}
}
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