Add version package and update the makefile

The makefile will grab the version info from git and pass it to the
linker so that version information based on tags, commits and commit
times is available in the code.
This commit is contained in:
2025-05-08 15:54:13 +02:00
parent cd0fb4edd2
commit 5145191c2b
3 changed files with 32 additions and 6 deletions

View File

@ -3,6 +3,15 @@ BINARIES = $(patsubst cmd/%/,%,$(wildcard cmd/*/))
.PHONY: all build test validate clean run $(BINARIES)
VERSION := $(shell git describe --tags --always --dirty)
COMMIT := $(shell git rev-parse --short HEAD)
COMMIT_DATETIME := $(shell git log -1 --format=%cd --date=iso8601)
LDFLAGS := -X git.omicron.one/omicron/linkshare/internal/version.Version=$(VERSION) \
-X git.omicron.one/omicron/linkshare/internal/version.GitCommit=$(COMMIT) \
-X "git.omicron.one/omicron/linkshare/internal/version.CommitDateTime=$(COMMIT_DATETIME)"
all: build
@ -13,7 +22,7 @@ $(BINARY_DIR):
mkdir -p $(BINARY_DIR)
$(BINARIES): %: $(BINARY_DIR)
go build -o $(BINARY_DIR)/$@ ./cmd/$@/
go build -ldflags '$(LDFLAGS)' -o $(BINARY_DIR)/$@ ./cmd/$@/
test:
go test ./...

View File

@ -14,9 +14,9 @@ import (
"strconv"
_ "github.com/mattn/go-sqlite3"
)
const expectedSchemaVersion = 1
"git.omicron.one/omicron/linkshare/internal/version"
)
// DB represents a database connection
type DB struct {
@ -120,13 +120,13 @@ func (db *DB) CheckSchemaVersion() error {
if err != nil {
return err
}
version, err := db.getSchemaVersion()
version_, err := db.getSchemaVersion()
if err != nil {
return err
}
if version < expectedSchemaVersion {
if version_ < version.SchemaVersion {
return ErrDatabaseSchemaOutdated
} else if version > expectedSchemaVersion {
} else if version_ > version.SchemaVersion {
return ErrDatabaseSchemaUnsupported
}
return nil

View File

@ -0,0 +1,17 @@
package version
import "fmt"
var (
Version = "dev"
GitCommit = "unknown"
CommitDateTime = "unknown"
SchemaVersion = 1
)
// PrintVersionInfo prints formatted version information to stdout
func Print() {
fmt.Printf("Version: %s\n", Version)
fmt.Printf("Git commit: %s %s\n", GitCommit, CommitDateTime)
fmt.Printf("Schema: v%d\n", SchemaVersion)
}