Feat: print complex values in addon parameters (#4309)

* Feat: support nested parameters in addon parameters

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Feat: show all params

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Feat: handle any addon parameters

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Refactor: add a error check on json marshell

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Test: fix tests

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
This commit is contained in:
Charlie Chiang
2022-07-06 15:39:46 +08:00
committed by GitHub
parent a422ae8ab4
commit be317c412a
+10 -9
View File
@@ -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 != "" {