From be317c412a07f2715253427ce5d56e5e0ffab937 Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Wed, 6 Jul 2022 15:39:46 +0800 Subject: [PATCH] Feat: print complex values in addon parameters (#4309) * Feat: support nested parameters in addon parameters Signed-off-by: Charlie Chiang * Feat: show all params Signed-off-by: Charlie Chiang * Feat: handle any addon parameters Signed-off-by: Charlie Chiang * Refactor: add a error check on json marshell Signed-off-by: Charlie Chiang * Test: fix tests Signed-off-by: Charlie Chiang --- references/cli/addon.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/references/cli/addon.go b/references/cli/addon.go index ccfa20c39..f7cdeedcd 100644 --- a/references/cli/addon.go +++ b/references/cli/addon.go @@ -18,6 +18,7 @@ package cli import ( "context" + "encoding/json" "fmt" "os" "path/filepath" @@ -753,12 +754,13 @@ func printSchema(ref *openapi3.Schema, currentParams map[string]interface{}, ind } required := required[propKey] + // Extra indentation on nested objects + addedIndent := addIndent(indent) + var currentValue string thisParam, hasParam := currentParams[propKey] if hasParam { - // Only show default parameter when it is a string, int, float, or bool - // We don't care about object's default values (they have no defaults anyway). - currentValue = fmt.Sprint(thisParam) + currentValue = fmt.Sprintf("%#v", thisParam) switch thisParam.(type) { case int: case int64: @@ -768,13 +770,12 @@ func printSchema(ref *openapi3.Schema, currentParams map[string]interface{}, ind case string: case bool: default: - currentValue = "" + if js, err := json.MarshalIndent(thisParam, "", " "); err == nil { + currentValue = strings.ReplaceAll(string(js), "\n", "\n\t "+addedIndent) + } } } - // Extra indentation on nested objects - addedIndent := addIndent(indent) - // Header: addon: description ret += addedIndent ret += color.New(color.FgCyan).Sprintf("-> ") @@ -783,8 +784,8 @@ func printSchema(ref *openapi3.Schema, currentParams map[string]interface{}, ind // Show current value if currentValue != "" { - ret += addIndent(indent) - ret += "\tcurrent: " + color.New(color.FgGreen).Sprintf("%#v\n", currentValue) + ret += addedIndent + ret += "\tcurrent: " + color.New(color.FgGreen).Sprintf("%s\n", currentValue) } // Show default value if defaultValue != "" {