summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2020-10-15 20:02:19 -0700
committerStan Hu <stanhu@gmail.com>2020-10-15 22:41:48 -0700
commit9010cdccde441d37aff8a1d76f2e701134e42801 (patch)
treee9184718e6ad55b85dc805ea4359d4f0e4465daa
parent890bda90c9a09aa8d5a2441974138b92c8e38c18 (diff)
downloadgitlab-shell-9010cdccde441d37aff8a1d76f2e701134e42801.tar.gz
Add support for -version argument
This will help determine the version of the binary particularly on Cloud Native GitLab, where VERSION may not be shipped with the binaries.
-rw-r--r--Makefile5
-rw-r--r--cmd/gitlab-shell/main.go14
2 files changed, 18 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 5cffe76..664760f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,9 @@
.PHONY: validate verify verify_ruby verify_golang test test_ruby test_golang coverage coverage_golang setup _install build compile check clean
GO_SOURCES := $(shell find . -name '*.go')
+VERSION_STRING := $(shell git describe --match v* 2>/dev/null || cat VERSION 2>/dev/null || echo unknown)
+BUILD_TIME := $(shell date -u +%Y%m%d.%H%M%S)
+GOBUILD_FLAGS := -ldflags "-X main.Version=$(VERSION_STRING) -X main.BuildTime=$(BUILD_TIME)"
validate: verify test
@@ -37,7 +40,7 @@ _install:
build: bin/gitlab-shell
compile: bin/gitlab-shell
bin/gitlab-shell: $(GO_SOURCES)
- GOBIN="$(CURDIR)/bin" go install ./cmd/...
+ GOBIN="$(CURDIR)/bin" go install $(GOBUILD_FLAGS) ./cmd/...
check:
bin/check
diff --git a/cmd/gitlab-shell/main.go b/cmd/gitlab-shell/main.go
index 763aa5e..ff3a354 100644
--- a/cmd/gitlab-shell/main.go
+++ b/cmd/gitlab-shell/main.go
@@ -12,7 +12,21 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/internal/logger"
)
+var (
+ // Version is the current version of gitlab-shell
+ Version = "(unknown version)" // Set at build time in the Makefile
+ // BuildTime signifies the time the binary was build
+ BuildTime = "19700101.000000" // Set at build time in the Makefile
+)
+
func main() {
+ // We can't use the flag library because gitlab-shell receives other arguments
+ // that confuse the parser.
+ if len(os.Args) == 2 && os.Args[1] == "-version" {
+ fmt.Printf("gitlab-shell %s-%s\n", Version, BuildTime)
+ os.Exit(0)
+ }
+
readWriter := &readwriter.ReadWriter{
Out: os.Stdout,
In: os.Stdin,