marshal bytes to return as string with kubectl config view -o jsonpath

This commit is contained in:
Mike Dame 2018-06-12 13:50:47 -04:00
parent e6f64d0a79
commit a8b7c94620

View File

@ -22,7 +22,6 @@ import (
"io"
"reflect"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/jsonpath"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
@ -124,7 +123,9 @@ func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
}
var queryObj interface{} = obj
if meta.IsListType(obj) {
if unstructured, ok := obj.(runtime.Unstructured); ok {
queryObj = unstructured.UnstructuredContent()
} else {
data, err := json.Marshal(obj)
if err != nil {
return err
@ -135,20 +136,6 @@ func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
}
}
if unknown, ok := obj.(*runtime.Unknown); ok {
data, err := json.Marshal(unknown)
if err != nil {
return err
}
queryObj = map[string]interface{}{}
if err := json.Unmarshal(data, &queryObj); err != nil {
return err
}
}
if unstructured, ok := obj.(runtime.Unstructured); ok {
queryObj = unstructured.UnstructuredContent()
}
if err := j.JSONPath.Execute(w, queryObj); err != nil {
fmt.Fprintf(w, "Error executing template: %v. Printing more information for debugging the template:\n", err)
fmt.Fprintf(w, "\ttemplate was:\n\t\t%v\n", j.rawTemplate)