Skip to content

Commit 43e496b

Browse files
committed
cli/command/inspect: minor cleanup and improvements
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent cacd86c commit 43e496b

1 file changed

Lines changed: 43 additions & 28 deletions

File tree

cli/command/inspect/inspector.go

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,29 @@ type Inspector interface {
2626

2727
// TemplateInspector uses a text template to inspect elements.
2828
type TemplateInspector struct {
29-
outputStream io.Writer
30-
buffer *bytes.Buffer
31-
tmpl *template.Template
29+
out io.Writer
30+
buffer *bytes.Buffer
31+
tmpl *template.Template
3232
}
3333

3434
// NewTemplateInspector creates a new inspector with a template.
35-
func NewTemplateInspector(outputStream io.Writer, tmpl *template.Template) Inspector {
35+
func NewTemplateInspector(out io.Writer, tmpl *template.Template) *TemplateInspector {
36+
if out == nil {
37+
out = io.Discard
38+
}
3639
return &TemplateInspector{
37-
outputStream: outputStream,
38-
buffer: new(bytes.Buffer),
39-
tmpl: tmpl,
40+
out: out,
41+
buffer: new(bytes.Buffer),
42+
tmpl: tmpl,
4043
}
4144
}
4245

4346
// NewTemplateInspectorFromString creates a new TemplateInspector from a string
4447
// which is compiled into a template.
4548
func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, error) {
49+
if out == nil {
50+
return nil, errors.New("no output stream")
51+
}
4652
if tmplStr == "" {
4753
return NewIndentedInspector(out), nil
4854
}
@@ -65,6 +71,9 @@ type GetRefFunc func(ref string) (any, []byte, error)
6571
// Inspect fetches objects by reference using GetRefFunc and writes the json
6672
// representation to the output writer.
6773
func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFunc) error {
74+
if out == nil {
75+
return errors.New("no output stream")
76+
}
6877
inspector, err := NewTemplateInspectorFromString(out, tmplStr)
6978
if err != nil {
7079
return cli.StatusError{StatusCode: 64, Status: err.Error()}
@@ -138,18 +147,21 @@ func (i *TemplateInspector) tryRawInspectFallback(rawElement []byte) error {
138147
// Flush writes the result of inspecting all elements into the output stream.
139148
func (i *TemplateInspector) Flush() error {
140149
if i.buffer.Len() == 0 {
141-
_, err := io.WriteString(i.outputStream, "\n")
150+
_, err := io.WriteString(i.out, "\n")
142151
return err
143152
}
144-
_, err := io.Copy(i.outputStream, i.buffer)
153+
_, err := io.Copy(i.out, i.buffer)
145154
return err
146155
}
147156

148157
// NewIndentedInspector generates a new inspector with an indented representation
149158
// of elements.
150-
func NewIndentedInspector(outputStream io.Writer) Inspector {
151-
return &elementsInspector{
152-
outputStream: outputStream,
159+
func NewIndentedInspector(out io.Writer) Inspector {
160+
if out == nil {
161+
out = io.Discard
162+
}
163+
return &jsonInspector{
164+
out: out,
153165
raw: func(dst *bytes.Buffer, src []byte) error {
154166
return json.Indent(dst, src, "", " ")
155167
},
@@ -161,23 +173,26 @@ func NewIndentedInspector(outputStream io.Writer) Inspector {
161173

162174
// NewJSONInspector generates a new inspector with a compact representation
163175
// of elements.
164-
func NewJSONInspector(outputStream io.Writer) Inspector {
165-
return &elementsInspector{
166-
outputStream: outputStream,
167-
raw: json.Compact,
168-
el: json.Marshal,
176+
func NewJSONInspector(out io.Writer) Inspector {
177+
if out == nil {
178+
out = io.Discard
179+
}
180+
return &jsonInspector{
181+
out: out,
182+
raw: json.Compact,
183+
el: json.Marshal,
169184
}
170185
}
171186

172-
type elementsInspector struct {
173-
outputStream io.Writer
174-
elements []any
175-
rawElements [][]byte
176-
raw func(dst *bytes.Buffer, src []byte) error
177-
el func(v any) ([]byte, error)
187+
type jsonInspector struct {
188+
out io.Writer
189+
elements []any
190+
rawElements [][]byte
191+
raw func(dst *bytes.Buffer, src []byte) error
192+
el func(v any) ([]byte, error)
178193
}
179194

180-
func (e *elementsInspector) Inspect(typedElement any, rawElement []byte) error {
195+
func (e *jsonInspector) Inspect(typedElement any, rawElement []byte) error {
181196
if rawElement != nil {
182197
e.rawElements = append(e.rawElements, rawElement)
183198
} else {
@@ -186,9 +201,9 @@ func (e *elementsInspector) Inspect(typedElement any, rawElement []byte) error {
186201
return nil
187202
}
188203

189-
func (e *elementsInspector) Flush() error {
204+
func (e *jsonInspector) Flush() error {
190205
if len(e.elements) == 0 && len(e.rawElements) == 0 {
191-
_, err := io.WriteString(e.outputStream, "[]\n")
206+
_, err := io.WriteString(e.out, "[]\n")
192207
return err
193208
}
194209

@@ -216,9 +231,9 @@ func (e *elementsInspector) Flush() error {
216231
buffer = bytes.NewReader(b)
217232
}
218233

219-
if _, err := io.Copy(e.outputStream, buffer); err != nil {
234+
if _, err := io.Copy(e.out, buffer); err != nil {
220235
return err
221236
}
222-
_, err := io.WriteString(e.outputStream, "\n")
237+
_, err := io.WriteString(e.out, "\n")
223238
return err
224239
}

0 commit comments

Comments
 (0)