2016-06-06 22:15:37 -04:00
|
|
|
package system
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
import (
|
2015-12-11 18:58:54 -05:00
|
|
|
"runtime"
|
2015-11-30 09:44:06 -05:00
|
|
|
"time"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-03-16 15:19:22 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-06-06 22:15:37 -04:00
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/cli"
|
2015-12-11 18:58:54 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
|
|
|
"github.com/docker/docker/utils"
|
2016-03-04 12:29:44 -05:00
|
|
|
"github.com/docker/docker/utils/templates"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2016-06-06 22:15:37 -04:00
|
|
|
"github.com/spf13/cobra"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-07-20 20:55:30 -04:00
|
|
|
var versionTemplate = `Client:
|
2015-06-25 21:25:41 -04:00
|
|
|
Version: {{.Client.Version}}
|
2015-07-23 05:40:54 -04:00
|
|
|
API version: {{.Client.APIVersion}}
|
2015-06-25 21:25:41 -04:00
|
|
|
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}}
|
2015-07-23 05:40:54 -04:00
|
|
|
API version: {{.Server.APIVersion}}
|
2015-06-25 21:25:41 -04:00
|
|
|
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}}`
|
|
|
|
|
2016-06-06 22:15:37 -04:00
|
|
|
type versionOptions struct {
|
|
|
|
format string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVersionCommand creats a new cobra.Command for `docker version`
|
|
|
|
func NewVersionCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
|
|
var opts versionOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "version [OPTIONS]",
|
|
|
|
Short: "Show the Docker version information",
|
|
|
|
Args: cli.ExactArgs(0),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runVersion(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-06-06 22:15:37 -04:00
|
|
|
func runVersion(dockerCli *client.DockerCli, opts *versionOptions) error {
|
|
|
|
ctx := context.Background()
|
2015-12-04 14:31:24 -05:00
|
|
|
|
|
|
|
templateFormat := versionTemplate
|
2016-06-06 22:15:37 -04:00
|
|
|
if opts.format != "" {
|
|
|
|
templateFormat = opts.format
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-06-25 21:25:41 -04:00
|
|
|
|
2016-06-06 22:15:37 -04:00
|
|
|
tmpl, err := templates.Parse(templateFormat)
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
2015-12-11 18:58:54 -05:00
|
|
|
vd := types.VersionResponse{
|
|
|
|
Client: &types.Version{
|
|
|
|
Version: dockerversion.Version,
|
2016-06-06 22:15:37 -04:00
|
|
|
APIVersion: dockerCli.Client().ClientVersion(),
|
2015-12-11 18:58:54 -05:00
|
|
|
GoVersion: runtime.Version(),
|
|
|
|
GitCommit: dockerversion.GitCommit,
|
|
|
|
BuildTime: dockerversion.BuildTime,
|
|
|
|
Os: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
Experimental: utils.ExperimentalBuild(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-06-06 22:15:37 -04:00
|
|
|
serverVersion, err := dockerCli.Client().ServerVersion(ctx)
|
2015-12-11 18:58:54 -05:00
|
|
|
if err == nil {
|
|
|
|
vd.Server = &serverVersion
|
|
|
|
}
|
2015-12-04 14:31:24 -05:00
|
|
|
|
|
|
|
// first we need to make BuildTime more human friendly
|
|
|
|
t, errTime := time.Parse(time.RFC3339Nano, vd.Client.BuildTime)
|
|
|
|
if errTime == nil {
|
|
|
|
vd.Client.BuildTime = t.Format(time.ANSIC)
|
2015-05-28 20:59:07 -04:00
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-04 14:31:24 -05:00
|
|
|
if vd.ServerOK() {
|
2015-11-30 09:44:06 -05:00
|
|
|
t, errTime = time.Parse(time.RFC3339Nano, vd.Server.BuildTime)
|
|
|
|
if errTime == nil {
|
|
|
|
vd.Server.BuildTime = t.Format(time.ANSIC)
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-06 22:15:37 -04:00
|
|
|
if err2 := tmpl.Execute(dockerCli.Out(), vd); err2 != nil && err == nil {
|
2015-12-04 14:31:24 -05:00
|
|
|
err = err2
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-06-06 22:15:37 -04:00
|
|
|
dockerCli.Out().Write([]byte{'\n'})
|
2015-12-04 14:31:24 -05:00
|
|
|
return err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|