Bläddra i källkod

add --format flag to `docker version`

Signed-off-by: Patrick Hemmer <patrick.hemmer@gmail.com>
Patrick Hemmer 10 år sedan
förälder
incheckning
41588a5766
3 ändrade filer med 104 tillägg och 41 borttagningar
  1. 59 36
      api/client/version.go
  2. 23 3
      docs/reference/commandline/version.md
  3. 22 2
      man/docker-version.1.md

+ 59 - 36
api/client/version.go

@@ -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)
-
-	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)
 	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(&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
 }
 }

+ 23 - 3
docs/reference/commandline/version.md

@@ -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,
-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
     $ 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"}}
+

+ 22 - 2
man/docker-version.1.md

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