github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/cli/command/system/version.go (about) 1 package system 2 3 import ( 4 "runtime" 5 "time" 6 7 "golang.org/x/net/context" 8 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/cli" 11 "github.com/docker/docker/cli/command" 12 "github.com/docker/docker/dockerversion" 13 "github.com/docker/docker/utils/templates" 14 "github.com/spf13/cobra" 15 ) 16 17 var versionTemplate = `Client: 18 Version: {{.Client.Version}} 19 API version: {{.Client.APIVersion}} 20 Go version: {{.Client.GoVersion}} 21 Git commit: {{.Client.GitCommit}} 22 Built: {{.Client.BuildTime}} 23 OS/Arch: {{.Client.Os}}/{{.Client.Arch}}{{if .ServerOK}} 24 25 Server: 26 Version: {{.Server.Version}} 27 API version: {{.Server.APIVersion}} 28 Go version: {{.Server.GoVersion}} 29 Git commit: {{.Server.GitCommit}} 30 Built: {{.Server.BuildTime}} 31 OS/Arch: {{.Server.Os}}/{{.Server.Arch}} 32 Experimental: {{.Server.Experimental}}{{end}}` 33 34 type versionOptions struct { 35 format string 36 } 37 38 // NewVersionCommand creates a new cobra.Command for `docker version` 39 func NewVersionCommand(dockerCli *command.DockerCli) *cobra.Command { 40 var opts versionOptions 41 42 cmd := &cobra.Command{ 43 Use: "version [OPTIONS]", 44 Short: "Show the Docker version information", 45 Args: cli.NoArgs, 46 RunE: func(cmd *cobra.Command, args []string) error { 47 return runVersion(dockerCli, &opts) 48 }, 49 } 50 51 flags := cmd.Flags() 52 53 flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template") 54 55 return cmd 56 } 57 58 func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error { 59 ctx := context.Background() 60 61 templateFormat := versionTemplate 62 if opts.format != "" { 63 templateFormat = opts.format 64 } 65 66 tmpl, err := templates.Parse(templateFormat) 67 if err != nil { 68 return cli.StatusError{StatusCode: 64, 69 Status: "Template parsing error: " + err.Error()} 70 } 71 72 vd := types.VersionResponse{ 73 Client: &types.Version{ 74 Version: dockerversion.Version, 75 APIVersion: dockerCli.Client().ClientVersion(), 76 GoVersion: runtime.Version(), 77 GitCommit: dockerversion.GitCommit, 78 BuildTime: dockerversion.BuildTime, 79 Os: runtime.GOOS, 80 Arch: runtime.GOARCH, 81 }, 82 } 83 84 serverVersion, err := dockerCli.Client().ServerVersion(ctx) 85 if err == nil { 86 vd.Server = &serverVersion 87 } 88 89 // first we need to make BuildTime more human friendly 90 t, errTime := time.Parse(time.RFC3339Nano, vd.Client.BuildTime) 91 if errTime == nil { 92 vd.Client.BuildTime = t.Format(time.ANSIC) 93 } 94 95 if vd.ServerOK() { 96 t, errTime = time.Parse(time.RFC3339Nano, vd.Server.BuildTime) 97 if errTime == nil { 98 vd.Server.BuildTime = t.Format(time.ANSIC) 99 } 100 } 101 102 if err2 := tmpl.Execute(dockerCli.Out(), vd); err2 != nil && err == nil { 103 err = err2 104 } 105 dockerCli.Out().Write([]byte{'\n'}) 106 return err 107 }