From 525ecaa04178d717d275c3c260d3b4f6c399b78c Mon Sep 17 00:00:00 2001 From: FengyunPan Date: Wed, 14 Jun 2017 09:37:39 +0800 Subject: [PATCH] Add friendly message when fetch chart --- cmd/helm/fetch.go | 8 +++++- pkg/chartutil/expand.go | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/cmd/helm/fetch.go b/cmd/helm/fetch.go index a639e7f02..85876d62b 100644 --- a/cmd/helm/fetch.go +++ b/cmd/helm/fetch.go @@ -169,7 +169,13 @@ func (f *fetchCmd) run() error { return fmt.Errorf("Failed to untar: %s is not a directory", ud) } - return chartutil.ExpandFile(ud, saved) + expandDir, err := chartutil.ExpandFileWithPath(ud, saved) + if err != nil { + return fmt.Errorf("Failed to untar: %s", err) + } + fmt.Fprintf(f.out, "Untared the chart into: %s\n", expandDir) + } else { + fmt.Fprintf(f.out, "Downloaded the chart into: %s\n", saved) } return nil } diff --git a/pkg/chartutil/expand.go b/pkg/chartutil/expand.go index ae28f8147..ca19e95fc 100644 --- a/pkg/chartutil/expand.go +++ b/pkg/chartutil/expand.go @@ -81,3 +81,67 @@ func ExpandFile(dest, src string) error { defer h.Close() return Expand(dest, h) } + +// Expand uncompresses and extracts a chart into the specified directory. +// Return the directory when extracting successfully. +func ExpandWithPath(dir string, r io.Reader) (string, error) { + gr, err := gzip.NewReader(r) + if err != nil { + return "", err + } + defer gr.Close() + tr := tar.NewReader(gr) + expandDir := "" + for { + header, err := tr.Next() + if err == io.EOF { + break + } else if err != nil { + return "", err + } + + //split header name and create missing directories + d, _ := filepath.Split(header.Name) + fullDir := filepath.Join(dir, d) + _, err = os.Stat(fullDir) + if err != nil && d != "" { + if err := os.MkdirAll(fullDir, 0700); err != nil { + return "", err + } + } + //get first fullDir which saves the uncompressed chart + if expandDir == "" { + expandDir = fullDir + } + + 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 expandDir, nil +} + +// ExpandFile expands the src file into the dest directory. +func ExpandFileWithPath(dest, src string) (string, error) { + h, err := os.Open(src) + if err != nil { + return "", err + } + defer h.Close() + return ExpandWithPath(dest, h) +}