Skip to content

Commit ed1c775

Browse files
committed
fix docker info --format=json not outputting json format
The --format=json option was added for all inspect commands, but was not implemented for "docker info". This patch implements the missing option. Before this patch: docker info --format=json json With this patch applied: docker info --format=json {"ID":"80c2f18a-2c88-4e4a-ba69-dca0eea59835","Containers":7,"ContainersRunning":"..."} Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent daf99dd commit ed1c775

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

cli/command/formatter/formatter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919
JSONFormatKey = "json"
2020

2121
DefaultQuietFormat = "{{.ID}}"
22-
jsonFormat = "{{json .}}"
22+
JSONFormat = "{{json .}}"
2323
)
2424

2525
// Format is the format string rendered using the Context
@@ -62,7 +62,7 @@ func (c *Context) preFormat() {
6262
case c.Format.IsTable():
6363
c.finalFormat = c.finalFormat[len(TableFormatKey):]
6464
case c.Format.IsJSON():
65-
c.finalFormat = jsonFormat
65+
c.finalFormat = JSONFormat
6666
}
6767

6868
c.finalFormat = strings.Trim(c.finalFormat, " ")

cli/command/system/info.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
pluginmanager "github.com/docker/cli/cli-plugins/manager"
1313
"github.com/docker/cli/cli/command"
1414
"github.com/docker/cli/cli/command/completion"
15+
"github.com/docker/cli/cli/command/formatter"
1516
"github.com/docker/cli/cli/debug"
17+
flagsHelper "github.com/docker/cli/cli/flags"
1618
"github.com/docker/cli/templates"
1719
"github.com/docker/docker/api/types"
1820
"github.com/docker/docker/api/types/swarm"
@@ -62,10 +64,7 @@ func NewInfoCommand(dockerCli command.Cli) *cobra.Command {
6264
ValidArgsFunction: completion.NoComplete,
6365
}
6466

65-
flags := cmd.Flags()
66-
67-
flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
68-
67+
cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
6968
return cmd
7069
}
7170

@@ -507,6 +506,10 @@ func printServerWarningsLegacy(dockerCli command.Cli, info types.Info) {
507506
}
508507

509508
func formatInfo(dockerCli command.Cli, info info, format string) error {
509+
if format == formatter.JSONFormatKey {
510+
format = formatter.JSONFormat
511+
}
512+
510513
// Ensure slice/array fields render as `[]` not `null`
511514
if info.ClientInfo != nil && info.ClientInfo.Plugins == nil {
512515
info.ClientInfo.Plugins = make([]pluginmanager.Plugin, 0)

cli/command/system/info_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ func TestPrettyPrintInfo(t *testing.T) {
396396
assert.NilError(t, formatInfo(cli, tc.dockerInfo, "{{json .}}"))
397397
golden.Assert(t, cli.OutBuffer().String(), tc.jsonGolden+".json.golden")
398398
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
399+
400+
cli = test.NewFakeCli(&fakeClient{})
401+
assert.NilError(t, formatInfo(cli, tc.dockerInfo, "json"))
402+
golden.Assert(t, cli.OutBuffer().String(), tc.jsonGolden+".json.golden")
403+
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
399404
}
400405
})
401406
}

docs/reference/commandline/info.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Display system-wide information
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:---------------------------------------|:---------|:--------|:----------------------------------------------|
14-
| [`-f`](#format), [`--format`](#format) | `string` | | Format the output using the given Go template |
12+
| Name | Type | Default | Description |
13+
|:---------------------------------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
14+
| [`-f`](#format), [`--format`](#format) | `string` | | Format output using a custom template:<br>'json': Print in JSON format<br>'TEMPLATE': Print output using the given Go template.<br>Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
1515

1616

1717
<!---MARKER_GEN_END-->

docs/reference/commandline/system_info.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Display system-wide information
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:-----------------|:---------|:--------|:----------------------------------------------|
14-
| `-f`, `--format` | `string` | | Format the output using the given Go template |
12+
| Name | Type | Default | Description |
13+
|:-----------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
14+
| `-f`, `--format` | `string` | | Format output using a custom template:<br>'json': Print in JSON format<br>'TEMPLATE': Print output using the given Go template.<br>Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
1515

1616

1717
<!---MARKER_GEN_END-->

0 commit comments

Comments
 (0)