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-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-06 22:15:37 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2015-12-11 18:58:54 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2016-03-04 12:29:44 -05:00
|
|
|
"github.com/docker/docker/utils/templates"
|
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}}
|
2016-10-06 10:09:54 -04:00
|
|
|
OS/Arch: {{.Client.Os}}/{{.Client.Arch}}{{if .ServerOK}}
|
2015-06-25 21:25:41 -04:00
|
|
|
|
|
|
|
Server:
|
2016-11-02 13:04:39 -04:00
|
|
|
Version: {{.Server.Version}}
|
|
|
|
API version: {{.Server.APIVersion}}
|
|
|
|
Minimum API version: {{.Server.MinAPIVersion}}
|
|
|
|
Go version: {{.Server.GoVersion}}
|
|
|
|
Git commit: {{.Server.GitCommit}}
|
|
|
|
Built: {{.Server.BuildTime}}
|
|
|
|
OS/Arch: {{.Server.Os}}/{{.Server.Arch}}
|
|
|
|
Experimental: {{.Server.Experimental}}{{end}}`
|
2015-06-25 21:25:41 -04:00
|
|
|
|
2016-06-06 22:15:37 -04:00
|
|
|
type versionOptions struct {
|
|
|
|
format string
|
|
|
|
}
|
|
|
|
|
2016-07-16 05:55:18 -04:00
|
|
|
// NewVersionCommand creates a new cobra.Command for `docker version`
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewVersionCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-06 22:15:37 -04:00
|
|
|
var opts versionOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "version [OPTIONS]",
|
|
|
|
Short: "Show the Docker version information",
|
2016-07-29 13:41:52 -04:00
|
|
|
Args: cli.NoArgs,
|
2016-06-06 22:15:37 -04:00
|
|
|
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-06-06 22:15:37 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
|
2016-06-06 22:15:37 -04:00
|
|
|
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{
|
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,
|
2015-12-11 18:58:54 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|