From 46234b82e2dbfff4c1243e02608850eddefbb162 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 9 Apr 2023 13:11:19 +0200 Subject: [PATCH] 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 --- cli/command/system/info.go | 11 +++++++---- cli/command/system/info_test.go | 5 +++++ docs/reference/commandline/info.md | 6 +++--- docs/reference/commandline/system_info.md | 6 +++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index 4bd3e03565..d30f8b2dfb 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -12,7 +12,9 @@ import ( pluginmanager "github.com/docker/cli/cli-plugins/manager" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" + "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/debug" + flagsHelper "github.com/docker/cli/cli/flags" "github.com/docker/cli/templates" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" @@ -62,10 +64,7 @@ func NewInfoCommand(dockerCli command.Cli) *cobra.Command { ValidArgsFunction: completion.NoComplete, } - flags := cmd.Flags() - - flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template") - + cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) return cmd } @@ -507,6 +506,10 @@ func printServerWarningsLegacy(dockerCli command.Cli, info types.Info) { } func formatInfo(dockerCli command.Cli, info info, format string) error { + if format == formatter.JSONFormatKey { + format = formatter.JSONFormat + } + // Ensure slice/array fields render as `[]` not `null` if info.ClientInfo != nil && info.ClientInfo.Plugins == nil { info.ClientInfo.Plugins = make([]pluginmanager.Plugin, 0) diff --git a/cli/command/system/info_test.go b/cli/command/system/info_test.go index 199c93721d..5df964006f 100644 --- a/cli/command/system/info_test.go +++ b/cli/command/system/info_test.go @@ -396,6 +396,11 @@ func TestPrettyPrintInfo(t *testing.T) { assert.NilError(t, formatInfo(cli, tc.dockerInfo, "{{json .}}")) golden.Assert(t, cli.OutBuffer().String(), tc.jsonGolden+".json.golden") assert.Check(t, is.Equal("", cli.ErrBuffer().String())) + + cli = test.NewFakeCli(&fakeClient{}) + assert.NilError(t, formatInfo(cli, tc.dockerInfo, "json")) + golden.Assert(t, cli.OutBuffer().String(), tc.jsonGolden+".json.golden") + assert.Check(t, is.Equal("", cli.ErrBuffer().String())) } }) } diff --git a/docs/reference/commandline/info.md b/docs/reference/commandline/info.md index 41235357ee..8ba1b821b0 100644 --- a/docs/reference/commandline/info.md +++ b/docs/reference/commandline/info.md @@ -9,9 +9,9 @@ Display system-wide information ### Options -| Name | Type | Default | Description | -|:---------------------------------------|:---------|:--------|:----------------------------------------------| -| [`-f`](#format), [`--format`](#format) | `string` | | Format the output using the given Go template | +| Name | Type | Default | Description | +|:---------------------------------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| [`-f`](#format), [`--format`](#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 | diff --git a/docs/reference/commandline/system_info.md b/docs/reference/commandline/system_info.md index 8ef2093a07..e85ace3382 100644 --- a/docs/reference/commandline/system_info.md +++ b/docs/reference/commandline/system_info.md @@ -9,9 +9,9 @@ Display system-wide information ### Options -| Name | Type | Default | Description | -|:-----------------|:---------|:--------|:----------------------------------------------| -| `-f`, `--format` | `string` | | Format the output using the given Go template | +| Name | Type | Default | Description | +|:-----------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `-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 |