Skip to content

Commit

Permalink
[enhancement] Pass version via ldflags, trim GOPATH in stacktraces (#50)
Browse files Browse the repository at this point in the history
This way you can avoid unnecessary commits and unnecessary build info.
  • Loading branch information
kovetskiy committed Mar 17, 2021
1 parent b08f9bf commit 0e3b9a4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ before:
builds:
- env:
- CGO_ENABLED=0
gcflags:
- "all=-trimpath={{.Env.GOPATH}}"
ldflags:
- "-X=main.version={{.Env.VERSION}}"
goos:
- linux
- windows
Expand Down
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -122,5 +126,4 @@ func main() {

fmt.Println(strings.Join(configs, "\n"))
}

}

0 comments on commit 0e3b9a4

Please sign in to comment.