Browse code

Merge pull request #23333 from yongtang/23211-spf13-cobra-version

Use spf13/cobra for docker version

Vincent Demeester authored on 2016/06/08 06:46:01
Showing 5 changed files
... ...
@@ -19,6 +19,5 @@ func (cli *DockerCli) Command(name string) func(...string) error {
19 19
 		"save":    cli.CmdSave,
20 20
 		"stats":   cli.CmdStats,
21 21
 		"update":  cli.CmdUpdate,
22
-		"version": cli.CmdVersion,
23 22
 	}[name]
24 23
 }
25 24
new file mode 100644
... ...
@@ -0,0 +1,110 @@
0
+package system
1
+
2
+import (
3
+	"runtime"
4
+	"time"
5
+
6
+	"golang.org/x/net/context"
7
+
8
+	"github.com/docker/docker/api/client"
9
+	"github.com/docker/docker/cli"
10
+	"github.com/docker/docker/dockerversion"
11
+	"github.com/docker/docker/utils"
12
+	"github.com/docker/docker/utils/templates"
13
+	"github.com/docker/engine-api/types"
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 .Client.Experimental}}
24
+ Experimental: {{.Client.Experimental}}{{end}}{{if .ServerOK}}
25
+
26
+Server:
27
+ Version:      {{.Server.Version}}
28
+ API version:  {{.Server.APIVersion}}
29
+ Go version:   {{.Server.GoVersion}}
30
+ Git commit:   {{.Server.GitCommit}}
31
+ Built:        {{.Server.BuildTime}}
32
+ OS/Arch:      {{.Server.Os}}/{{.Server.Arch}}{{if .Server.Experimental}}
33
+ Experimental: {{.Server.Experimental}}{{end}}{{end}}`
34
+
35
+type versionOptions struct {
36
+	format string
37
+}
38
+
39
+// NewVersionCommand creats a new cobra.Command for `docker version`
40
+func NewVersionCommand(dockerCli *client.DockerCli) *cobra.Command {
41
+	var opts versionOptions
42
+
43
+	cmd := &cobra.Command{
44
+		Use:   "version [OPTIONS]",
45
+		Short: "Show the Docker version information",
46
+		Args:  cli.ExactArgs(0),
47
+		RunE: func(cmd *cobra.Command, args []string) error {
48
+			return runVersion(dockerCli, &opts)
49
+		},
50
+	}
51
+
52
+	flags := cmd.Flags()
53
+
54
+	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
55
+
56
+	return cmd
57
+}
58
+
59
+func runVersion(dockerCli *client.DockerCli, opts *versionOptions) error {
60
+	ctx := context.Background()
61
+
62
+	templateFormat := versionTemplate
63
+	if opts.format != "" {
64
+		templateFormat = opts.format
65
+	}
66
+
67
+	tmpl, err := templates.Parse(templateFormat)
68
+	if err != nil {
69
+		return cli.StatusError{StatusCode: 64,
70
+			Status: "Template parsing error: " + err.Error()}
71
+	}
72
+
73
+	vd := types.VersionResponse{
74
+		Client: &types.Version{
75
+			Version:      dockerversion.Version,
76
+			APIVersion:   dockerCli.Client().ClientVersion(),
77
+			GoVersion:    runtime.Version(),
78
+			GitCommit:    dockerversion.GitCommit,
79
+			BuildTime:    dockerversion.BuildTime,
80
+			Os:           runtime.GOOS,
81
+			Arch:         runtime.GOARCH,
82
+			Experimental: utils.ExperimentalBuild(),
83
+		},
84
+	}
85
+
86
+	serverVersion, err := dockerCli.Client().ServerVersion(ctx)
87
+	if err == nil {
88
+		vd.Server = &serverVersion
89
+	}
90
+
91
+	// first we need to make BuildTime more human friendly
92
+	t, errTime := time.Parse(time.RFC3339Nano, vd.Client.BuildTime)
93
+	if errTime == nil {
94
+		vd.Client.BuildTime = t.Format(time.ANSIC)
95
+	}
96
+
97
+	if vd.ServerOK() {
98
+		t, errTime = time.Parse(time.RFC3339Nano, vd.Server.BuildTime)
99
+		if errTime == nil {
100
+			vd.Server.BuildTime = t.Format(time.ANSIC)
101
+		}
102
+	}
103
+
104
+	if err2 := tmpl.Execute(dockerCli.Out(), vd); err2 != nil && err == nil {
105
+		err = err2
106
+	}
107
+	dockerCli.Out().Write([]byte{'\n'})
108
+	return err
109
+}
0 110
deleted file mode 100644
... ...
@@ -1,95 +0,0 @@
1
-package client
2
-
3
-import (
4
-	"runtime"
5
-	"text/template"
6
-	"time"
7
-
8
-	"golang.org/x/net/context"
9
-
10
-	Cli "github.com/docker/docker/cli"
11
-	"github.com/docker/docker/dockerversion"
12
-	flag "github.com/docker/docker/pkg/mflag"
13
-	"github.com/docker/docker/utils"
14
-	"github.com/docker/docker/utils/templates"
15
-	"github.com/docker/engine-api/types"
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
-// CmdVersion shows Docker version information.
37
-//
38
-// Available version information is shown for: client Docker version, client API version, client Go version, client Git commit, client OS/Arch, server Docker version, server API version, server Go version, server Git commit, and server OS/Arch.
39
-//
40
-// Usage: docker version
41
-func (cli *DockerCli) CmdVersion(args ...string) (err error) {
42
-	cmd := Cli.Subcmd("version", nil, Cli.DockerCommands["version"].Description, true)
43
-	tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
44
-	cmd.Require(flag.Exact, 0)
45
-
46
-	cmd.ParseFlags(args, true)
47
-
48
-	templateFormat := versionTemplate
49
-	if *tmplStr != "" {
50
-		templateFormat = *tmplStr
51
-	}
52
-
53
-	var tmpl *template.Template
54
-	if tmpl, err = templates.Parse(templateFormat); err != nil {
55
-		return Cli.StatusError{StatusCode: 64,
56
-			Status: "Template parsing error: " + err.Error()}
57
-	}
58
-
59
-	vd := types.VersionResponse{
60
-		Client: &types.Version{
61
-			Version:      dockerversion.Version,
62
-			APIVersion:   cli.client.ClientVersion(),
63
-			GoVersion:    runtime.Version(),
64
-			GitCommit:    dockerversion.GitCommit,
65
-			BuildTime:    dockerversion.BuildTime,
66
-			Os:           runtime.GOOS,
67
-			Arch:         runtime.GOARCH,
68
-			Experimental: utils.ExperimentalBuild(),
69
-		},
70
-	}
71
-
72
-	serverVersion, err := cli.client.ServerVersion(context.Background())
73
-	if err == nil {
74
-		vd.Server = &serverVersion
75
-	}
76
-
77
-	// first we need to make BuildTime more human friendly
78
-	t, errTime := time.Parse(time.RFC3339Nano, vd.Client.BuildTime)
79
-	if errTime == nil {
80
-		vd.Client.BuildTime = t.Format(time.ANSIC)
81
-	}
82
-
83
-	if vd.ServerOK() {
84
-		t, errTime = time.Parse(time.RFC3339Nano, vd.Server.BuildTime)
85
-		if errTime == nil {
86
-			vd.Server.BuildTime = t.Format(time.ANSIC)
87
-		}
88
-	}
89
-
90
-	if err2 := tmpl.Execute(cli.out, vd); err2 != nil && err == nil {
91
-		err = err2
92
-	}
93
-	cli.out.Write([]byte{'\n'})
94
-	return err
95
-}
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"github.com/docker/docker/api/client/container"
6 6
 	"github.com/docker/docker/api/client/image"
7 7
 	"github.com/docker/docker/api/client/network"
8
+	"github.com/docker/docker/api/client/system"
8 9
 	"github.com/docker/docker/api/client/volume"
9 10
 	"github.com/docker/docker/cli"
10 11
 	cliflags "github.com/docker/docker/cli/flags"
... ...
@@ -58,6 +59,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
58 58
 		image.NewImportCommand(dockerCli),
59 59
 		image.NewTagCommand(dockerCli),
60 60
 		network.NewNetworkCommand(dockerCli),
61
+		system.NewVersionCommand(dockerCli),
61 62
 		volume.NewVolumeCommand(dockerCli),
62 63
 	)
63 64
 
... ...
@@ -24,7 +24,6 @@ var DockerCommandUsage = []Command{
24 24
 	{"save", "Save one or more images to a tar archive"},
25 25
 	{"stats", "Display a live stream of container(s) resource usage statistics"},
26 26
 	{"update", "Update configuration of one or more containers"},
27
-	{"version", "Show the Docker version information"},
28 27
 }
29 28
 
30 29
 // DockerCommands stores all the docker command