From 45f9d32ffc0c90b4691203ab5eeaa1eda981007c Mon Sep 17 00:00:00 2001
From: Matt Butcher <mbutcher@engineyard.com>
Date: Wed, 3 Feb 2016 15:57:45 -0700
Subject: [PATCH] Add LoadData() for server-side reading.

---
 chart/chart.go      | 24 ++++++++++++++++++++++++
 chart/chart_test.go | 23 ++++++++++++++++++++++-
 2 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/chart/chart.go b/chart/chart.go
index 716bd4630..a7b3b4cee 100644
--- a/chart/chart.go
+++ b/chart/chart.go
@@ -18,6 +18,7 @@ package chart
 
 import (
 	"archive/tar"
+	"bytes"
 	"compress/gzip"
 	"errors"
 	"fmt"
@@ -240,6 +241,29 @@ func LoadDir(chart string) (*Chart, error) {
 	}, nil
 }
 
+// LoadData loads a chart from data, where data is a []byte containing a gzipped tar file.
+func LoadData(data []byte) (*Chart, error) {
+	b := bytes.NewBuffer(data)
+	unzipped, err := gzip.NewReader(b)
+	if err != nil {
+		return nil, err
+	}
+	defer unzipped.Close()
+
+	untarred := tar.NewReader(unzipped)
+	c, err := loadTar(untarred)
+	if err != nil {
+		return nil, err
+	}
+
+	cf, err := LoadChartfile(filepath.Join(c.tmpDir, ChartfileName))
+	if err != nil {
+		return nil, err
+	}
+	c.chartyaml = cf
+	return &Chart{loader: c}, nil
+}
+
 // Load loads a chart from a chart archive.
 //
 // A chart archive is a gzipped tar archive that follows the Chart format
diff --git a/chart/chart_test.go b/chart/chart_test.go
index 542664729..88a1815a5 100644
--- a/chart/chart_test.go
+++ b/chart/chart_test.go
@@ -17,6 +17,7 @@ limitations under the License.
 package chart
 
 import (
+	"io/ioutil"
 	"path/filepath"
 	"testing"
 
@@ -57,7 +58,6 @@ func TestLoadDir(t *testing.T) {
 }
 
 func TestLoad(t *testing.T) {
-
 	c, err := Load(testarchive)
 	if err != nil {
 		t.Errorf("Failed to load chart: %s", err)
@@ -75,6 +75,27 @@ func TestLoad(t *testing.T) {
 	}
 }
 
+func TestLoadData(t *testing.T) {
+	data, err := ioutil.ReadFile(testarchive)
+	if err != nil {
+		t.Errorf("Failed to read testarchive file: %s", err)
+		return
+	}
+	c, err := LoadData(data)
+	if err != nil {
+		t.Errorf("Failed to load chart: %s", err)
+		return
+	}
+	if c.Chartfile() == nil {
+		t.Error("No chartfile was loaded.")
+		return
+	}
+
+	if c.Chartfile().Name != "frobnitz" {
+		t.Errorf("Expected name to be frobnitz, got %q", c.Chartfile().Name)
+	}
+}
+
 func TestLoadIll(t *testing.T) {
 	c, err := Load(testill)
 	if err != nil {
-- 
GitLab