github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/version.go (about)

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