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