From 302f990979bd9500bceb1465c84d401a64c80295 Mon Sep 17 00:00:00 2001 From: berkgokden Date: Tue, 7 May 2019 14:12:59 +0200 Subject: [PATCH] Fix for json path, now it supports Yaml and large structs LAM-247 --- cmd/get.go | 1 + cmd/root.go | 2 +- util/util.go | 19 ++++++++++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index 64a4255..571adf1 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -73,6 +73,7 @@ Json path example with wait if JsonPath != "" { resultPath, jsonpathError := util.GetJsonPath(result, OutputType, JsonPath) if jsonpathError != nil { + getError = jsonpathError time.Sleep(5 * time.Second) continue } diff --git a/cmd/root.go b/cmd/root.go index 91a1424..63437dc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,7 +60,7 @@ var Hosts []string var kubeConfigPath string // version should be in format d.d.d where d is a decimal number -const Version string = "v0.0.28" +const Version string = "v0.0.29" var AppName string = InitAppName() diff --git a/util/util.go b/util/util.go index c285f72..b97a2fe 100644 --- a/util/util.go +++ b/util/util.go @@ -125,8 +125,12 @@ func DownloadFile(filepath string, url string) error { } func GetJsonPath(source string, sourceFormat string, jsonPath string) (string, error) { + sourceAsJson, conversionToJsonError := Convert(sourceFormat, "json", source) + if conversionToJsonError != nil { + return "", conversionToJsonError + } var jsonInterface map[string]interface{} - err := json.Unmarshal([]byte(source), &jsonInterface) + err := json.Unmarshal([]byte(sourceAsJson), &jsonInterface) if err != nil { return "", err } @@ -134,11 +138,16 @@ func GetJsonPath(source string, sourceFormat string, jsonPath string) (string, e if err != nil { return "", err } - str, ok := resultPath.(string) - if !ok { - return "", errors.New("There is no string representation for " + jsonPath) + strJson, jsonMarshalError := json.Marshal(resultPath) + if jsonMarshalError != nil { + return "", jsonMarshalError + } + + sourceAsSourceFormat, conversionFromJsonError := Convert("json", sourceFormat, string(strJson)) + if conversionFromJsonError != nil { + return "", conversionFromJsonError } - return str, nil + return sourceAsSourceFormat, nil } func VerifyCertForHost(resourceUrl string, cert string) error {