Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit a043169

Browse files
authored
Merge pull request #1002 from laverya/improve-version-output
improve version command output and fix git sha
2 parents 96ca42d + b67feab commit a043169

File tree

4 files changed

+167
-3
lines changed

4 files changed

+167
-3
lines changed

Diff for: deploy/.goreleaser.unstable.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ builds:
2525
main: cmd/ship/main.go
2626
ldflags: -s -w
2727
-X github.com/replicatedhq/ship/pkg/version.version={{.Version}}
28-
-X github.com/replicatedhq/ship/pkg/version.gitSHA={{.Commit}}
28+
-X github.com/replicatedhq/ship/pkg/version.gitSHA={{.FullCommit}}
2929
-X github.com/replicatedhq/ship/pkg/version.buildTime={{.Date}}
3030
-X github.com/replicatedhq/ship/pkg/version.helm=v2.14.1
3131
-X github.com/replicatedhq/ship/pkg/version.kustomize=v2.0.3

Diff for: deploy/.goreleaser.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ builds:
2525
main: cmd/ship/main.go
2626
ldflags: -s -w
2727
-X github.com/replicatedhq/ship/pkg/version.version={{.Version}}
28-
-X github.com/replicatedhq/ship/pkg/version.gitSHA={{.Commit}}
28+
-X github.com/replicatedhq/ship/pkg/version.gitSHA={{.FullCommit}}
2929
-X github.com/replicatedhq/ship/pkg/version.buildTime={{.Date}}
3030
-X github.com/replicatedhq/ship/pkg/version.helm=v2.14.1
3131
-X github.com/replicatedhq/ship/pkg/version.kustomize=v2.0.3

Diff for: pkg/version/version.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package version
22

3-
import "time"
3+
import (
4+
"runtime"
5+
"time"
6+
)
47

58
var (
69
build Build
@@ -13,6 +16,7 @@ type Build struct {
1316
BuildTime time.Time `json:"buildTime,omitempty"`
1417
TimeFallback string `json:"buildTimeFallback,omitempty"`
1518
Dependencies BuildDependencies `json:"dependencies,omitempty"`
19+
GoInfo GoInfo `json:"go,omitempty"`
1620
}
1721

1822
type BuildDependencies struct {
@@ -21,6 +25,13 @@ type BuildDependencies struct {
2125
Terraform string `json:"terraform,omitempty"`
2226
}
2327

28+
type GoInfo struct {
29+
Version string `json:"version,omitempty"`
30+
Compiler string `json:"compiler,omitempty"`
31+
OS string `json:"os,omitempty"`
32+
Arch string `json:"arch,omitempty"`
33+
}
34+
2435
// initBuild sets up the version info from build args
2536
func initBuild() {
2637
build.Version = version
@@ -39,6 +50,8 @@ func initBuild() {
3950
Terraform: terraform,
4051
}
4152
build.Dependencies = deps
53+
54+
build.GoInfo = getGoInfo()
4255
}
4356

4457
// GetBuild gets the build
@@ -60,3 +73,12 @@ func GitSHA() string {
6073
func BuildTime() time.Time {
6174
return build.BuildTime
6275
}
76+
77+
func getGoInfo() GoInfo {
78+
return GoInfo{
79+
Version: runtime.Version(),
80+
Compiler: runtime.Compiler,
81+
OS: runtime.GOOS,
82+
Arch: runtime.GOARCH,
83+
}
84+
}

Diff for: pkg/version/version_test.go

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package version
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestVersion(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
want string
14+
}{
15+
{
16+
name: "empty",
17+
want: "",
18+
},
19+
{
20+
name: "version string",
21+
want: "v0.1.2",
22+
},
23+
}
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
req := require.New(t)
27+
28+
version = tt.want
29+
initBuild()
30+
31+
got := Version()
32+
req.Equal(tt.want, got)
33+
})
34+
}
35+
}
36+
37+
func TestGitSHA(t *testing.T) {
38+
tests := []struct {
39+
name string
40+
sha string
41+
want string
42+
}{
43+
{
44+
name: "empty",
45+
sha: "",
46+
want: "",
47+
},
48+
{
49+
name: "too short",
50+
sha: "123456",
51+
want: "",
52+
},
53+
{
54+
name: "7 chars",
55+
sha: "1234567",
56+
want: "1234567",
57+
},
58+
{
59+
name: "full sha",
60+
sha: "e21cf800acca2aa972e7f5f65f7134b5da92f05f",
61+
want: "e21cf80",
62+
},
63+
}
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
req := require.New(t)
67+
68+
gitSHA = tt.sha
69+
initBuild()
70+
71+
got := GitSHA()
72+
req.Equal(tt.want, got)
73+
})
74+
}
75+
}
76+
77+
func TestBuildTime(t *testing.T) {
78+
req := require.New(t)
79+
aTime, err := time.Parse(time.RFC3339, "2019-06-26T18:53:19Z")
80+
req.NoError(err, "parse constant time")
81+
82+
tests := []struct {
83+
name string
84+
timestring string
85+
want time.Time
86+
}{
87+
{
88+
name: "empty",
89+
timestring: "",
90+
want: time.Time{},
91+
},
92+
{
93+
name: "proper format",
94+
timestring: "2019-06-26T18:53:19Z",
95+
want: aTime,
96+
},
97+
}
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
req := require.New(t)
101+
102+
buildTime = tt.timestring
103+
initBuild()
104+
105+
got := BuildTime()
106+
req.Equal(tt.want, got)
107+
})
108+
}
109+
}
110+
111+
func TestGetBuild(t *testing.T) {
112+
tests := []struct {
113+
name string
114+
gitSHA string
115+
version string
116+
buildTime string
117+
want Build
118+
}{
119+
{
120+
name: "goInfo",
121+
gitSHA: "12345678",
122+
want: Build{
123+
GitSHA: "1234567",
124+
GoInfo: getGoInfo(),
125+
},
126+
},
127+
}
128+
for _, tt := range tests {
129+
t.Run(tt.name, func(t *testing.T) {
130+
req := require.New(t)
131+
132+
version = tt.version
133+
gitSHA = tt.gitSHA
134+
buildTime = tt.buildTime
135+
136+
initBuild()
137+
138+
got := GetBuild()
139+
req.Equal(tt.want, got)
140+
})
141+
}
142+
}

0 commit comments

Comments
 (0)