Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --format flag to docker version #14194

Merged
merged 1 commit into from Jul 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 59 additions & 36 deletions api/client/version.go
Expand Up @@ -2,8 +2,8 @@ package client

import (
"encoding/json"
"fmt"
"runtime"
"text/template"

"github.com/docker/docker/api"
"github.com/docker/docker/api/types"
Expand All @@ -12,61 +12,84 @@ import (
"github.com/docker/docker/utils"
)

var VersionTemplate = `Client:
Version: {{.Client.Version}}
API version: {{.Client.ApiVersion}}
Go version: {{.Client.GoVersion}}
Git commit: {{.Client.GitCommit}}
Built: {{.Client.BuildTime}}
OS/Arch: {{.Client.Os}}/{{.Client.Arch}}{{if .Client.Experimental}}
Experimental: {{.Client.Experimental}}{{end}}{{if .ServerOK}}

Server:
Version: {{.Server.Version}}
API version: {{.Server.ApiVersion}}
Go version: {{.Server.GoVersion}}
Git commit: {{.Server.GitCommit}}
Built: {{.Server.BuildTime}}
OS/Arch: {{.Server.Os}}/{{.Server.Arch}}{{if .Server.Experimental}}
Experimental: {{.Server.Experimental}}{{end}}{{end}}`

type VersionData struct {
Client types.Version
ServerOK bool
Server types.Version
}

// CmdVersion shows Docker version information.
//
// Available version information is shown for: client Docker version, client API version, client Go version, client Git commit, client OS/Arch, server Docker version, server API version, server Go version, server Git commit, and server OS/Arch.
//
// Usage: docker version
func (cli *DockerCli) CmdVersion(args ...string) error {
func (cli *DockerCli) CmdVersion(args ...string) (err error) {
cmd := cli.Subcmd("version", nil, "Show the Docker version information.", true)
tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phemmer i just noticed: you shouldn't use #format, that's for old flags, you are introducing a new one here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @phemmer could you take care of this, or want someone else to do so?

cmd.Require(flag.Exact, 0)

cmd.ParseFlags(args, true)

fmt.Println("Client:")
if dockerversion.VERSION != "" {
fmt.Fprintf(cli.out, " Version: %s\n", dockerversion.VERSION)
}
fmt.Fprintf(cli.out, " API version: %s\n", api.Version)
fmt.Fprintf(cli.out, " Go version: %s\n", runtime.Version())
if dockerversion.GITCOMMIT != "" {
fmt.Fprintf(cli.out, " Git commit: %s\n", dockerversion.GITCOMMIT)
if *tmplStr == "" {
*tmplStr = VersionTemplate
}
if dockerversion.BUILDTIME != "" {
fmt.Fprintf(cli.out, " Built: %s\n", dockerversion.BUILDTIME)

var tmpl *template.Template
if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
return StatusError{StatusCode: 64,
Status: "Template parsing error: " + err.Error()}
}
fmt.Fprintf(cli.out, " OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
if utils.ExperimentalBuild() {
fmt.Fprintf(cli.out, " Experimental: true\n")

vd := VersionData{
Client: types.Version{
Version: dockerversion.VERSION,
ApiVersion: api.Version,
GoVersion: runtime.Version(),
GitCommit: dockerversion.GITCOMMIT,
BuildTime: dockerversion.BUILDTIME,
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Experimental: utils.ExperimentalBuild(),
},
}

defer func() {
if err2 := tmpl.Execute(cli.out, vd); err2 != nil && err == nil {
err = err2
}
cli.out.Write([]byte{'\n'})
}()

serverResp, err := cli.call("GET", "/version", nil, nil)
if err != nil {
return err
}

defer serverResp.body.Close()

var v types.Version
if err := json.NewDecoder(serverResp.body).Decode(&v); err != nil {
fmt.Fprintf(cli.err, "Error reading remote version: %s\n", err)
return err
if err = json.NewDecoder(serverResp.body).Decode(&vd.Server); err != nil {
return StatusError{StatusCode: 1,
Status: "Error reading remote version: " + err.Error()}
}

fmt.Println("\nServer:")
fmt.Fprintf(cli.out, " Version: %s\n", v.Version)
if v.ApiVersion != "" {
fmt.Fprintf(cli.out, " API version: %s\n", v.ApiVersion)
}
fmt.Fprintf(cli.out, " Go version: %s\n", v.GoVersion)
fmt.Fprintf(cli.out, " Git commit: %s\n", v.GitCommit)
if len(v.BuildTime) > 0 {
fmt.Fprintf(cli.out, " Built: %s\n", v.BuildTime)
}
fmt.Fprintf(cli.out, " OS/Arch: %s/%s\n", v.Os, v.Arch)
if v.Experimental {
fmt.Fprintf(cli.out, " Experimental: true\n")
}
fmt.Fprintf(cli.out, "\n")
return nil
vd.ServerOK = true

return
}
26 changes: 23 additions & 3 deletions docs/reference/commandline/version.md
Expand Up @@ -15,8 +15,17 @@ weight=1

Show the Docker version information.

Show the Docker version, API version, Go version, Git commit, Build date/time,
and OS/architecture of both Docker client and daemon. Example use:
-f, --format="" Format the output using the given go template

By default, this will render all version information in an easy to read
layout. If a format is specified, the given template will be executed instead.

Go's [text/template](http://golang.org/pkg/text/template/) package
describes all the details of the format.

## Examples

**Default output:**

$ docker version
Client:
Expand All @@ -33,4 +42,15 @@ and OS/architecture of both Docker client and daemon. Example use:
Go version: go1.4.2
Git commit: f5bae0a
Built: Tue Jun 23 17:56:00 UTC 2015
OS/Arch: linux/amd64
OS/Arch: linux/amd64

**Get server version:**

$ docker version --format '{{.Server.Version}}'
1.8.0

**Dump raw data:**

$ docker version --format '{{json .}}'
{"Client":{"Version":"1.8.0","ApiVersion":"1.20","GitCommit":"f5bae0a","GoVersion":"go1.4.2","Os":"linux","Arch":"amd64","BuildTime":"Tue Jun 23 17:56:00 UTC 2015"},"ServerOK":true,"Server":{"Version":"1.8.0","ApiVersion":"1.20","GitCommit":"f5bae0a","GoVersion":"go1.4.2","Os":"linux","Arch":"amd64","KernelVersion":"3.13.2-gentoo","BuildTime":"Tue Jun 23 17:56:00 UTC 2015"}}

24 changes: 22 additions & 2 deletions man/docker-version.1.md
Expand Up @@ -6,19 +6,25 @@ docker-version - Show the Docker version information.

# SYNOPSIS
**docker version**
[**--help**]
[**-f**|**--format**[=*FORMAT*]]

# DESCRIPTION
This command displays version information for both the Docker client and
daemon.

# OPTIONS
There are no available options.
**--help**
Print usage statement

**-f**, **--format**=""
Format the output using the given go template.

# EXAMPLES

## Display Docker version information

Here is a sample output:
The default output:

$ docker version
Client:
Expand All @@ -36,7 +42,21 @@ Here is a sample output:
Git commit: f5bae0a
Built: Tue Jun 23 17:56:00 UTC 2015
OS/Arch: linux/amd64

Get server version:

$ docker version --format '{{.Server.Version}}'
1.8.0

Dump raw data:

To view all available fields, you can use the format `{{json .}}`.

$ docker version --format '{{json .}}'
{"Client":{"Version":"1.8.0","ApiVersion":"1.20","GitCommit":"f5bae0a","GoVersion":"go1.4.2","Os":"linux","Arch":"amd64","BuildTime":"Tue Jun 23 17:56:00 UTC 2015"},"ServerOK":true,"Server":{"Version":"1.8.0","ApiVersion":"1.20","GitCommit":"f5bae0a","GoVersion":"go1.4.2","Os":"linux","Arch":"amd64","KernelVersion":"3.13.2-gentoo","BuildTime":"Tue Jun 23 17:56:00 UTC 2015"}}


# HISTORY
June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
June 2015, updated by John Howard <jhoward@microsoft.com>
June 2015, updated by Patrick Hemmer <patrick.hemmer@gmail.com