Skip to content

Commit

Permalink
fix: Go template rendering (#5042)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Feb 21, 2024
1 parent 43883c3 commit 62fb735
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
9 changes: 7 additions & 2 deletions cmd/kubectl-testkube/commands/common/render/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package render
import (
"fmt"
"io"
"reflect"

"github.com/spf13/cobra"

Expand All @@ -25,10 +26,14 @@ func List(cmd *cobra.Command, obj interface{}, w io.Writer) error {
return RenderJSON(obj, w)
case OutputGoTemplate:
tpl := cmd.Flag("go-template").Value.String()
list, ok := obj.([]interface{})
if !ok {
value := reflect.ValueOf(obj)
if value.Kind() != reflect.Slice {
return fmt.Errorf("can't render, need list type but got: %+v", obj)
}
list := make([]interface{}, value.Len())
for i := 0; i < value.Len(); i++ {
list[i] = value.Index(i).Interface()
}
return RenderGoTemplateList(list, w, tpl)
default:
return RenderYaml(obj, w)
Expand Down
8 changes: 1 addition & 7 deletions cmd/kubectl-testkube/commands/common/render/obj.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"fmt"
"io"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -30,12 +29,7 @@ func Obj(cmd *cobra.Command, obj interface{}, w io.Writer, renderer ...CliObjRen
return RenderJSON(obj, w)
case OutputGoTemplate:
tpl := cmd.Flag("go-template").Value.String()
// need to make type assetion to list first
list, ok := obj.([]interface{})
if !ok {
return fmt.Errorf("can't render, need list type but got: %+v", obj)
}
return RenderGoTemplateList(list, w, tpl)
return RenderGoTemplate(obj, w, tpl)
default:
return RenderYaml(obj, w)
}
Expand Down

0 comments on commit 62fb735

Please sign in to comment.