Skip to content

Commit

Permalink
fix docker version --format=json not outputting json format
Browse files Browse the repository at this point in the history
The --format=json option was added for all inspect commands, but was not
implemented for "docker version". This patch implements the missing option.

Before this patch:

    docker version --format=json
    json

With this patch:

    docker version --format=json
    {"Client":{"Platform":{"Name":""},"Version":"24.0.0-dev","ApiVersion":"..."}}

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 23bd746)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Apr 26, 2023
1 parent 9dfe5a2 commit f914316
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +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":""}}
11 changes: 7 additions & 4 deletions cli/command/system/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/docker/cli/cli"
"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/command/formatter/tabwriter"
flagsHelper "github.com/docker/cli/cli/flags"
"github.com/docker/cli/cli/version"
"github.com/docker/cli/templates"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -101,9 +103,7 @@ func NewVersionCommand(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
}

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

func newVersionTemplate(templateFormat string) (*template.Template, error) {
if templateFormat == "" {
switch templateFormat {
case "":
templateFormat = defaultVersionTemplate
case formatter.JSONFormatKey:
templateFormat = formatter.JSONFormat
}
tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder})
tmpl, err := tmpl.Parse(templateFormat)
Expand Down
37 changes: 27 additions & 10 deletions cli/command/system/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"strings"
"testing"

"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/golden"

"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
)

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

func TestVersionAlign(t *testing.T) {
func TestVersionFormat(t *testing.T) {
vi := versionInfo{
Client: clientVersion{
Version: "18.99.5-ce",
Expand Down Expand Up @@ -104,10 +103,28 @@ func TestVersionAlign(t *testing.T) {
},
})

cli := test.NewFakeCli(&fakeClient{})
tmpl, err := newVersionTemplate("")
assert.NilError(t, err)
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.golden"))
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
t.Run("default", func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{})
tmpl, err := newVersionTemplate("")
assert.NilError(t, err)
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.golden"))
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
})
t.Run("json", func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{})
tmpl, err := newVersionTemplate("json")
assert.NilError(t, err)
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.json.golden"))
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
})
t.Run("json template", func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{})
tmpl, err := newVersionTemplate("{{json .}}")
assert.NilError(t, err)
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.json.golden"))
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
})
}
6 changes: 3 additions & 3 deletions docs/reference/commandline/version.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Show the Docker version 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:<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 |


<!---MARKER_GEN_END-->
Expand Down

0 comments on commit f914316

Please sign in to comment.