Skip to content

Commit 881ad1b

Browse files
authored
chore: Externalize version & build information (#104)
* chore: Externalize version information * Use full Git commit information * Introduce short and long version information * Typos * More typos
1 parent fa95873 commit 881ad1b

File tree

5 files changed

+65
-20
lines changed

5 files changed

+65
-20
lines changed

.github/actions/spelling/allow.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ gh
6363
ghcr
6464
githash
6565
github
66+
GOARCH
6667
goimports
6768
golang
6869
golangci
@@ -98,6 +99,7 @@ kubefake
9899
kubernetes
99100
Kustomization
100101
kustomize
102+
ldflags
101103
LDFLAGS
102104
len
103105
linted
@@ -171,6 +173,7 @@ structcheck
171173
svg
172174
svi
173175
svl
176+
SZ
174177
taglist
175178
tagsortmode
176179
TEMPFILE

Makefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
LDFLAGS=-extldflags "-static"
2-
31
IMAGE_NAMESPACE?=argoprojlabs
42
IMAGE_NAME=argocd-image-updater
53
IMAGE_TAG?=latest
@@ -9,6 +7,21 @@ else
97
IMAGE_PREFIX=
108
endif
119

10+
CURRENT_DIR=$(shell pwd)
11+
VERSION=$(shell cat ${CURRENT_DIR}/VERSION)
12+
GIT_COMMIT=$(shell git rev-parse HEAD)
13+
BUILD_DATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
14+
15+
LDFLAGS=
16+
17+
VERSION_PACKAGE=github.com/argoproj-labs/argocd-image-updater/pkg/version
18+
19+
override LDFLAGS += -extldflags "-static"
20+
override LDFLAGS += \
21+
-X ${VERSION_PACKAGE}.version=${VERSION} \
22+
-X ${VERSION_PACKAGE}.gitCommit=${GIT_COMMIT} \
23+
-X ${VERSION_PACKAGE}.buildDate=${BUILD_DATE}
24+
1225
.PHONY: all
1326
all: prereq controller
1427

@@ -43,7 +56,7 @@ prereq:
4356

4457
.PHONY: controller
4558
controller:
46-
CGO_ENABLED=0 go build -o dist/argocd-image-updater cmd/main.go
59+
CGO_ENABLED=0 go build -ldflags '${LDFLAGS}' -o dist/argocd-image-updater cmd/main.go
4760

4861
.PHONY: image
4962
image: clean-image mod-vendor

cmd/main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,25 @@ func newRootCommand() error {
170170

171171
// newVersionCommand implements "version" command
172172
func newVersionCommand() *cobra.Command {
173+
var short bool
173174
var versionCmd = &cobra.Command{
174175
Use: "version",
175176
Short: "Display version information",
176177
RunE: func(cmd *cobra.Command, args []string) error {
177-
fmt.Printf("%s\n", version.Useragent())
178+
if !short {
179+
fmt.Printf("%s\n", version.Useragent())
180+
fmt.Printf(" BuildDate: %s\n", version.BuildDate())
181+
fmt.Printf(" GitCommit: %s\n", version.GitCommit())
182+
fmt.Printf(" GoVersion: %s\n", version.GoVersion())
183+
fmt.Printf(" GoCompiler: %s\n", version.GoCompiler())
184+
fmt.Printf(" Platform: %s\n", version.GoPlatform())
185+
} else {
186+
fmt.Printf("%s\n", version.Version())
187+
}
178188
return nil
179189
},
180190
}
181-
191+
versionCmd.Flags().BoolVar(&short, "short", false, "show only the version number")
182192
return versionCmd
183193
}
184194

pkg/version/version.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package version
22

3-
import "fmt"
4-
5-
const (
6-
majorVersion = "0"
7-
minorVersion = "7"
8-
patchVersion = "0"
9-
preReleaseString = "master"
3+
import (
4+
"fmt"
5+
"runtime"
6+
"time"
107
)
118

12-
const binaryName = "argocd-image-updater"
9+
var (
10+
version = "9.9.99"
11+
buildDate = time.Now().UTC().Format(time.RFC3339)
12+
gitCommit = "unknown"
13+
binaryName = "argocd-image-updater"
14+
)
1315

1416
func Version() string {
15-
version := fmt.Sprintf("v%s.%s.%s", majorVersion, minorVersion, patchVersion)
16-
if preReleaseString != "" {
17-
version += fmt.Sprintf("-%s", preReleaseString)
18-
}
17+
version := fmt.Sprintf("v%s+%s", version, gitCommit[0:7])
1918
return version
2019
}
2120

@@ -24,5 +23,25 @@ func BinaryName() string {
2423
}
2524

2625
func Useragent() string {
27-
return fmt.Sprintf("%s %s", BinaryName(), Version())
26+
return fmt.Sprintf("%s: %s", BinaryName(), Version())
27+
}
28+
29+
func GitCommit() string {
30+
return gitCommit
31+
}
32+
33+
func BuildDate() string {
34+
return buildDate
35+
}
36+
37+
func GoVersion() string {
38+
return runtime.Version()
39+
}
40+
41+
func GoPlatform() string {
42+
return runtime.GOOS + "/" + runtime.GOARCH
43+
}
44+
45+
func GoCompiler() string {
46+
return runtime.Compiler
2847
}

pkg/version/version_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ func Test_BinaryName(t *testing.T) {
1212
}
1313

1414
func Test_Version(t *testing.T) {
15-
assert.Regexp(t, `^v[0-9]\.[0-9]\.[0-9](-[a-z]+)*$`, Version())
15+
assert.Regexp(t, `^v[0-9]+\.[0-9]+\.[0-9]+(\-[a-z]+)*(\+[a-z0-9]+)*$`, Version())
1616
}
1717

1818
func Test_Useragent(t *testing.T) {
19-
assert.Regexp(t, `^[a-z\-]+\sv[0-9]\.[0-9]\.[0-9](-[a-z]+)*$`, Useragent())
19+
assert.Regexp(t, `^[a-z\-]+:\sv[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+)*(\+[a-z0-9]+)*$`, Useragent())
2020
}

0 commit comments

Comments
 (0)