/
parse.go
64 lines (62 loc) · 1.95 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package controllers
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"fmt"
"io"
"os"
"strings"
)
// parseValuesAndQuestions parses the base64TgzChart and emits the values.yaml and questions.yaml contained within it
// If values.yaml or questions.yaml are not specified, it will return an empty string for each
func parseValuesAndQuestions(base64TgzChart string) (string, string, error) {
tgzChartBytes, err := base64.StdEncoding.DecodeString(base64TgzChart)
if err != nil {
return "", "", fmt.Errorf("unable to decode base64TgzChart to tgzChart: %s", err)
}
gzipReader, err := gzip.NewReader(bytes.NewReader(tgzChartBytes))
if err != nil {
return "", "", fmt.Errorf("unable to create gzipReader to read from base64TgzChart: %s", err)
}
defer gzipReader.Close()
tarReader := tar.NewReader(gzipReader)
var valuesYamlBuffer, questionsYamlBuffer bytes.Buffer
var foundValuesYaml, foundQuestionsYaml bool
for {
h, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return "", "", err
}
if h.Typeflag != tar.TypeReg {
continue
}
splitName := strings.SplitN(h.Name, string(os.PathSeparator), 2)
nameWithoutRootDir := splitName[0]
if len(splitName) > 1 {
nameWithoutRootDir = splitName[1]
}
if nameWithoutRootDir == "values.yaml" || nameWithoutRootDir == "values.yml" {
if foundValuesYaml {
// multiple values.yaml
return "", "", errors.New("multiple values.yaml or values.yml found in base64TgzChart provided")
}
foundValuesYaml = true
io.Copy(&valuesYamlBuffer, tarReader)
}
if nameWithoutRootDir == "questions.yaml" || nameWithoutRootDir == "questions.yml" {
if foundQuestionsYaml {
// multiple values.yaml
return "", "", errors.New("multiple questions.yaml or questions.yml found in base64TgzChart provided")
}
foundQuestionsYaml = true
io.Copy(&questionsYamlBuffer, tarReader)
}
}
return valuesYamlBuffer.String(), questionsYamlBuffer.String(), nil
}