github.com/tompao/docker@v1.9.1/api/client/version.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"runtime"
     6  	"text/template"
     7  
     8  	"github.com/docker/docker/api"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/autogen/dockerversion"
    11  	Cli "github.com/docker/docker/cli"
    12  	flag "github.com/docker/docker/pkg/mflag"
    13  	"github.com/docker/docker/utils"
    14  )
    15  
    16  var versionTemplate = `Client:
    17   Version:      {{.Client.Version}}
    18   API version:  {{.Client.APIVersion}}
    19   Go version:   {{.Client.GoVersion}}
    20   Git commit:   {{.Client.GitCommit}}
    21   Built:        {{.Client.BuildTime}}
    22   OS/Arch:      {{.Client.Os}}/{{.Client.Arch}}{{if .Client.Experimental}}
    23   Experimental: {{.Client.Experimental}}{{end}}{{if .ServerOK}}
    24  
    25  Server:
    26   Version:      {{.Server.Version}}
    27   API version:  {{.Server.APIVersion}}
    28   Go version:   {{.Server.GoVersion}}
    29   Git commit:   {{.Server.GitCommit}}
    30   Built:        {{.Server.BuildTime}}
    31   OS/Arch:      {{.Server.Os}}/{{.Server.Arch}}{{if .Server.Experimental}}
    32   Experimental: {{.Server.Experimental}}{{end}}{{end}}`
    33  
    34  type versionData struct {
    35  	Client   types.Version
    36  	ServerOK bool
    37  	Server   types.Version
    38  }
    39  
    40  // CmdVersion shows Docker version information.
    41  //
    42  // 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.
    43  //
    44  // Usage: docker version
    45  func (cli *DockerCli) CmdVersion(args ...string) (err error) {
    46  	cmd := Cli.Subcmd("version", nil, Cli.DockerCommands["version"].Description, true)
    47  	tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
    48  	cmd.Require(flag.Exact, 0)
    49  
    50  	cmd.ParseFlags(args, true)
    51  	if *tmplStr == "" {
    52  		*tmplStr = versionTemplate
    53  	}
    54  
    55  	var tmpl *template.Template
    56  	if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
    57  		return Cli.StatusError{StatusCode: 64,
    58  			Status: "Template parsing error: " + err.Error()}
    59  	}
    60  
    61  	vd := versionData{
    62  		Client: types.Version{
    63  			Version:      dockerversion.VERSION,
    64  			APIVersion:   api.Version,
    65  			GoVersion:    runtime.Version(),
    66  			GitCommit:    dockerversion.GITCOMMIT,
    67  			BuildTime:    dockerversion.BUILDTIME,
    68  			Os:           runtime.GOOS,
    69  			Arch:         runtime.GOARCH,
    70  			Experimental: utils.ExperimentalBuild(),
    71  		},
    72  	}
    73  
    74  	defer func() {
    75  		if err2 := tmpl.Execute(cli.out, vd); err2 != nil && err == nil {
    76  			err = err2
    77  		}
    78  		cli.out.Write([]byte{'\n'})
    79  	}()
    80  
    81  	serverResp, err := cli.call("GET", "/version", nil, nil)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	defer serverResp.body.Close()
    87  
    88  	if err = json.NewDecoder(serverResp.body).Decode(&vd.Server); err != nil {
    89  		return Cli.StatusError{StatusCode: 1,
    90  			Status: "Error reading remote version: " + err.Error()}
    91  	}
    92  
    93  	vd.ServerOK = true
    94  
    95  	return
    96  }