Skip to content

Commit d09afc0

Browse files
committed
Output compact JSON by default for --format=json
With this change all `inspect` commands will output a compact JSON representation of the elements, the default format (indented JSON) stays the same. Signed-off-by: Djordje Lukic <[email protected]>
1 parent 4935449 commit d09afc0

File tree

13 files changed

+62
-42
lines changed

13 files changed

+62
-42
lines changed

cli/command/config/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
3131
},
3232
}
3333

34-
cmd.Flags().StringVarP(&opts.Format, "format", "f", "json", flagsHelper.InspectFormatHelp)
34+
cmd.Flags().StringVarP(&opts.Format, "format", "f", "", flagsHelper.InspectFormatHelp)
3535
cmd.Flags().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format")
3636
return cmd
3737
}

cli/command/container/inspect.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package container
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/docker/cli/cli"
78
"github.com/docker/cli/cli/command"
@@ -31,7 +32,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
3132
}
3233

3334
flags := cmd.Flags()
34-
flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
35+
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3536
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes")
3637

3738
return cmd
@@ -44,5 +45,6 @@ func runInspect(dockerCli command.Cli, opts inspectOptions) error {
4445
getRefFunc := func(ref string) (interface{}, []byte, error) {
4546
return client.ContainerInspectWithRaw(ctx, ref, opts.size)
4647
}
48+
fmt.Println(opts.format)
4749
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
4850
}

cli/command/context/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
3535
}
3636

3737
flags := cmd.Flags()
38-
flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
38+
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3939
return cmd
4040
}
4141

cli/command/image/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
3030
}
3131

3232
flags := cmd.Flags()
33-
flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
33+
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3434
return cmd
3535
}
3636

cli/command/inspect/inspector.go

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515

1616
// Inspector defines an interface to implement to process elements
1717
type Inspector interface {
18+
// Inspect writes the raw element in JSON format.
1819
Inspect(typedElement interface{}, rawElement []byte) error
20+
// Flush writes the result of inspecting all elements into the output stream.
1921
Flush() error
2022
}
2123

@@ -38,10 +40,14 @@ func NewTemplateInspector(outputStream io.Writer, tmpl *template.Template) Inspe
3840
// NewTemplateInspectorFromString creates a new TemplateInspector from a string
3941
// which is compiled into a template.
4042
func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, error) {
41-
if tmplStr == "" || tmplStr == "json" {
43+
if tmplStr == "" {
4244
return NewIndentedInspector(out), nil
4345
}
4446

47+
if tmplStr == "json" {
48+
return NewJSONInspector(out), nil
49+
}
50+
4551
tmpl, err := templates.Parse(tmplStr)
4652
if err != nil {
4753
return nil, errors.Errorf("Template parsing error: %s", err)
@@ -136,64 +142,80 @@ func (i *TemplateInspector) Flush() error {
136142
return err
137143
}
138144

139-
// IndentedInspector uses a buffer to stop the indented representation of an element.
140-
type IndentedInspector struct {
141-
outputStream io.Writer
142-
elements []interface{}
143-
rawElements [][]byte
145+
// NewIndentedInspector generates a new inspector with an indented representation
146+
// of elements.
147+
func NewIndentedInspector(outputStream io.Writer) Inspector {
148+
return &elementsInspector{
149+
outputStream: outputStream,
150+
raw: func(dst *bytes.Buffer, src []byte) error {
151+
return json.Indent(dst, src, "", " ")
152+
},
153+
el: func(v interface{}) ([]byte, error) {
154+
return json.MarshalIndent(v, "", " ")
155+
},
156+
}
144157
}
145158

146-
// NewIndentedInspector generates a new IndentedInspector.
147-
func NewIndentedInspector(outputStream io.Writer) Inspector {
148-
return &IndentedInspector{
159+
// NewJSONInspector generates a new inspector with a compact representation
160+
// of elements.
161+
func NewJSONInspector(outputStream io.Writer) Inspector {
162+
return &elementsInspector{
149163
outputStream: outputStream,
164+
raw: json.Compact,
165+
el: json.Marshal,
150166
}
151167
}
152168

153-
// Inspect writes the raw element with an indented json format.
154-
func (i *IndentedInspector) Inspect(typedElement interface{}, rawElement []byte) error {
169+
type elementsInspector struct {
170+
outputStream io.Writer
171+
elements []interface{}
172+
rawElements [][]byte
173+
raw func(dst *bytes.Buffer, src []byte) error
174+
el func(v interface{}) ([]byte, error)
175+
}
176+
177+
func (e *elementsInspector) Inspect(typedElement interface{}, rawElement []byte) error {
155178
if rawElement != nil {
156-
i.rawElements = append(i.rawElements, rawElement)
179+
e.rawElements = append(e.rawElements, rawElement)
157180
} else {
158-
i.elements = append(i.elements, typedElement)
181+
e.elements = append(e.elements, typedElement)
159182
}
160183
return nil
161184
}
162185

163-
// Flush writes the result of inspecting all elements into the output stream.
164-
func (i *IndentedInspector) Flush() error {
165-
if len(i.elements) == 0 && len(i.rawElements) == 0 {
166-
_, err := io.WriteString(i.outputStream, "[]\n")
186+
func (e *elementsInspector) Flush() error {
187+
if len(e.elements) == 0 && len(e.rawElements) == 0 {
188+
_, err := io.WriteString(e.outputStream, "[]\n")
167189
return err
168190
}
169191

170192
var buffer io.Reader
171-
if len(i.rawElements) > 0 {
193+
if len(e.rawElements) > 0 {
172194
bytesBuffer := new(bytes.Buffer)
173195
bytesBuffer.WriteString("[")
174-
for idx, r := range i.rawElements {
196+
for idx, r := range e.rawElements {
175197
bytesBuffer.Write(r)
176-
if idx < len(i.rawElements)-1 {
198+
if idx < len(e.rawElements)-1 {
177199
bytesBuffer.WriteString(",")
178200
}
179201
}
180202
bytesBuffer.WriteString("]")
181-
indented := new(bytes.Buffer)
182-
if err := json.Indent(indented, bytesBuffer.Bytes(), "", " "); err != nil {
203+
output := new(bytes.Buffer)
204+
if err := e.raw(output, bytesBuffer.Bytes()); err != nil {
183205
return err
184206
}
185-
buffer = indented
207+
buffer = output
186208
} else {
187-
b, err := json.MarshalIndent(i.elements, "", " ")
209+
b, err := e.el(e.elements)
188210
if err != nil {
189211
return err
190212
}
191213
buffer = bytes.NewReader(b)
192214
}
193215

194-
if _, err := io.Copy(i.outputStream, buffer); err != nil {
216+
if _, err := io.Copy(e.outputStream, buffer); err != nil {
195217
return err
196218
}
197-
_, err := io.WriteString(i.outputStream, "\n")
219+
_, err := io.WriteString(e.outputStream, "\n")
198220
return err
199221
}

cli/command/inspect/inspector_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,7 @@ func TestNewTemplateInspectorFromString(t *testing.T) {
277277
{
278278
name: "json specific value outputs json",
279279
template: "json",
280-
expected: `[
281-
{
282-
"Name": "test"
283-
}
284-
]
280+
expected: `[{"Name":"test"}]
285281
`,
286282
},
287283
{

cli/command/network/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
3030
},
3131
}
3232

33-
cmd.Flags().StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
33+
cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3434
cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", false, "Verbose output for diagnostics")
3535

3636
return cmd

cli/command/node/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
3232
}
3333

3434
flags := cmd.Flags()
35-
flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
35+
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3636
flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
3737
return cmd
3838
}

cli/command/plugin/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
2929
}
3030

3131
flags := cmd.Flags()
32-
flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
32+
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3333
return cmd
3434
}
3535

cli/command/secret/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func newSecretInspectCommand(dockerCli command.Cli) *cobra.Command {
3030
},
3131
}
3232

33-
cmd.Flags().StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
33+
cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
3434
cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
3535
return cmd
3636
}

0 commit comments

Comments
 (0)