2015-03-24 23:57:23 -04:00
package client
import (
2015-04-27 17:11:29 -04:00
"encoding/json"
2015-03-24 23:57:23 -04:00
"runtime"
2015-06-25 21:25:41 -04:00
"text/template"
2015-03-24 23:57:23 -04:00
"github.com/docker/docker/api"
2015-04-27 17:11:29 -04:00
"github.com/docker/docker/api/types"
2015-03-24 23:57:23 -04:00
"github.com/docker/docker/autogen/dockerversion"
2015-05-05 00:18:28 -04:00
Cli "github.com/docker/docker/cli"
2015-03-24 23:57:23 -04:00
flag "github.com/docker/docker/pkg/mflag"
2015-05-28 20:59:07 -04:00
"github.com/docker/docker/utils"
2015-03-24 23:57:23 -04:00
)
2015-06-25 21:25:41 -04:00
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
}
2015-03-25 13:34:41 -04:00
// 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
2015-06-25 21:25:41 -04:00
func ( cli * DockerCli ) CmdVersion ( args ... string ) ( err error ) {
2015-05-05 00:18:28 -04:00
cmd := Cli . Subcmd ( "version" , nil , "Show the Docker version information." , true )
2015-06-25 21:25:41 -04:00
tmplStr := cmd . String ( [ ] string { "f" , "#format" , "-format" } , "" , "Format the output using the given go template" )
2015-03-24 23:57:23 -04:00
cmd . Require ( flag . Exact , 0 )
2015-05-23 11:57:50 -04:00
cmd . ParseFlags ( args , true )
2015-06-25 21:25:41 -04:00
if * tmplStr == "" {
* tmplStr = VersionTemplate
2015-03-24 23:57:23 -04:00
}
2015-06-25 21:25:41 -04:00
var tmpl * template . Template
if tmpl , err = template . New ( "" ) . Funcs ( funcMap ) . Parse ( * tmplStr ) ; err != nil {
2015-05-05 00:18:28 -04:00
return Cli . StatusError { StatusCode : 64 ,
2015-06-25 21:25:41 -04:00
Status : "Template parsing error: " + err . Error ( ) }
2015-06-19 13:03:13 -04:00
}
2015-06-25 21:25:41 -04:00
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 ( ) ,
} ,
2015-05-28 20:59:07 -04:00
}
2015-03-24 23:57:23 -04:00
2015-06-25 21:25:41 -04:00
defer func ( ) {
if err2 := tmpl . Execute ( cli . out , vd ) ; err2 != nil && err == nil {
err = err2
}
cli . out . Write ( [ ] byte { '\n' } )
} ( )
2015-07-09 22:05:50 -04:00
serverResp , err := cli . call ( "GET" , "/version" , nil , nil )
2015-03-24 23:57:23 -04:00
if err != nil {
return err
}
2015-07-09 22:05:50 -04:00
defer serverResp . body . Close ( )
2015-07-03 02:19:23 -04:00
2015-06-25 21:25:41 -04:00
if err = json . NewDecoder ( serverResp . body ) . Decode ( & vd . Server ) ; err != nil {
2015-05-05 00:18:28 -04:00
return Cli . StatusError { StatusCode : 1 ,
2015-06-25 21:25:41 -04:00
Status : "Error reading remote version: " + err . Error ( ) }
2015-03-24 23:57:23 -04:00
}
2015-04-27 17:11:29 -04:00
2015-06-25 21:25:41 -04:00
vd . ServerOK = true
return
2015-03-24 23:57:23 -04:00
}