Skip to content

Commit 6233560

Browse files
authored
Merge pull request #4168 from thaJeztah/docker_info_json
fix docker info, docker version --format=json not outputting json format
2 parents fff5a31 + 23bd746 commit 6233560

9 files changed

Lines changed: 60 additions & 31 deletions

File tree

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
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Client":{"Platform":{"Name":""},"Version":"18.99.5-ce","ApiVersion":"1.38","DefaultAPIVersion":"1.38","GitCommit":"deadbeef","GoVersion":"go1.10.2","Os":"linux","Arch":"amd64","BuildTime":"Wed May 30 22:21:05 2018","Context":"my-context"},"Server":{"Platform":{"Name":"Docker Enterprise Edition (EE) 2.0"},"Components":[{"Name":"Engine","Version":"17.06.2-ee-15","Details":{"ApiVersion":"1.30","Arch":"amd64","BuildTime":"Mon Jul 9 23:38:38 2018","Experimental":"false","GitCommit":"64ddfa6","GoVersion":"go1.8.7","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"Universal Control Plane","Version":"17.06.2-ee-15","Details":{"ApiVersion":"1.30","Arch":"amd64","BuildTime":"Mon Jul 2 21:24:07 UTC 2018","GitCommit":"4513922","GoVersion":"go1.9.4","MinApiVersion":"1.20","Os":"linux","Version":"3.0.3-tp2"}},{"Name":"Kubernetes","Version":"1.8+","Details":{"buildDate":"2018-04-26T16:51:21Z","compiler":"gc","gitCommit":"8d637aedf46b9c21dde723e29c645b9f27106fa5","gitTreeState":"clean","gitVersion":"v1.8.11-docker-8d637ae","goVersion":"go1.8.3","major":"1","minor":"8+","platform":"linux/amd64"}},{"Name":"Calico","Version":"v3.0.8","Details":{"cni":"v2.0.6","kube-controllers":"v2.0.5","node":"v3.0.8"}}],"Version":"","ApiVersion":"","GitCommit":"","GoVersion":"","Os":"","Arch":""}}

cli/command/system/version.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"github.com/docker/cli/cli"
1212
"github.com/docker/cli/cli/command"
1313
"github.com/docker/cli/cli/command/completion"
14+
"github.com/docker/cli/cli/command/formatter"
1415
"github.com/docker/cli/cli/command/formatter/tabwriter"
16+
flagsHelper "github.com/docker/cli/cli/flags"
1517
"github.com/docker/cli/cli/version"
1618
"github.com/docker/cli/templates"
1719
"github.com/docker/docker/api/types"
@@ -20,7 +22,7 @@ import (
2022
"github.com/tonistiigi/go-rosetta"
2123
)
2224

23-
var versionTemplate = `{{with .Client -}}
25+
const defaultVersionTemplate = `{{with .Client -}}
2426
Client:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}}
2527
Version: {{.Version}}
2628
API version: {{.APIVersion}}{{if ne .APIVersion .DefaultAPIVersion}} (downgraded from {{.DefaultAPIVersion}}){{end}}
@@ -101,9 +103,7 @@ func NewVersionCommand(dockerCli command.Cli) *cobra.Command {
101103
ValidArgsFunction: completion.NoComplete,
102104
}
103105

104-
flags := cmd.Flags()
105-
flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
106-
106+
cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
107107
return cmd
108108
}
109109

@@ -194,8 +194,11 @@ func prettyPrintVersion(dockerCli command.Cli, vd versionInfo, tmpl *template.Te
194194
}
195195

196196
func newVersionTemplate(templateFormat string) (*template.Template, error) {
197-
if templateFormat == "" {
198-
templateFormat = versionTemplate
197+
switch templateFormat {
198+
case "":
199+
templateFormat = defaultVersionTemplate
200+
case formatter.JSONFormatKey:
201+
templateFormat = formatter.JSONFormat
199202
}
200203
tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder})
201204
tmpl, err := tmpl.Parse(templateFormat)

cli/command/system/version_test.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/docker/cli/internal/test"
10+
"github.com/docker/docker/api/types"
911
"gotest.tools/v3/assert"
1012
is "gotest.tools/v3/assert/cmp"
1113
"gotest.tools/v3/golden"
12-
13-
"github.com/docker/cli/internal/test"
14-
"github.com/docker/docker/api/types"
1514
)
1615

1716
func TestVersionWithoutServer(t *testing.T) {
@@ -30,7 +29,7 @@ func TestVersionWithoutServer(t *testing.T) {
3029
assert.Assert(t, !strings.Contains(out, "Server:"), "actual: %s", out)
3130
}
3231

33-
func TestVersionAlign(t *testing.T) {
32+
func TestVersionFormat(t *testing.T) {
3433
vi := versionInfo{
3534
Client: clientVersion{
3635
Version: "18.99.5-ce",
@@ -104,10 +103,28 @@ func TestVersionAlign(t *testing.T) {
104103
},
105104
})
106105

107-
cli := test.NewFakeCli(&fakeClient{})
108-
tmpl, err := newVersionTemplate("")
109-
assert.NilError(t, err)
110-
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
111-
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.golden"))
112-
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
106+
t.Run("default", func(t *testing.T) {
107+
cli := test.NewFakeCli(&fakeClient{})
108+
tmpl, err := newVersionTemplate("")
109+
assert.NilError(t, err)
110+
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
111+
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.golden"))
112+
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
113+
})
114+
t.Run("json", func(t *testing.T) {
115+
cli := test.NewFakeCli(&fakeClient{})
116+
tmpl, err := newVersionTemplate("json")
117+
assert.NilError(t, err)
118+
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
119+
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.json.golden"))
120+
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
121+
})
122+
t.Run("json template", func(t *testing.T) {
123+
cli := test.NewFakeCli(&fakeClient{})
124+
tmpl, err := newVersionTemplate("{{json .}}")
125+
assert.NilError(t, err)
126+
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
127+
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.json.golden"))
128+
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
129+
})
113130
}

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

docs/reference/commandline/version.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Show the Docker version information
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:---------------------------------------|:---------|:--------|:----------------------------------------------|
10-
| [`-f`](#format), [`--format`](#format) | `string` | | Format the output using the given Go template |
8+
| Name | Type | Default | Description |
9+
|:---------------------------------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
10+
| [`-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 |
1111

1212

1313
<!---MARKER_GEN_END-->

0 commit comments

Comments
 (0)