From 5ed20e47f74d8571f52d3d184527b355308b872c Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Mon, 11 Apr 2016 13:35:44 -0400 Subject: [PATCH] Setting the version at build time from the latest tag Signed-off-by: Dave Henderson --- Makefile | 5 ++++- README.md | 9 ++++----- main.go | 6 ++---- version/version.go | 8 ++++++++ 4 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 version/version.go diff --git a/Makefile b/Makefile index dcbbf56cb..8d91d2646 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ GO := go PKG_NAME := gomplate PREFIX := . +COMMIT_FLAG := -X `go list ./version`.GitCommit=`git rev-parse --short HEAD 2>/dev/null` +VERSION_FLAG := -X `go list ./version`.Version=`git describe --abbrev=0 --tags $(git rev-list --tags --max-count=1) 2>/dev/null | sed 's/v\(.*\)/\1/'` + define gocross GOOS=$(1) GOARCH=$(2) \ $(GO) build \ @@ -21,7 +24,7 @@ build-x: $(shell find . -type f -name '*.go') $(call gocross,windows,386) $(PREFIX)/bin/$(PKG_NAME)$(call extension,$(GOOS)): $(shell find . -type f -name '*.go') - $(GO) build -o $@ + $(GO) build -ldflags "$(COMMIT_FLAG) $(VERSION_FLAG)" -o $@ build: $(PREFIX)/bin/$(PKG_NAME)$(call extension,$(GOOS)) diff --git a/README.md b/README.md index 4eba577ba..8120c709c 100644 --- a/README.md +++ b/README.md @@ -201,11 +201,10 @@ _Note:_ it's important for the `if`/`else`/`end` keywords to appear on the same Right now the release process is semi-automatic. -1. Update the Version `const` in `main.go` -2. Create a release tag: `git tag -a v0.0.9 -m "Releasing v0.9.9"` -3. Build binaries: `make build-x` -4. _(optional)_ compress the binaries with `upx` (may not work for all architectures) -5. Create a release in [github](https://github.com/hairyhenderson/gomplate/releases)! +1. Create a release tag: `git tag -a v0.0.9 -m "Releasing v0.9.9"` +2. Build binaries: `make build-x` +3. _(optional)_ compress the binaries with `upx` (may not work for all architectures) +4. Create a release in [github](https://github.com/hairyhenderson/gomplate/releases)! ## License diff --git a/main.go b/main.go index a0109b3b9..0df87d5d6 100644 --- a/main.go +++ b/main.go @@ -9,18 +9,16 @@ import ( "os" "github.com/hairyhenderson/gomplate/aws" + "github.com/hairyhenderson/gomplate/version" "text/template" ) -// version -const Version = "0.2.2" - func init() { ver := flag.Bool("v", false, "Print version and exit") flag.Parse() if *ver { - fmt.Println(Version) + fmt.Println(version.Version) os.Exit(0) } } diff --git a/version/version.go b/version/version.go new file mode 100644 index 000000000..cf5075c2d --- /dev/null +++ b/version/version.go @@ -0,0 +1,8 @@ +package version + +var ( + // Version will be overwritten automatically by the build + Version = "0.0.0" + // GitCommit will be overwritten automatically by the build + GitCommit = "HEAD" +)