Skip to content
This repository was archived by the owner on Nov 5, 2021. It is now read-only.

Commit 86a49b1

Browse files
imumesh18alexellis
authored andcommitted
Added jaas version support
Signed-off-by: dungeonmaster18 <[email protected]>
1 parent 7ea9613 commit 86a49b1

File tree

18 files changed

+1246
-6
lines changed

18 files changed

+1246
-6
lines changed

Dockerfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ MAINTAINER [email protected]
55
RUN mkdir -p /go/src/github.com/alexellis/jaas
66
WORKDIR /go/src/github.com/alexellis/jaas
77

8-
COPY vendor vendor
9-
COPY cmd cmd
10-
COPY main.go .
8+
COPY . .
119

12-
RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-s -w" -installsuffix cgo -o /root/jaas
10+
RUN VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') \
11+
&& GIT_COMMIT=$(git rev-list -1 HEAD) \
12+
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/alexellis/jaas/version.GitCommit=${GIT_COMMIT} -X github.com/alexellis/jaas/version.Version=${VERSION}" -a -installsuffix cgo -o /root/jaas
1313

1414
FROM alpine:3.6
1515
WORKDIR /root/

Gopkg.lock

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/jaas.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Run ad-hoc commands`,
2929
}
3030

3131
func executeRoot(cmd *cobra.Command, args []string) error {
32-
fmt.Printf("JaaS!")
32+
fmt.Printf(figletStr)
3333
cmd.Help()
3434

3535
return nil

cmd/version.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Alex Ellis 2017. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
"runtime"
9+
10+
"github.com/alexellis/jaas/version"
11+
"github.com/morikuni/aec"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
// GitCommit injected at build-time
16+
var (
17+
shortVersion bool
18+
)
19+
20+
func init() {
21+
versionCmd.Flags().BoolVar(&shortVersion, "short-version", false, "Just print Git SHA")
22+
23+
rootCmd.AddCommand(versionCmd)
24+
}
25+
26+
// versionCmd displays version information
27+
var versionCmd = &cobra.Command{
28+
Use: "version [--short-version]",
29+
Short: "Display the clients version information",
30+
Long: fmt.Sprintf(`The version command returns the current clients version information.
31+
32+
This currently consists of the GitSHA from which the client was built.
33+
- https://github.com/alexellis/jaas/tree/%s`, version.GitCommit),
34+
Example: ` jaas version
35+
jaas version --short-version`,
36+
Run: runVersion,
37+
}
38+
39+
func runVersion(cmd *cobra.Command, args []string) {
40+
if shortVersion {
41+
fmt.Println(version.BuildVersion())
42+
} else {
43+
figletColoured := aec.BlueF.Apply(figletStr)
44+
if runtime.GOOS == "windows" {
45+
figletColoured = aec.GreenF.Apply(figletStr)
46+
}
47+
fmt.Printf(figletColoured)
48+
fmt.Printf("Commit: %s\n", version.GitCommit)
49+
fmt.Printf("Version: %s\n", version.BuildVersion())
50+
}
51+
}
52+
53+
const figletStr = `
54+
_ ____
55+
| | __ _ __ _/ ___|
56+
_ | |/ _` + "`" + ` |/ _` + "`" + ` \___ \
57+
| |_| | (_| | (_| |___) |
58+
\___/ \__,_|\__,_|____/
59+
60+
`

cmd/version_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Alex Ellis 2017. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package cmd
5+
6+
import (
7+
"regexp"
8+
"testing"
9+
10+
"github.com/alexellis/jaas/test"
11+
"github.com/alexellis/jaas/version"
12+
)
13+
14+
func Test_addVersionDev(t *testing.T) {
15+
version.GitCommit = "sha-test"
16+
17+
stdOut := test.CaptureStdout(func() {
18+
rootCmd.SetArgs([]string{"version"})
19+
rootCmd.Execute()
20+
})
21+
22+
if found, err := regexp.MatchString(`(?m:Commit: sha-test)`, stdOut); err != nil || !found {
23+
t.Fatalf("Output is not as expected:\n%s", stdOut)
24+
}
25+
26+
if found, err := regexp.MatchString(`(?m:Version: dev)`, stdOut); err != nil || !found {
27+
t.Fatalf("Output is not as expected:\n%s", stdOut)
28+
}
29+
}
30+
31+
func Test_addVersion(t *testing.T) {
32+
version.GitCommit = "sha-test"
33+
version.Version = "version.tag"
34+
35+
stdOut := test.CaptureStdout(func() {
36+
rootCmd.SetArgs([]string{"version"})
37+
rootCmd.Execute()
38+
})
39+
40+
if found, err := regexp.MatchString(`(?m:Commit: sha-test)`, stdOut); err != nil || !found {
41+
t.Fatalf("Output is not as expected:\n%s", stdOut)
42+
}
43+
44+
if found, err := regexp.MatchString(`(?m:Version: version.tag)`, stdOut); err != nil || !found {
45+
t.Fatalf("Output is not as expected:\n%s", stdOut)
46+
}
47+
}
48+
49+
func Test_addVersion_short_version(t *testing.T) {
50+
version.Version = "version.tag"
51+
52+
stdOut := test.CaptureStdout(func() {
53+
rootCmd.SetArgs([]string{"version", "--short-version"})
54+
rootCmd.Execute()
55+
})
56+
57+
if found, err := regexp.MatchString("^version\\.tag", stdOut); err != nil || !found {
58+
t.Fatalf("Output is not as expected:\n%s", stdOut)
59+
}
60+
}

test/utils.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
)
8+
9+
func CaptureStdout(f func()) string {
10+
stdOut := os.Stdout
11+
r, w, _ := os.Pipe()
12+
defer r.Close()
13+
os.Stdout = w
14+
15+
f()
16+
17+
w.Close()
18+
os.Stdout = stdOut
19+
20+
var b bytes.Buffer
21+
io.Copy(&b, r)
22+
23+
return b.String()
24+
}

vendor/github.com/morikuni/aec/LICENSE

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/morikuni/aec/README.md

+178
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)