From 50aa1e5b2819ea739beaa58bfe7836c2c8b4b9ff Mon Sep 17 00:00:00 2001 From: Daniel Zhang Date: Wed, 15 Feb 2017 08:21:40 +0800 Subject: [PATCH] Docker version output is not consistent when there are downgrades or incompatibilities. Signed-off-by: Daniel Zhang --- api/types/client.go | 12 --------- cli/command/system/version.go | 50 ++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/api/types/client.go b/api/types/client.go index 998cf68c5c..698c565991 100644 --- a/api/types/client.go +++ b/api/types/client.go @@ -256,18 +256,6 @@ type ResizeOptions struct { Width uint } -// VersionResponse holds version information for the client and the server -type VersionResponse struct { - Client *Version - Server *Version -} - -// ServerOK returns true when the client could connect to the docker server -// and parse the information received. It returns false otherwise. -func (v VersionResponse) ServerOK() bool { - return v.Server != nil -} - // NodeListOptions holds parameters to list nodes with. type NodeListOptions struct { Filters filters.Args diff --git a/cli/command/system/version.go b/cli/command/system/version.go index 569da21886..468db7d03a 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -1,7 +1,6 @@ package system import ( - "fmt" "runtime" "time" @@ -17,7 +16,7 @@ import ( var versionTemplate = `Client: Version: {{.Client.Version}} - API version: {{.Client.APIVersion}} + API version: {{.Client.APIVersion}}{{if ne .Client.APIVersion .Client.DefaultAPIVersion}} (downgraded from {{.Client.DefaultAPIVersion}}){{end}} Go version: {{.Client.GoVersion}} Git commit: {{.Client.GitCommit}} Built: {{.Client.BuildTime}} @@ -36,6 +35,29 @@ type versionOptions struct { format string } +// versionInfo contains version information of both the Client, and Server +type versionInfo struct { + Client clientVersion + Server *types.Version +} + +type clientVersion struct { + Version string + APIVersion string `json:"ApiVersion"` + DefaultAPIVersion string `json:"DefaultAPIVersion,omitempty"` + GitCommit string + GoVersion string + Os string + Arch string + BuildTime string `json:",omitempty"` +} + +// ServerOK returns true when the client could connect to the docker server +// and parse the information received. It returns false otherwise. +func (v versionInfo) ServerOK() bool { + return v.Server != nil +} + // NewVersionCommand creates a new cobra.Command for `docker version` func NewVersionCommand(dockerCli *command.DockerCli) *cobra.Command { var opts versionOptions @@ -70,20 +92,16 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error { Status: "Template parsing error: " + err.Error()} } - APIVersion := dockerCli.Client().ClientVersion() - if defaultAPIVersion := dockerCli.DefaultVersion(); APIVersion != defaultAPIVersion { - APIVersion = fmt.Sprintf("%s (downgraded from %s)", APIVersion, defaultAPIVersion) - } - - vd := types.VersionResponse{ - Client: &types.Version{ - Version: dockerversion.Version, - APIVersion: APIVersion, - GoVersion: runtime.Version(), - GitCommit: dockerversion.GitCommit, - BuildTime: dockerversion.BuildTime, - Os: runtime.GOOS, - Arch: runtime.GOARCH, + vd := versionInfo{ + Client: clientVersion{ + Version: dockerversion.Version, + APIVersion: dockerCli.Client().ClientVersion(), + DefaultAPIVersion: dockerCli.DefaultVersion(), + GoVersion: runtime.Version(), + GitCommit: dockerversion.GitCommit, + BuildTime: dockerversion.BuildTime, + Os: runtime.GOOS, + Arch: runtime.GOARCH, }, }