Version relocation works

Signed-off-by: Lewis Cowles <lewiscowles@me.com>
This commit is contained in:
Lewis Cowles
2017-11-06 22:26:22 +00:00
committed by Alex Ellis
parent 96155667fa
commit 47abbdf3d7
6 changed files with 58 additions and 25 deletions

View File

@@ -16,7 +16,7 @@ RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))"
RUN license-check -path ./ --verbose=false \
&& VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') \
&& GIT_COMMIT=$(git rev-list -1 HEAD) \
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/version.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/version.Version=${VERSION}" -a -installsuffix cgo -o faas-cli
FROM alpine:latest
RUN apk --no-cache add ca-certificates

View File

@@ -16,11 +16,11 @@ RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))"
RUN license-check -path ./ --verbose=false \
&& VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') \
&& GIT_COMMIT=$(git rev-list -1 HEAD) \
&& GOARCH=arm GOARM=6 CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli-armhf \
&& CGO_ENABLED=0 GOOS=darwin go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli-darwin \
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli \
&& CGO_ENABLED=0 GOOS=windows go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli.exe \
&& CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli-arm64
&& GOARCH=arm GOARM=6 CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/version.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/version.Version=${VERSION}" -a -installsuffix cgo -o faas-cli-armhf \
&& CGO_ENABLED=0 GOOS=darwin go build --ldflags "-s -w -X github.com/openfaas/faas-cli/version.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/version.Version=${VERSION}" -a -installsuffix cgo -o faas-cli-darwin \
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/version.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/version.Version=${VERSION}" -a -installsuffix cgo -o faas-cli \
&& CGO_ENABLED=0 GOOS=windows go build --ldflags "-s -w -X github.com/openfaas/faas-cli/version.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/version.Version=${VERSION}" -a -installsuffix cgo -o faas-cli.exe \
&& CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build --ldflags "-s -w -X github.com/openfaas/faas-cli/version.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/version.Version=${VERSION}" -a -installsuffix cgo -o faas-cli-arm64
FROM alpine:latest
RUN apk --no-cache add ca-certificates

View File

@@ -8,12 +8,11 @@ import (
"runtime"
"github.com/morikuni/aec"
"github.com/openfaas/faas-cli/version"
"github.com/spf13/cobra"
)
// GitCommit injected at build-time
var GitCommit string
var Version string
var (
shortVersion bool
)
@@ -31,35 +30,31 @@ var versionCmd = &cobra.Command{
Long: fmt.Sprintf(`The version command returns the current clients version information.
This currently consists of the GitSHA from which the client was built.
- https://github.com/openfaas/faas-cli/tree/%s`, GitCommit),
- https://github.com/openfaas/faas-cli/tree/%s`, version.GitCommit),
Example: ` faas-cli version
faas-cli version --short-version`,
Run: runVersion,
}
func runVersion(cmd *cobra.Command, args []string) {
if len(Version) == 0 {
Version = "dev"
}
if shortVersion {
fmt.Println(Version)
fmt.Println(version.BuildVersion())
} else {
figletColoured := aec.BlueF.Apply(figletStr)
if runtime.GOOS == "windows" {
figletColoured = aec.GreenF.Apply(figletStr)
}
fmt.Printf(figletColoured)
fmt.Printf("Commit: %s\n", GitCommit)
fmt.Printf("Version: %s\n", Version)
fmt.Printf("Commit: %s\n", version.GitCommit)
fmt.Printf("Version: %s\n", version.BuildVersion())
}
}
const figletStr = ` ___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` + "`" + ` |/ _` + "`" + ` \___ \
const figletStr = ` ___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` + "`" + ` |/ _` + "`" + ` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|
`

View File

@@ -8,10 +8,11 @@ import (
"testing"
"github.com/openfaas/faas-cli/test"
"github.com/openfaas/faas-cli/version"
)
func Test_addVersionDev(t *testing.T) {
GitCommit = "sha-test"
version.GitCommit = "sha-test"
stdOut := test.CaptureStdout(func() {
faasCmd.SetArgs([]string{"version"})
@@ -28,8 +29,8 @@ func Test_addVersionDev(t *testing.T) {
}
func Test_addVersion(t *testing.T) {
GitCommit = "sha-test"
Version = "version.tag"
version.GitCommit = "sha-test"
version.Version = "version.tag"
stdOut := test.CaptureStdout(func() {
faasCmd.SetArgs([]string{"version"})
@@ -46,7 +47,7 @@ func Test_addVersion(t *testing.T) {
}
func Test_addVersion_short_version(t *testing.T) {
Version = "version.tag"
version.Version = "version.tag"
stdOut := test.CaptureStdout(func() {
faasCmd.SetArgs([]string{"version", "--short-version"})

14
version/version.go Normal file
View File

@@ -0,0 +1,14 @@
package version
const UserAgent = "OpenFaaS CLI"
var (
Version, GitCommit string
)
func BuildVersion() string {
if len(Version) == 0 {
return "dev"
}
return Version
}

23
version/version_test.go Normal file
View File

@@ -0,0 +1,23 @@
package version
import (
"testing"
)
func Test_EmptyVersionMeansBuildVersionReturnsDev(t *testing.T) {
Version = ""
output := BuildVersion()
expected := "dev"
if output != expected {
t.Fatalf("Version is not from Build - want: %s, got: %s\n", expected, output)
}
}
func Test_VersionReturnedFromBuildVersion(t *testing.T) {
Version = "testing-manual"
output := BuildVersion()
expected := Version
if output != expected {
t.Fatalf("Version is not from Build - want: %s, got: %s\n", expected, output)
}
}