mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #14194 from phemmer/cli-version-format
add --format flag to `docker version`
This commit is contained in:
commit
e221291a25
3 changed files with 104 additions and 41 deletions
|
@ -2,8 +2,8 @@ package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/docker/docker/api"
|
"github.com/docker/docker/api"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
@ -12,34 +12,71 @@ import (
|
||||||
"github.com/docker/docker/utils"
|
"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.
|
// 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.
|
// 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
|
// 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)
|
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")
|
||||||
cmd.Require(flag.Exact, 0)
|
cmd.Require(flag.Exact, 0)
|
||||||
|
|
||||||
cmd.ParseFlags(args, true)
|
cmd.ParseFlags(args, true)
|
||||||
|
if *tmplStr == "" {
|
||||||
|
*tmplStr = VersionTemplate
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("Client:")
|
var tmpl *template.Template
|
||||||
if dockerversion.VERSION != "" {
|
if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
|
||||||
fmt.Fprintf(cli.out, " Version: %s\n", dockerversion.VERSION)
|
return StatusError{StatusCode: 64,
|
||||||
|
Status: "Template parsing error: " + err.Error()}
|
||||||
}
|
}
|
||||||
fmt.Fprintf(cli.out, " API version: %s\n", api.Version)
|
|
||||||
fmt.Fprintf(cli.out, " Go version: %s\n", runtime.Version())
|
vd := VersionData{
|
||||||
if dockerversion.GITCOMMIT != "" {
|
Client: types.Version{
|
||||||
fmt.Fprintf(cli.out, " Git commit: %s\n", dockerversion.GITCOMMIT)
|
Version: dockerversion.VERSION,
|
||||||
}
|
ApiVersion: api.Version,
|
||||||
if dockerversion.BUILDTIME != "" {
|
GoVersion: runtime.Version(),
|
||||||
fmt.Fprintf(cli.out, " Built: %s\n", dockerversion.BUILDTIME)
|
GitCommit: dockerversion.GITCOMMIT,
|
||||||
}
|
BuildTime: dockerversion.BUILDTIME,
|
||||||
fmt.Fprintf(cli.out, " OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
|
Os: runtime.GOOS,
|
||||||
if utils.ExperimentalBuild() {
|
Arch: runtime.GOARCH,
|
||||||
fmt.Fprintf(cli.out, " Experimental: true\n")
|
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)
|
serverResp, err := cli.call("GET", "/version", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -47,26 +84,12 @@ func (cli *DockerCli) CmdVersion(args ...string) error {
|
||||||
|
|
||||||
defer serverResp.body.Close()
|
defer serverResp.body.Close()
|
||||||
|
|
||||||
var v types.Version
|
if err = json.NewDecoder(serverResp.body).Decode(&vd.Server); err != nil {
|
||||||
if err := json.NewDecoder(serverResp.body).Decode(&v); err != nil {
|
return StatusError{StatusCode: 1,
|
||||||
fmt.Fprintf(cli.err, "Error reading remote version: %s\n", err)
|
Status: "Error reading remote version: " + err.Error()}
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("\nServer:")
|
vd.ServerOK = true
|
||||||
fmt.Fprintf(cli.out, " Version: %s\n", v.Version)
|
|
||||||
if v.ApiVersion != "" {
|
return
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,17 @@ weight=1
|
||||||
|
|
||||||
Show the Docker version information.
|
Show the Docker version information.
|
||||||
|
|
||||||
Show the Docker version, API version, Go version, Git commit, Build date/time,
|
-f, --format="" Format the output using the given go template
|
||||||
and OS/architecture of both Docker client and daemon. Example use:
|
|
||||||
|
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
|
$ docker version
|
||||||
Client:
|
Client:
|
||||||
|
@ -33,4 +42,15 @@ and OS/architecture of both Docker client and daemon. Example use:
|
||||||
Go version: go1.4.2
|
Go version: go1.4.2
|
||||||
Git commit: f5bae0a
|
Git commit: f5bae0a
|
||||||
Built: Tue Jun 23 17:56:00 UTC 2015
|
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"}}
|
||||||
|
|
||||||
|
|
|
@ -6,19 +6,25 @@ docker-version - Show the Docker version information.
|
||||||
|
|
||||||
# SYNOPSIS
|
# SYNOPSIS
|
||||||
**docker version**
|
**docker version**
|
||||||
|
[**--help**]
|
||||||
|
[**-f**|**--format**[=*FORMAT*]]
|
||||||
|
|
||||||
# DESCRIPTION
|
# DESCRIPTION
|
||||||
This command displays version information for both the Docker client and
|
This command displays version information for both the Docker client and
|
||||||
daemon.
|
daemon.
|
||||||
|
|
||||||
# OPTIONS
|
# OPTIONS
|
||||||
There are no available options.
|
**--help**
|
||||||
|
Print usage statement
|
||||||
|
|
||||||
|
**-f**, **--format**=""
|
||||||
|
Format the output using the given go template.
|
||||||
|
|
||||||
# EXAMPLES
|
# EXAMPLES
|
||||||
|
|
||||||
## Display Docker version information
|
## Display Docker version information
|
||||||
|
|
||||||
Here is a sample output:
|
The default output:
|
||||||
|
|
||||||
$ docker version
|
$ docker version
|
||||||
Client:
|
Client:
|
||||||
|
@ -36,7 +42,21 @@ Here is a sample output:
|
||||||
Git commit: f5bae0a
|
Git commit: f5bae0a
|
||||||
Built: Tue Jun 23 17:56:00 UTC 2015
|
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:
|
||||||
|
|
||||||
|
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
|
# HISTORY
|
||||||
June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
|
June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
|
||||||
June 2015, updated by John Howard <jhoward@microsoft.com>
|
June 2015, updated by John Howard <jhoward@microsoft.com>
|
||||||
|
June 2015, updated by Patrick Hemmer <patrick.hemmer@gmail.com
|
||||||
|
|
Loading…
Add table
Reference in a new issue