From 9c0234bbcbf8ba13e7da8d0933c2da55e16e2d27 Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Tue, 8 Mar 2022 14:54:21 +0100 Subject: [PATCH] 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 Signed-off-by: Sebastiaan van Stijn --- cli/command/config/inspect.go | 2 +- cli/command/container/inspect.go | 2 +- cli/command/context/inspect.go | 2 +- cli/command/image/inspect.go | 2 +- cli/command/inspect/inspector.go | 76 ++++++++++++------- cli/command/inspect/inspector_test.go | 6 +- cli/command/network/inspect.go | 2 +- cli/command/node/inspect.go | 2 +- cli/command/plugin/inspect.go | 2 +- cli/command/secret/inspect.go | 2 +- cli/command/service/inspect.go | 2 +- cli/command/system/inspect.go | 2 +- cli/command/volume/inspect.go | 2 +- docs/reference/commandline/config_inspect.md | 2 +- docs/reference/commandline/context_inspect.md | 2 +- docs/reference/commandline/info.md | 2 +- docs/reference/commandline/network_inspect.md | 2 +- docs/reference/commandline/node_inspect.md | 2 +- docs/reference/commandline/plugin_inspect.md | 2 +- docs/reference/commandline/secret_inspect.md | 2 +- docs/reference/commandline/service_inspect.md | 2 +- docs/reference/commandline/volume_inspect.md | 2 +- 22 files changed, 70 insertions(+), 52 deletions(-) diff --git a/cli/command/config/inspect.go b/cli/command/config/inspect.go index ddce83fd7a..a5b57c776f 100644 --- a/cli/command/config/inspect.go +++ b/cli/command/config/inspect.go @@ -31,7 +31,7 @@ func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command { }, } - cmd.Flags().StringVarP(&opts.Format, "format", "f", "json", flagsHelper.InspectFormatHelp) + cmd.Flags().StringVarP(&opts.Format, "format", "f", "", flagsHelper.InspectFormatHelp) cmd.Flags().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format") return cmd } diff --git a/cli/command/container/inspect.go b/cli/command/container/inspect.go index 168740fae0..b5d4060789 100644 --- a/cli/command/container/inspect.go +++ b/cli/command/container/inspect.go @@ -31,7 +31,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() - flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes") return cmd diff --git a/cli/command/context/inspect.go b/cli/command/context/inspect.go index 246436c1ca..65233bae15 100644 --- a/cli/command/context/inspect.go +++ b/cli/command/context/inspect.go @@ -35,7 +35,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() - flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) return cmd } diff --git a/cli/command/image/inspect.go b/cli/command/image/inspect.go index e08b5fc3d9..2e3d5bf086 100644 --- a/cli/command/image/inspect.go +++ b/cli/command/image/inspect.go @@ -30,7 +30,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() - flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) return cmd } diff --git a/cli/command/inspect/inspector.go b/cli/command/inspect/inspector.go index 15dbea14ce..307972e2b0 100644 --- a/cli/command/inspect/inspector.go +++ b/cli/command/inspect/inspector.go @@ -15,7 +15,9 @@ import ( // Inspector defines an interface to implement to process elements type Inspector interface { + // Inspect writes the raw element in JSON format. Inspect(typedElement interface{}, rawElement []byte) error + // Flush writes the result of inspecting all elements into the output stream. Flush() error } @@ -38,10 +40,14 @@ func NewTemplateInspector(outputStream io.Writer, tmpl *template.Template) Inspe // NewTemplateInspectorFromString creates a new TemplateInspector from a string // which is compiled into a template. func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, error) { - if tmplStr == "" || tmplStr == "json" { + if tmplStr == "" { return NewIndentedInspector(out), nil } + if tmplStr == "json" { + return NewJSONInspector(out), nil + } + tmpl, err := templates.Parse(tmplStr) if err != nil { return nil, errors.Errorf("Template parsing error: %s", err) @@ -136,64 +142,80 @@ func (i *TemplateInspector) Flush() error { return err } -// IndentedInspector uses a buffer to stop the indented representation of an element. -type IndentedInspector struct { - outputStream io.Writer - elements []interface{} - rawElements [][]byte -} - -// NewIndentedInspector generates a new IndentedInspector. +// NewIndentedInspector generates a new inspector with an indented representation +// of elements. func NewIndentedInspector(outputStream io.Writer) Inspector { - return &IndentedInspector{ + return &elementsInspector{ outputStream: outputStream, + raw: func(dst *bytes.Buffer, src []byte) error { + return json.Indent(dst, src, "", " ") + }, + el: func(v interface{}) ([]byte, error) { + return json.MarshalIndent(v, "", " ") + }, } } -// Inspect writes the raw element with an indented json format. -func (i *IndentedInspector) Inspect(typedElement interface{}, rawElement []byte) error { +// NewJSONInspector generates a new inspector with a compact representation +// of elements. +func NewJSONInspector(outputStream io.Writer) Inspector { + return &elementsInspector{ + outputStream: outputStream, + raw: json.Compact, + el: json.Marshal, + } +} + +type elementsInspector struct { + outputStream io.Writer + elements []interface{} + rawElements [][]byte + raw func(dst *bytes.Buffer, src []byte) error + el func(v interface{}) ([]byte, error) +} + +func (e *elementsInspector) Inspect(typedElement interface{}, rawElement []byte) error { if rawElement != nil { - i.rawElements = append(i.rawElements, rawElement) + e.rawElements = append(e.rawElements, rawElement) } else { - i.elements = append(i.elements, typedElement) + e.elements = append(e.elements, typedElement) } return nil } -// Flush writes the result of inspecting all elements into the output stream. -func (i *IndentedInspector) Flush() error { - if len(i.elements) == 0 && len(i.rawElements) == 0 { - _, err := io.WriteString(i.outputStream, "[]\n") +func (e *elementsInspector) Flush() error { + if len(e.elements) == 0 && len(e.rawElements) == 0 { + _, err := io.WriteString(e.outputStream, "[]\n") return err } var buffer io.Reader - if len(i.rawElements) > 0 { + if len(e.rawElements) > 0 { bytesBuffer := new(bytes.Buffer) bytesBuffer.WriteString("[") - for idx, r := range i.rawElements { + for idx, r := range e.rawElements { bytesBuffer.Write(r) - if idx < len(i.rawElements)-1 { + if idx < len(e.rawElements)-1 { bytesBuffer.WriteString(",") } } bytesBuffer.WriteString("]") - indented := new(bytes.Buffer) - if err := json.Indent(indented, bytesBuffer.Bytes(), "", " "); err != nil { + output := new(bytes.Buffer) + if err := e.raw(output, bytesBuffer.Bytes()); err != nil { return err } - buffer = indented + buffer = output } else { - b, err := json.MarshalIndent(i.elements, "", " ") + b, err := e.el(e.elements) if err != nil { return err } buffer = bytes.NewReader(b) } - if _, err := io.Copy(i.outputStream, buffer); err != nil { + if _, err := io.Copy(e.outputStream, buffer); err != nil { return err } - _, err := io.WriteString(i.outputStream, "\n") + _, err := io.WriteString(e.outputStream, "\n") return err } diff --git a/cli/command/inspect/inspector_test.go b/cli/command/inspect/inspector_test.go index 74c49b2f82..637f3b92d1 100644 --- a/cli/command/inspect/inspector_test.go +++ b/cli/command/inspect/inspector_test.go @@ -277,11 +277,7 @@ func TestNewTemplateInspectorFromString(t *testing.T) { { name: "json specific value outputs json", template: "json", - expected: `[ - { - "Name": "test" - } -] + expected: `[{"Name":"test"}] `, }, { diff --git a/cli/command/network/inspect.go b/cli/command/network/inspect.go index 890ac77506..89c1d6c0ca 100644 --- a/cli/command/network/inspect.go +++ b/cli/command/network/inspect.go @@ -30,7 +30,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { }, } - cmd.Flags().StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", false, "Verbose output for diagnostics") return cmd diff --git a/cli/command/node/inspect.go b/cli/command/node/inspect.go index 51f7c50e1d..977026f641 100644 --- a/cli/command/node/inspect.go +++ b/cli/command/node/inspect.go @@ -32,7 +32,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() - flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format") return cmd } diff --git a/cli/command/plugin/inspect.go b/cli/command/plugin/inspect.go index baf166fccf..c84711af2b 100644 --- a/cli/command/plugin/inspect.go +++ b/cli/command/plugin/inspect.go @@ -29,7 +29,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() - flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) return cmd } diff --git a/cli/command/secret/inspect.go b/cli/command/secret/inspect.go index 05962b9a8b..0a8df1bcea 100644 --- a/cli/command/secret/inspect.go +++ b/cli/command/secret/inspect.go @@ -30,7 +30,7 @@ func newSecretInspectCommand(dockerCli command.Cli) *cobra.Command { }, } - cmd.Flags().StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format") return cmd } diff --git a/cli/command/service/inspect.go b/cli/command/service/inspect.go index a06149ec01..59e4c03cd2 100644 --- a/cli/command/service/inspect.go +++ b/cli/command/service/inspect.go @@ -38,7 +38,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() - flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format") return cmd } diff --git a/cli/command/system/inspect.go b/cli/command/system/inspect.go index 56199aecde..e15d86a66d 100644 --- a/cli/command/system/inspect.go +++ b/cli/command/system/inspect.go @@ -37,7 +37,7 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command { } flags := cmd.Flags() - flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) flags.StringVar(&opts.inspectType, "type", "", "Return JSON for specified type") flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container") diff --git a/cli/command/volume/inspect.go b/cli/command/volume/inspect.go index 3d3887635e..c2fbc39c8c 100644 --- a/cli/command/volume/inspect.go +++ b/cli/command/volume/inspect.go @@ -28,7 +28,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { }, } - cmd.Flags().StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp) + cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) return cmd } diff --git a/docs/reference/commandline/config_inspect.md b/docs/reference/commandline/config_inspect.md index 1d8022bbbf..bb8c025cb0 100644 --- a/docs/reference/commandline/config_inspect.md +++ b/docs/reference/commandline/config_inspect.md @@ -15,7 +15,7 @@ Options: -f, --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. - Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates (default "json") + Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates --pretty Print the information in a human friendly format --help Print usage ``` diff --git a/docs/reference/commandline/context_inspect.md b/docs/reference/commandline/context_inspect.md index 6d038b428f..7125ec3ac8 100644 --- a/docs/reference/commandline/context_inspect.md +++ b/docs/reference/commandline/context_inspect.md @@ -15,7 +15,7 @@ Options: -f, --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. - Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates (default "json") + Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates ``` ## Description diff --git a/docs/reference/commandline/info.md b/docs/reference/commandline/info.md index 316e301ab6..0071edd862 100644 --- a/docs/reference/commandline/info.md +++ b/docs/reference/commandline/info.md @@ -12,7 +12,7 @@ Usage: docker info [OPTIONS] Display system-wide information Options: - -f, --format string Format the output using the given Go template (default "json") + -f, --format string Format the output using the given Go template --help Print usage ``` diff --git a/docs/reference/commandline/network_inspect.md b/docs/reference/commandline/network_inspect.md index 2c0150253a..45dc88d3b4 100644 --- a/docs/reference/commandline/network_inspect.md +++ b/docs/reference/commandline/network_inspect.md @@ -15,7 +15,7 @@ Options: -f, --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. - Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates (default "json") + Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates -v, --verbose Verbose output for diagnostics --help Print usage ``` diff --git a/docs/reference/commandline/node_inspect.md b/docs/reference/commandline/node_inspect.md index 0ac4289909..7a492f5f05 100644 --- a/docs/reference/commandline/node_inspect.md +++ b/docs/reference/commandline/node_inspect.md @@ -15,7 +15,7 @@ Options: -f, --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. - Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates (default "json") + Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates --pretty Print the information in a human friendly format --help Print usage ``` diff --git a/docs/reference/commandline/plugin_inspect.md b/docs/reference/commandline/plugin_inspect.md index 2369dc1d1d..d84f7d68fb 100644 --- a/docs/reference/commandline/plugin_inspect.md +++ b/docs/reference/commandline/plugin_inspect.md @@ -15,7 +15,7 @@ Options: -f, --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. - Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates (default "json") + Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates --help Print usage ``` diff --git a/docs/reference/commandline/secret_inspect.md b/docs/reference/commandline/secret_inspect.md index ba5cc91211..67ce7a2763 100644 --- a/docs/reference/commandline/secret_inspect.md +++ b/docs/reference/commandline/secret_inspect.md @@ -15,7 +15,7 @@ Options: -f, --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. - Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates (default "json") + Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates --pretty Print the information in a human friendly format --help Print usage ``` diff --git a/docs/reference/commandline/service_inspect.md b/docs/reference/commandline/service_inspect.md index f284b7e94b..791244d939 100644 --- a/docs/reference/commandline/service_inspect.md +++ b/docs/reference/commandline/service_inspect.md @@ -15,7 +15,7 @@ Options: -f, --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. - Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates (default "json") + Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates --pretty Print the information in a human friendly format --help Print usage ``` diff --git a/docs/reference/commandline/volume_inspect.md b/docs/reference/commandline/volume_inspect.md index 9c2e3b75b1..86eab1e46c 100644 --- a/docs/reference/commandline/volume_inspect.md +++ b/docs/reference/commandline/volume_inspect.md @@ -15,7 +15,7 @@ Options: -f, --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. - Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates (default "json") + Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates --help Print usage ```