Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the need to inject Version manually #768

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,16 @@ You can install from source to $GOBIN (`go env GOBIN`) with:

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

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

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

To use the latest development version switch to the `master` branch.

To build a fully statically linked binary:

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

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

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

Functionally it doesn't matter too much, but builds will be a bit easier and
faster as it won't require a C compiler.
Expand Down
34 changes: 33 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package goatcounter
import (
"context"
"fmt"
"runtime/debug"
"time"

"zgo.at/z18n"
Expand All @@ -18,9 +19,40 @@ import (
// Version of GoatCounter; set at compile-time with:
//
// -ldflags="-X zgo.at/goatcounter/v2.Version=…"

var Version = "dev"

func getCommit() (string, time.Time, bool) {
rev := ""
var last time.Time
dirty := false
info, _ := debug.ReadBuildInfo()
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
rev = kv.Value
case "vcs.time":
last, _ = time.Parse(time.RFC3339, kv.Value)
case "vcs.modified":
dirty = kv.Value == "true"
}
}
return rev, last, dirty
}

func init() {
if Version == "" || Version == "dev" {
// Only calculate the version if not explicitly overridden with:
// -ldflags="-X zgo.at/goatcounter/v2.Version=$tag"
// which is done for official builds.
if rev, last, dirty := getCommit(); rev != "" {
Version = rev[:12] + "_" + last.Format("2006-01-02T15:04:05Z0700")
if dirty {
Version += "-dev"
}
}
}
}

var (
keyCacheSites = &struct{ n string }{""}
keyCacheUA = &struct{ n string }{""}
Expand Down