From e24c06aaddd9577723cf4767f96c0af0048e28a0 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Tue, 20 Nov 2018 09:25:24 +0100 Subject: [PATCH] correctly set version, commit & date through ldflags in goreleaser config. closes #186 --- .goreleaser.yml | 2 +- Makefile | 2 +- main.go | 2 +- pkg/cli/cli.go | 9 ++++++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index ba2bb60..f4a5f7b 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -14,7 +14,7 @@ builds: - 386 - arm64 ldflags: - - -extldflags "-static" + - -extldflags "-static" -s -w -X "main.version={{.Version}}" -X "main.commit={{.Commit}}" -X "main.date={{.Date}}" hooks: pre: packr post: packr clean diff --git a/Makefile b/Makefile index bbc1537..befb9bd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ EXECUTABLE := fathom -LDFLAGS += -extldflags "-static" -X "main.version=$(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')" -X "main.commit=$(shell git rev-parse HEAD)" +LDFLAGS += -extldflags "-static" -X "main.version=$(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')" -X "main.commit=$(shell git rev-parse HEAD)" -X "main.date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" MAIN_PKG := ./main.go PACKAGES ?= $(shell go list ./... | grep -v /vendor/) ASSET_SOURCES ?= $(shell find assets/src/. -type f) diff --git a/main.go b/main.go index eb00dec..f4c7e0a 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ var ( ) func main() { - err := cli.Run(version) + err := cli.Run(version, commit, date) if err != nil { fmt.Print(err) os.Exit(1) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index e458d55..231bdbc 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -1,7 +1,9 @@ package cli import ( + "fmt" "os" + "strings" "time" log "github.com/sirupsen/logrus" @@ -19,7 +21,8 @@ type App struct { // CLI application var app *App -func Run(v string) error { +// Run parses the CLI arguments & run application command +func Run(version string, commit string, buildDate string) error { // force all times in UTC, regardless of server timezone time.Local = time.UTC @@ -27,7 +30,7 @@ func Run(v string) error { app = &App{cli.NewApp(), nil, nil} app.Name = "Fathom" app.Usage = "simple & transparent website analytics" - app.Version = v + app.Version = fmt.Sprintf("%v, commit %v, built at %v", strings.TrimPrefix(version, "v"), commit, buildDate) app.HelpName = "fathom" app.Flags = []cli.Flag{ cli.StringFlag{ @@ -45,7 +48,7 @@ func Run(v string) error { } if len(os.Args) < 2 || os.Args[1] != "--version" { - log.Printf("%s %s", app.Name, app.Version) + log.Printf("%s version %s", app.Name, app.Version) } err := app.Run(os.Args)