Display proper version information

- The cli version defaults to "unknown-version" unless set via the VERSION env var
- The commit version can be overridden via GITCOMMIT env var
- The build time can be overridden via BUILDTIME env var

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
master
Kenfe-Mickael Laventure 2017-05-09 09:38:23 -07:00
parent 5cbd2b7d6c
commit cf51bde7d9
10 changed files with 40 additions and 105 deletions

View File

@ -1,12 +1,12 @@
#
# github.com/docker/cli
# github.com/docker/cli
#
.PHONY: build clean test lint cross
# build the CLI
build: clean
@go build -o ./build/docker github.com/docker/cli/cmd/docker
@./scripts/build/binary
# remove build artifacts
clean:
@ -23,9 +23,7 @@ lint:
# build the CLI for multiple architectures
cross: clean
@gox -output build/docker-{{.OS}}-{{.Arch}} \
-osarch="linux/arm linux/amd64 darwin/amd64 windows/amd64" \
github.com/docker/cli/cmd/docker
@./scripts/build/cross
vendor: vendor.conf
@vndr 2> /dev/null

View File

@ -7,6 +7,7 @@ import (
"os"
"runtime"
"github.com/docker/cli/cli"
cliconfig "github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/credentials"
@ -15,7 +16,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/docker/dockerversion"
dopts "github.com/docker/docker/opts"
"github.com/docker/go-connections/sockets"
"github.com/docker/go-connections/tlsconfig"
@ -300,5 +300,5 @@ func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, er
// UserAgent returns the user agent string used for making API requests
func UserAgent() string {
return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
return "Docker-Client/" + cli.Version + " (" + runtime.GOOS + ")"
}

View File

@ -9,7 +9,6 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/docker/api/types"
"github.com/docker/docker/dockerversion"
"github.com/docker/docker/pkg/templates"
"github.com/spf13/cobra"
)
@ -94,12 +93,12 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
vd := versionInfo{
Client: clientVersion{
Version: dockerversion.Version,
Version: cli.Version,
APIVersion: dockerCli.Client().ClientVersion(),
DefaultAPIVersion: dockerCli.DefaultVersion(),
GoVersion: runtime.Version(),
GitCommit: dockerversion.GitCommit,
BuildTime: dockerversion.BuildTime,
GitCommit: cli.GitCommit,
BuildTime: cli.BuildTime,
Os: runtime.GOOS,
Arch: runtime.GOARCH,
},

9
cli/version.go Normal file
View File

@ -0,0 +1,9 @@
package cli
// Default build-time variable.
// These values are overriding via ldflags
var (
Version = "unknown-version"
GitCommit = "unknown-commit"
BuildTime = "unknown-buildtime"
)

View File

@ -15,7 +15,6 @@ import (
cliflags "github.com/docker/cli/cli/flags"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/docker/dockerversion"
"github.com/docker/docker/pkg/term"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -184,7 +183,7 @@ func main() {
}
func showVersion() {
fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
fmt.Printf("Docker version %s, build %s\n", cli.Version, cli.GitCommit)
}
func dockerPreRun(opts *cliflags.ClientOptions) {

5
scripts/build/binary Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env sh
source ./scripts/build/ldflags
go build -o ./build/docker --ldflags "${LDFLAGS}" github.com/docker/cli/cmd/docker

8
scripts/build/cross Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env sh
source ./scripts/build/ldflags
gox -output build/docker-{{.OS}}-{{.Arch}} \
-osarch="linux/arm linux/amd64 darwin/amd64 windows/amd64" \
--ldflags "${LDFLAGS}" \
github.com/docker/cli/cmd/docker

9
scripts/build/ldflags Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
VERSION=${VERSION:-"unknown-version"}
GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)}
BUILDTIME=${BUILDTIME:-$(date --utc --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')}
export LDFLAGS="-X github.com/docker/cli/cli.GitCommit=${GITCOMMIT} \
-X github.com/docker/cli/cli.BuildTime=${BUILDTIME} \
-X github.com/docker/cli/cli.Version=${VERSION} ${LDFLAGS}"

View File

@ -1,76 +0,0 @@
package dockerversion
import (
"fmt"
"runtime"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/pkg/useragent"
"golang.org/x/net/context"
)
// UAStringKey is used as key type for user-agent string in net/context struct
const UAStringKey = "upstream-user-agent"
// DockerUserAgent is the User-Agent the Docker client uses to identify itself.
// In accordance with RFC 7231 (5.5.3) is of the form:
// [docker client's UA] UpstreamClient([upstream client's UA])
func DockerUserAgent(ctx context.Context) string {
httpVersion := make([]useragent.VersionInfo, 0, 6)
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "docker", Version: Version})
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "go", Version: runtime.Version()})
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "git-commit", Version: GitCommit})
if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "kernel", Version: kernelVersion.String()})
}
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "os", Version: runtime.GOOS})
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "arch", Version: runtime.GOARCH})
dockerUA := useragent.AppendVersions("", httpVersion...)
upstreamUA := getUserAgentFromContext(ctx)
if len(upstreamUA) > 0 {
ret := insertUpstreamUserAgent(upstreamUA, dockerUA)
return ret
}
return dockerUA
}
// getUserAgentFromContext returns the previously saved user-agent context stored in ctx, if one exists
func getUserAgentFromContext(ctx context.Context) string {
var upstreamUA string
if ctx != nil {
var ki interface{} = ctx.Value(UAStringKey)
if ki != nil {
upstreamUA = ctx.Value(UAStringKey).(string)
}
}
return upstreamUA
}
// escapeStr returns s with every rune in charsToEscape escaped by a backslash
func escapeStr(s string, charsToEscape string) string {
var ret string
for _, currRune := range s {
appended := false
for _, escapeableRune := range charsToEscape {
if currRune == escapeableRune {
ret += `\` + string(currRune)
appended = true
break
}
}
if !appended {
ret += string(currRune)
}
}
return ret
}
// insertUpstreamUserAgent adds the upstream client useragent to create a user-agent
// string of the form:
// $dockerUA UpstreamClient($upstreamUA)
func insertUpstreamUserAgent(upstreamUA string, dockerUA string) string {
charsToEscape := `();\`
upstreamUAEscaped := escapeStr(upstreamUA, charsToEscape)
return fmt.Sprintf("%s UpstreamClient(%s)", dockerUA, upstreamUAEscaped)
}

View File

@ -1,16 +0,0 @@
// +build !autogen
// Package dockerversion is auto-generated at build-time
package dockerversion
// Default build-time variable for library-import.
// This file is overridden on build with build-time informations.
const (
GitCommit string = "library-import"
Version string = "library-import"
BuildTime string = "library-import"
IAmStatic string = "library-import"
ContainerdCommitID string = "library-import"
RuncCommitID string = "library-import"
InitCommitID string = "library-import"
)