Skip to content

Commit b805031

Browse files
authored
Simplify the building process for end users. (#768)
Use debug.ReadBuildInfo() to generate Version. Before: $ CGO_ENABLED=0 go install ./cmd/goatcounter $ goatcounter version version=dev; go=go1.23.1; GOOS=linux; GOARCH=amd64; race=false; cgo=false After: $ CGO_ENABLED=0 go install ./cmd/goatcounter $ goatcounter version version=535cf676f9a3_2024-09-23T18:24:51Z; go=go1.23.1; GOOS=linux; GOARCH=amd64; race=false; cgo=false After, with a locally modified file: $ CGO_ENABLED=0 go install ./cmd/goatcounter $ goatcounter version version=8ab2fe057181_2024-09-23T18:49:30Z-dev; go=go1.23.1; GOOS=linux; GOARCH=amd64; race=false; cgo=false Technically this variable should live inside cmd/goatcounter but that would be a more significant refactoring.
1 parent bdf6c6a commit b805031

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

Diff for: README.md

+3-11
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,16 @@ You can install from source to $GOBIN (`go env GOBIN`) with:
132132

133133
% git clone --branch=release-2.5 https://github.com/arp242/goatcounter.git
134134
% cd goatcounter
135-
% go build -ldflags="-X zgo.at/goatcounter/v2.Version=$(git log -n1 --format='%h_%cI')" ./cmd/goatcounter
135+
% go build ./cmd/goatcounter
136136

137137
Which will produce a `goatcounter` binary in the current directory.
138138

139-
The `-ldflags=[..]` sets the version; this isn't *strictly* required as such,
140-
but it's recommended as it's used to "bust" the cache for static files and may
141-
also be useful later when reporting bugs. This can be any string and doesn't
142-
follow any particular format, you can also set this to the current date or
143-
`banana` or anything you want really.
144-
145139
To use the latest development version switch to the `master` branch.
146140

147141
To build a fully statically linked binary:
148142

149143
% go build -tags osusergo,netgo,sqlite_omit_load_extension \
150-
-ldflags="-X zgo.at/goatcounter/v2.Version=$(git log -n1 --format='%h_%cI') -extldflags=-static" \
144+
-extldflags=-static" \
151145
./cmd/goatcounter
152146

153147
It's recommended to use the latest release as in the above command. The master
@@ -158,9 +152,7 @@ to surprises.
158152
You can compile goatcounter without cgo if you're planning to use PostgreSQL and
159153
don't use SQLite:
160154

161-
% CGO_ENABLED=0 go build \
162-
-ldflags="-X zgo.at/goatcounter.Version=$(git log -n1 --format='%h_%cI')" \
163-
./cmd/goatcounter
155+
% CGO_ENABLED=0 go build ./cmd/goatcounter
164156

165157
Functionally it doesn't matter too much, but builds will be a bit easier and
166158
faster as it won't require a C compiler.

Diff for: context.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goatcounter
33
import (
44
"context"
55
"fmt"
6+
"runtime/debug"
67
"time"
78

89
"zgo.at/z18n"
@@ -13,10 +14,43 @@ import (
1314

1415
// Version of GoatCounter; set at compile-time with:
1516
//
16-
// -ldflags="-X zgo.at/goatcounter/v2.Version=…"
17-
17+
// -ldflags="-X zgo.at/goatcounter/v2.Version=…"
1818
var Version = "dev"
1919

20+
func getCommit() (string, time.Time, bool) {
21+
var (
22+
rev string
23+
last time.Time
24+
dirty bool
25+
info, _ = debug.ReadBuildInfo()
26+
)
27+
for _, kv := range info.Settings {
28+
switch kv.Key {
29+
case "vcs.revision":
30+
rev = kv.Value
31+
case "vcs.time":
32+
last, _ = time.Parse(time.RFC3339, kv.Value)
33+
case "vcs.modified":
34+
dirty = kv.Value == "true"
35+
}
36+
}
37+
return rev, last, dirty
38+
}
39+
40+
func init() {
41+
if Version == "" || Version == "dev" {
42+
// Only calculate the version if not explicitly overridden with:
43+
// -ldflags="-X zgo.at/goatcounter/v2.Version=$tag"
44+
// which is done for release builds.
45+
if rev, last, dirty := getCommit(); rev != "" {
46+
Version = rev[:12] + "_" + last.Format("2006-01-02T15:04:05Z0700")
47+
if dirty {
48+
Version += "-dev"
49+
}
50+
}
51+
}
52+
}
53+
2054
var (
2155
keyCacheSites = &struct{ n string }{""}
2256
keyCacheUA = &struct{ n string }{""}

0 commit comments

Comments
 (0)