1
0
mirror of https://github.com/rancher/cli.git synced 2026-02-05 09:48:36 +01:00

Call yaml.Unmarshal on values/answers file using YAML

We use json.Unmarshal for files using JSON format, and convert the
YAML format files to JSON first and call json.Unmarshal on them as well.
The yaml to json conversion let to unquoted numbers getting converted to
scientific notation. So this commit fixes it by calling yaml.Unmarshal
directly on file with yaml format instead of converting them to json.
This commit is contained in:
rajashree
2019-09-30 15:04:29 -07:00
parent a8000549ed
commit d8d74599f0
2 changed files with 28 additions and 18 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@
/.trash-cache
/.idea
trash.lock
/cli

View File

@@ -1078,13 +1078,13 @@ func processAnswers(
}
if ctx.String("values") != "" {
if err := getValuesFile(ctx.String("values"), answers); err != nil {
if err := parseValuesFile(ctx.String("values"), answers); err != nil {
return answers, err
}
}
if ctx.String("answers") != "" {
err := getAnswersFile(ctx.String("answers"), answers)
err := parseAnswersFile(ctx.String("answers"), answers)
if err != nil {
return answers, err
}
@@ -1108,18 +1108,11 @@ func processAnswers(
return answers, nil
}
func getAnswersFile(location string, answers map[string]string) error {
bytes, err := readFileReturnJSON(location)
func parseAnswersFile(location string, answers map[string]string) error {
holder, err := parseFile(location)
if err != nil {
return err
}
holder := make(map[string]interface{})
err = json.Unmarshal(bytes, &holder)
if err != nil {
return err
}
for key, value := range holder {
switch value.(type) {
case nil:
@@ -1131,20 +1124,36 @@ func getAnswersFile(location string, answers map[string]string) error {
return nil
}
// getValuesFile reads a values file and parse it to answers in helm strvals format
func getValuesFile(location string, answers map[string]string) error {
bytes, err := readFileReturnJSON(location)
// parseValuesFile reads a values file and parses it to answers in helm strvals format
func parseValuesFile(location string, answers map[string]string) error {
values, err := parseFile(location)
if err != nil {
return err
}
values := make(map[string]interface{})
if err := json.Unmarshal(bytes, &values); err != nil {
return err
}
valuesToAnswers(values, answers)
return nil
}
func parseFile(location string) (map[string]interface{}, error) {
bytes, err := ioutil.ReadFile(location)
if err != nil {
return nil, err
}
values := make(map[string]interface{})
if hasPrefix(bytes, []byte("{")) {
// this is the check that "readFileReturnJSON" uses to differentiate between JSON and YAML
if err := json.Unmarshal(bytes, &values); err != nil {
return nil, err
}
} else {
if err := yaml.Unmarshal(bytes, &values); err != nil {
return nil, err
}
}
return values, nil
}
func valuesToAnswers(values map[string]interface{}, answers map[string]string) {
for k, v := range values {
traverseValuesToAnswers(k, v, answers)