Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pkg/util/strvals/strvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"fmt"
"io"
"reflect"
"strconv"
"strings"

Expand Down Expand Up @@ -250,7 +251,18 @@ func set(data map[string]interface{}, key string, val interface{}) {
if len(key) == 0 {
return
}
data[key] = val

rt := reflect.TypeOf(val)
switch rt.Kind() {
case reflect.String:
if len(val.(string)) < 1 {
data[key] = nil
} else {
data[key] = val
}
default:
data[key] = val
}
}

func setIndex(list []interface{}, index int, val interface{}) (l2 []interface{}, err error) {
Expand All @@ -271,7 +283,16 @@ func setIndex(list []interface{}, index int, val interface{}) (l2 []interface{},
copy(newlist, list)
list = newlist
}
list[index] = val
switch reflect.TypeOf(val).Kind() {
case reflect.String:
if len(val.(string)) > 0 {
list[index] = val
} else {
list[index] = nil
}
default:
list[index] = val
}
return list, nil
}

Expand Down
72 changes: 72 additions & 0 deletions pkg/util/strvals/strvals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package strvals

import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"gotest.tools/assert"
"log"
"testing"
)

func TestSetStringFlag(t *testing.T) {
rawConfig := getActualRawConfig()
s := "deployments.dev.helm.values.containers[0]="
err := ParseIntoString(s, rawConfig)
if err != nil {
fmt.Println(errors.Wrap(err, "parsing --set-string flag"))
log.Fatal(err)
}
b, err := json.Marshal(rawConfig)
if err != nil {
fmt.Println(err)
}
fmt.Println("output : " + string(b))
assert.DeepEqual(t, rawConfig, getExpectedRawConfigForSetString())
}

func TestSetFlag(t *testing.T) {
s := "deployments.dev.helm.values.containers[1].image="
rawConfig := getActualRawConfig()
err := ParseInto(s, rawConfig)
if err != nil {
fmt.Println(errors.Wrap(err, "parsing --set flag"))
log.Fatal(err)
}
b, err := json.Marshal(rawConfig)
if err != nil {
fmt.Println(err)
}
fmt.Println("output : " + string(b))
assert.DeepEqual(t, rawConfig, getExpectedRawConfigForSet())
}

func getExpectedRawConfigForSet() map[string]interface{} {
jsonStr := "{\"deployments\":{\"dev\":{\"helm\":{\"values\":{\"containers\":[{\"image\":\"alpine\"},{\"image\":null}]}}}},\"name\":\"run-pipelines-demo\",\"pipelines\":{\"deploy\":\"create_deployments --all\",\"dev\":\"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"},\"version\":\"v2beta1\"}"
rawConfig := map[string]interface{}{}
err := json.Unmarshal([]byte(jsonStr), &rawConfig)
if err != nil {
fmt.Println(err)
}
return rawConfig
}

func getExpectedRawConfigForSetString() map[string]interface{} {
jsonStr := "{\"deployments\":{\"dev\":{\"helm\":{\"values\":{\"containers\":[null,{\"image\":\"ns\"}]}}}},\"name\":\"run-pipelines-demo\",\"pipelines\":{\"deploy\":\"create_deployments --all\",\"dev\":\"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"},\"version\":\"v2beta1\"}"
rawConfig := map[string]interface{}{}
err := json.Unmarshal([]byte(jsonStr), &rawConfig)
if err != nil {
fmt.Println(err)
}
return rawConfig
}

func getActualRawConfig() map[string]interface{} {
jsonStr := "{\n \"deployments\": {\n \"dev\": { \"helm\": { \"values\": { \"containers\": [{ \"image\": \"alpine\" },{ \"image\": \"ns\" }] } } }\n },\n \"name\": \"run-pipelines-demo\",\n \"pipelines\": {\n \"deploy\": \"create_deployments --all\",\n \"dev\": \"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"\n },\n \"version\": \"v2beta1\"\n}\n"
rawConfig := map[string]interface{}{}
err := json.Unmarshal([]byte(jsonStr), &rawConfig)
if err != nil {
fmt.Println(err)
}
return rawConfig
}