Skip to content

Commit c510096

Browse files
authored
CLOUDP-270626 Fix executing "output" at <.Results> (#3313)
1 parent 1a4ec00 commit c510096

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

internal/cli/output_opts.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222
"os"
23+
"reflect"
2324
"strings"
2425

2526
"github.com/PaesslerAG/jsonpath"
@@ -108,6 +109,40 @@ func (opts *OutputOpts) IsCygwinTerminal() bool {
108109
return terminal.IsCygwinTerminal(opts.OutWriter)
109110
}
110111

112+
func isNil(o any) bool {
113+
if o == nil {
114+
return true
115+
}
116+
ot := reflect.TypeOf(o)
117+
otk := ot.Kind()
118+
switch otk { //nolint:exhaustive // clearer code
119+
case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.Pointer, reflect.UnsafePointer, reflect.Interface:
120+
return reflect.ValueOf(o).IsNil()
121+
default:
122+
return false
123+
}
124+
}
125+
126+
func isOrPtrToSliceOrArray(o any) bool {
127+
ot := reflect.TypeOf(o)
128+
if ot == nil {
129+
return false
130+
}
131+
otk := ot.Kind()
132+
switch otk { //nolint:exhaustive // clearer code
133+
case reflect.Array, reflect.Slice:
134+
return true
135+
case reflect.Pointer:
136+
opt := reflect.PointerTo(ot)
137+
optk := opt.Kind()
138+
switch optk { //nolint:exhaustive // clearer code
139+
case reflect.Array, reflect.Slice:
140+
return true
141+
}
142+
}
143+
return false
144+
}
145+
111146
// Print will evaluate the defined format and try to parse it accordingly outputting to the set writer.
112147
func (opts *OutputOpts) Print(o any) error {
113148
if opts.ConfigOutput() == jsonFormat {
@@ -125,6 +160,13 @@ func (opts *OutputOpts) Print(o any) error {
125160
}
126161

127162
if t != "" {
163+
if isNil(o) {
164+
if isOrPtrToSliceOrArray(o) {
165+
o = []map[string]any{}
166+
} else {
167+
o = map[string]any{}
168+
}
169+
}
128170
return templatewriter.Print(opts.ConfigWriter(), t, o)
129171
}
130172
_, err = fmt.Fprintln(opts.ConfigWriter(), o)

0 commit comments

Comments
 (0)