From 0e3b9a465f110d7286a2037bb82b4c3521ade9c5 Mon Sep 17 00:00:00 2001 From: Egor Kovetskiy Date: Wed, 17 Mar 2021 12:51:15 +0300 Subject: [PATCH] [enhancement] Pass version via ldflags, trim GOPATH in stacktraces (#50) This way you can avoid unnecessary commits and unnecessary build info. --- .goreleaser.yml | 4 ++++ Makefile | 13 +++++++------ main.go | 9 ++++++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 55aa4dd..0541701 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -6,6 +6,10 @@ before: builds: - env: - CGO_ENABLED=0 + gcflags: + - "all=-trimpath={{.Env.GOPATH}}" + ldflags: + - "-X=main.version={{.Env.VERSION}}" goos: - linux - windows diff --git a/Makefile b/Makefile index c008785..130b3b2 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,13 @@ -VERSION_REGEX := 's/(v[0-9\.]+)/$(version)/g' +VERSION = $(shell git describe --tags --abbrev=0) + +version: + @echo $(VERSION) build: - go build -o smug *.go + go build -ldflags "-X=main.version=$(VERSION)" -gcflags "all=-trimpath=$(GOPATH)" test: - go test . + go test coverage: go test -coverprofile=coverage.out @@ -14,8 +17,6 @@ release: ifndef GITHUB_TOKEN $(error GITHUB_TOKEN is not defined) endif - sed -E -i.bak $(VERSION_REGEX) 'main.go' && rm main.go.bak - git commit -am 'Update version to $(version)' git tag -a $(version) -m '$(version)' git push origin $(version) - goreleaser --rm-dist + VERSION=$(version) goreleaser --rm-dist diff --git a/main.go b/main.go index 9bdad8e..a47a7f6 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "strings" ) -const version = "v0.1.9" +var version = "[dev build]" var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s @@ -43,7 +43,11 @@ func main() { } if err != nil { - fmt.Fprintf(os.Stderr, "Cannot parse command line options: %q", err.Error()) + fmt.Fprintf( + os.Stderr, + "Cannot parse command line options: %q", + err.Error(), + ) os.Exit(1) } @@ -122,5 +126,4 @@ func main() { fmt.Println(strings.Join(configs, "\n")) } - }