2016-09-08 13:11:39 -04:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/dockerversion"
|
|
|
|
"github.com/docker/docker/utils/templates"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var versionTemplate = `Client:
|
|
|
|
Version: {{.Client.Version}}
|
|
|
|
API version: {{.Client.APIVersion}}
|
|
|
|
Go version: {{.Client.GoVersion}}
|
|
|
|
Git commit: {{.Client.GitCommit}}
|
|
|
|
Built: {{.Client.BuildTime}}
|
2016-10-06 10:09:54 -04:00
|
|
|
OS/Arch: {{.Client.Os}}/{{.Client.Arch}}{{if .ServerOK}}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
Server:
|
|
|
|
Version: {{.Server.Version}}
|
|
|
|
API version: {{.Server.APIVersion}}
|
|
|
|
Go version: {{.Server.GoVersion}}
|
|
|
|
Git commit: {{.Server.GitCommit}}
|
|
|
|
Built: {{.Server.BuildTime}}
|
2016-10-06 10:09:54 -04:00
|
|
|
OS/Arch: {{.Server.Os}}/{{.Server.Arch}}
|
|
|
|
Experimental: {{.Server.Experimental}}{{end}}`
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
type versionOptions struct {
|
|
|
|
format string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVersionCommand creates a new cobra.Command for `docker version`
|
|
|
|
func NewVersionCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
var opts versionOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "version [OPTIONS]",
|
|
|
|
Short: "Show the Docker version information",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runVersion(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2016-10-18 06:50:11 -04:00
|
|
|
flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
templateFormat := versionTemplate
|
|
|
|
if opts.format != "" {
|
|
|
|
templateFormat = opts.format
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := templates.Parse(templateFormat)
|
|
|
|
if err != nil {
|
|
|
|
return cli.StatusError{StatusCode: 64,
|
|
|
|
Status: "Template parsing error: " + err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
vd := types.VersionResponse{
|
|
|
|
Client: &types.Version{
|
2016-10-06 10:09:54 -04:00
|
|
|
Version: dockerversion.Version,
|
|
|
|
APIVersion: dockerCli.Client().ClientVersion(),
|
|
|
|
GoVersion: runtime.Version(),
|
|
|
|
GitCommit: dockerversion.GitCommit,
|
|
|
|
BuildTime: dockerversion.BuildTime,
|
|
|
|
Os: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
serverVersion, err := dockerCli.Client().ServerVersion(ctx)
|
|
|
|
if err == nil {
|
|
|
|
vd.Server = &serverVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vd.ServerOK() {
|
|
|
|
t, errTime = time.Parse(time.RFC3339Nano, vd.Server.BuildTime)
|
|
|
|
if errTime == nil {
|
|
|
|
vd.Server.BuildTime = t.Format(time.ANSIC)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err2 := tmpl.Execute(dockerCli.Out(), vd); err2 != nil && err == nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
dockerCli.Out().Write([]byte{'\n'})
|
|
|
|
return err
|
|
|
|
}
|