From d40e44124047846a24202c2d0f2d0d155b398b28 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Tue, 27 Jun 2017 13:24:06 -0700 Subject: [PATCH] Use git sha1 as version if not tagged (#2969) --- Makefile | 23 ++++++++++++++--------- cmd/telegraf/telegraf.go | 21 +++++++++++++++------ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 1f4233ab0..a22d2bf1a 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,18 @@ -VERSION := $(shell sh -c 'git describe --always --tags') -BRANCH := $(shell sh -c 'git rev-parse --abbrev-ref HEAD') -COMMIT := $(shell sh -c 'git rev-parse --short HEAD') +VERSION := $(shell git describe --exact-match --tags 2>/dev/null) +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +COMMIT := $(shell git rev-parse --short HEAD) ifdef GOBIN PATH := $(GOBIN):$(PATH) else PATH := $(subst :,/bin:,$(GOPATH))/bin:$(PATH) endif +LDFLAGS := -X main.commit=$(COMMIT) -X main.branch=$(BRANCH) +ifdef VERSION + LDFLAGS += -X main.version=$(VERSION) +endif + + # Standard Telegraf build default: prepare build @@ -15,17 +21,16 @@ windows: prepare-windows build-windows # Only run the build (no dependency grabbing) build: - go install -ldflags \ - "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.branch=$(BRANCH)" ./... + go install -ldflags "$(LDFLAGS)" ./... build-windows: - GOOS=windows GOARCH=amd64 go build -o telegraf.exe -ldflags \ - "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.branch=$(BRANCH)" \ + GOOS=windows GOARCH=amd64 go build -o telegraf.exe \ + -ldflags "$(LDFLAGS)" \ ./cmd/telegraf/telegraf.go build-for-docker: - CGO_ENABLED=0 GOOS=linux go build -installsuffix cgo -o telegraf -ldflags \ - "-s -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.branch=$(BRANCH)" \ + CGO_ENABLED=0 GOOS=linux go build -installsuffix cgo -o telegraf \ + -ldflags "$(LDFLAGS)" \ ./cmd/telegraf/telegraf.go # run package script diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index af11e6682..e341606df 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -57,10 +57,12 @@ var fService = flag.String("service", "", // Telegraf version, populated linker. // ie, -ldflags "-X main.version=`git describe --always --tags`" + var ( - version string - commit string - branch string + nextVersion = "1.4.0" + version string + commit string + branch string ) func init() { @@ -196,7 +198,7 @@ func reloadLoop( } }() - log.Printf("I! Starting Telegraf (version %s)\n", version) + log.Printf("I! Starting Telegraf %s\n", displayVersion()) log.Printf("I! Loaded outputs: %s", strings.Join(c.OutputNames(), " ")) log.Printf("I! Loaded inputs: %s", strings.Join(c.InputNames(), " ")) log.Printf("I! Tags enabled: %s", c.ListTags()) @@ -254,6 +256,13 @@ func (p *program) Stop(s service.Service) error { return nil } +func displayVersion() string { + if version == "" { + return fmt.Sprintf("v%s~pre%s", nextVersion, commit) + } + return "v" + version +} + func main() { flag.Usage = func() { usageExit(0) } flag.Parse() @@ -295,7 +304,7 @@ func main() { if len(args) > 0 { switch args[0] { case "version": - fmt.Printf("Telegraf v%s (git: %s %s)\n", version, branch, commit) + fmt.Printf("Telegraf %s (git: %s %s)\n", displayVersion(), branch, commit) return case "config": config.PrintSampleConfig( @@ -323,7 +332,7 @@ func main() { } return case *fVersion: - fmt.Printf("Telegraf v%s (git: %s %s)\n", version, branch, commit) + fmt.Printf("Telegraf %s (git: %s %s)\n", displayVersion(), branch, commit) return case *fSampleConfig: config.PrintSampleConfig(