mirror of https://github.com/docker/cli.git
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:
parent
d8ecb00dda
commit
9c0234bbcb
|
@ -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")
|
cmd.Flags().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
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")
|
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
|
|
@ -35,7 +35,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
|
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
|
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,9 @@ import (
|
||||||
|
|
||||||
// Inspector defines an interface to implement to process elements
|
// Inspector defines an interface to implement to process elements
|
||||||
type Inspector interface {
|
type Inspector interface {
|
||||||
|
// Inspect writes the raw element in JSON format.
|
||||||
Inspect(typedElement interface{}, rawElement []byte) error
|
Inspect(typedElement interface{}, rawElement []byte) error
|
||||||
|
// Flush writes the result of inspecting all elements into the output stream.
|
||||||
Flush() error
|
Flush() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,10 +40,14 @@ func NewTemplateInspector(outputStream io.Writer, tmpl *template.Template) Inspe
|
||||||
// NewTemplateInspectorFromString creates a new TemplateInspector from a string
|
// NewTemplateInspectorFromString creates a new TemplateInspector from a string
|
||||||
// which is compiled into a template.
|
// which is compiled into a template.
|
||||||
func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, error) {
|
func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, error) {
|
||||||
if tmplStr == "" || tmplStr == "json" {
|
if tmplStr == "" {
|
||||||
return NewIndentedInspector(out), nil
|
return NewIndentedInspector(out), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tmplStr == "json" {
|
||||||
|
return NewJSONInspector(out), nil
|
||||||
|
}
|
||||||
|
|
||||||
tmpl, err := templates.Parse(tmplStr)
|
tmpl, err := templates.Parse(tmplStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Errorf("Template parsing error: %s", err)
|
return nil, errors.Errorf("Template parsing error: %s", err)
|
||||||
|
@ -136,64 +142,80 @@ func (i *TemplateInspector) Flush() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndentedInspector uses a buffer to stop the indented representation of an element.
|
// NewIndentedInspector generates a new inspector with an indented representation
|
||||||
type IndentedInspector struct {
|
// of elements.
|
||||||
outputStream io.Writer
|
|
||||||
elements []interface{}
|
|
||||||
rawElements [][]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewIndentedInspector generates a new IndentedInspector.
|
|
||||||
func NewIndentedInspector(outputStream io.Writer) Inspector {
|
func NewIndentedInspector(outputStream io.Writer) Inspector {
|
||||||
return &IndentedInspector{
|
return &elementsInspector{
|
||||||
outputStream: outputStream,
|
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.
|
// NewJSONInspector generates a new inspector with a compact representation
|
||||||
func (i *IndentedInspector) Inspect(typedElement interface{}, rawElement []byte) error {
|
// 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 {
|
if rawElement != nil {
|
||||||
i.rawElements = append(i.rawElements, rawElement)
|
e.rawElements = append(e.rawElements, rawElement)
|
||||||
} else {
|
} else {
|
||||||
i.elements = append(i.elements, typedElement)
|
e.elements = append(e.elements, typedElement)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush writes the result of inspecting all elements into the output stream.
|
func (e *elementsInspector) Flush() error {
|
||||||
func (i *IndentedInspector) Flush() error {
|
if len(e.elements) == 0 && len(e.rawElements) == 0 {
|
||||||
if len(i.elements) == 0 && len(i.rawElements) == 0 {
|
_, err := io.WriteString(e.outputStream, "[]\n")
|
||||||
_, err := io.WriteString(i.outputStream, "[]\n")
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer io.Reader
|
var buffer io.Reader
|
||||||
if len(i.rawElements) > 0 {
|
if len(e.rawElements) > 0 {
|
||||||
bytesBuffer := new(bytes.Buffer)
|
bytesBuffer := new(bytes.Buffer)
|
||||||
bytesBuffer.WriteString("[")
|
bytesBuffer.WriteString("[")
|
||||||
for idx, r := range i.rawElements {
|
for idx, r := range e.rawElements {
|
||||||
bytesBuffer.Write(r)
|
bytesBuffer.Write(r)
|
||||||
if idx < len(i.rawElements)-1 {
|
if idx < len(e.rawElements)-1 {
|
||||||
bytesBuffer.WriteString(",")
|
bytesBuffer.WriteString(",")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bytesBuffer.WriteString("]")
|
bytesBuffer.WriteString("]")
|
||||||
indented := new(bytes.Buffer)
|
output := new(bytes.Buffer)
|
||||||
if err := json.Indent(indented, bytesBuffer.Bytes(), "", " "); err != nil {
|
if err := e.raw(output, bytesBuffer.Bytes()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buffer = indented
|
buffer = output
|
||||||
} else {
|
} else {
|
||||||
b, err := json.MarshalIndent(i.elements, "", " ")
|
b, err := e.el(e.elements)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buffer = bytes.NewReader(b)
|
buffer = bytes.NewReader(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := io.Copy(i.outputStream, buffer); err != nil {
|
if _, err := io.Copy(e.outputStream, buffer); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err := io.WriteString(i.outputStream, "\n")
|
_, err := io.WriteString(e.outputStream, "\n")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -277,11 +277,7 @@ func TestNewTemplateInspectorFromString(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "json specific value outputs json",
|
name: "json specific value outputs json",
|
||||||
template: "json",
|
template: "json",
|
||||||
expected: `[
|
expected: `[{"Name":"test"}]
|
||||||
{
|
|
||||||
"Name": "test"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -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")
|
cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", false, "Verbose output for diagnostics")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
|
|
@ -32,7 +32,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
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")
|
flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.StringVarP(&opts.format, "format", "f", "json", flagsHelper.InspectFormatHelp)
|
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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")
|
cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
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")
|
flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
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.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")
|
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container")
|
||||||
|
|
||||||
|
|
|
@ -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
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ Options:
|
||||||
-f, --format string Format output using a custom template:
|
-f, --format string Format output using a custom template:
|
||||||
'json': Print in JSON format
|
'json': Print in JSON format
|
||||||
'TEMPLATE': Print output using the given Go template.
|
'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
|
--pretty Print the information in a human friendly format
|
||||||
--help Print usage
|
--help Print usage
|
||||||
```
|
```
|
||||||
|
|
|
@ -15,7 +15,7 @@ Options:
|
||||||
-f, --format string Format output using a custom template:
|
-f, --format string Format output using a custom template:
|
||||||
'json': Print in JSON format
|
'json': Print in JSON format
|
||||||
'TEMPLATE': Print output using the given Go template.
|
'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
|
## Description
|
||||||
|
|
|
@ -12,7 +12,7 @@ Usage: docker info [OPTIONS]
|
||||||
Display system-wide information
|
Display system-wide information
|
||||||
|
|
||||||
Options:
|
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
|
--help Print usage
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ Options:
|
||||||
-f, --format string Format output using a custom template:
|
-f, --format string Format output using a custom template:
|
||||||
'json': Print in JSON format
|
'json': Print in JSON format
|
||||||
'TEMPLATE': Print output using the given Go template.
|
'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
|
-v, --verbose Verbose output for diagnostics
|
||||||
--help Print usage
|
--help Print usage
|
||||||
```
|
```
|
||||||
|
|
|
@ -15,7 +15,7 @@ Options:
|
||||||
-f, --format string Format output using a custom template:
|
-f, --format string Format output using a custom template:
|
||||||
'json': Print in JSON format
|
'json': Print in JSON format
|
||||||
'TEMPLATE': Print output using the given Go template.
|
'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
|
--pretty Print the information in a human friendly format
|
||||||
--help Print usage
|
--help Print usage
|
||||||
```
|
```
|
||||||
|
|
|
@ -15,7 +15,7 @@ Options:
|
||||||
-f, --format string Format output using a custom template:
|
-f, --format string Format output using a custom template:
|
||||||
'json': Print in JSON format
|
'json': Print in JSON format
|
||||||
'TEMPLATE': Print output using the given Go template.
|
'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
|
--help Print usage
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ Options:
|
||||||
-f, --format string Format output using a custom template:
|
-f, --format string Format output using a custom template:
|
||||||
'json': Print in JSON format
|
'json': Print in JSON format
|
||||||
'TEMPLATE': Print output using the given Go template.
|
'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
|
--pretty Print the information in a human friendly format
|
||||||
--help Print usage
|
--help Print usage
|
||||||
```
|
```
|
||||||
|
|
|
@ -15,7 +15,7 @@ Options:
|
||||||
-f, --format string Format output using a custom template:
|
-f, --format string Format output using a custom template:
|
||||||
'json': Print in JSON format
|
'json': Print in JSON format
|
||||||
'TEMPLATE': Print output using the given Go template.
|
'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
|
--pretty Print the information in a human friendly format
|
||||||
--help Print usage
|
--help Print usage
|
||||||
```
|
```
|
||||||
|
|
|
@ -15,7 +15,7 @@ Options:
|
||||||
-f, --format string Format output using a custom template:
|
-f, --format string Format output using a custom template:
|
||||||
'json': Print in JSON format
|
'json': Print in JSON format
|
||||||
'TEMPLATE': Print output using the given Go template.
|
'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
|
--help Print usage
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue