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 <djordje.lukic@docker.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Djordje Lukic 2022-03-08 14:54:21 +01:00 committed by Sebastiaan van Stijn
parent d8ecb00dda
commit 9c0234bbcb
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
22 changed files with 70 additions and 52 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {
// NewIndentedInspector generates a new inspector with an indented representation
// of elements.
func NewIndentedInspector(outputStream io.Writer) Inspector {
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, "", " ")
},
}
}
// 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)
}
// NewIndentedInspector generates a new IndentedInspector.
func NewIndentedInspector(outputStream io.Writer) Inspector {
return &IndentedInspector{
outputStream: outputStream,
}
}
// Inspect writes the raw element with an indented json format.
func (i *IndentedInspector) Inspect(typedElement interface{}, rawElement []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
}

View File

@ -277,11 +277,7 @@ func TestNewTemplateInspectorFromString(t *testing.T) {
{
name: "json specific value outputs json",
template: "json",
expected: `[
{
"Name": "test"
}
]
expected: `[{"Name":"test"}]
`,
},
{

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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")

View File

@ -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
}

View File

@ -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
```

View File

@ -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

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```