diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..54bc7d9f --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/pkg/cmdline/version.go export-subst diff --git a/Makefile b/Makefile index 5a137330..9dea8679 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,8 @@ .PHONY: all build check clean cross test +GIT_VERSION ?= $(shell git describe --always --dirty) +VERSION_LDFLAGS=-X github.com/crc-org/macadam/pkg/cmdline.gitVersion=$(GIT_VERSION) + DEFAULT_GOOS=$(shell go env GOOS) DEFAULT_GOARCH=$(shell go env GOARCH) @@ -23,27 +26,27 @@ clean: bin/macadam-darwin-amd64: GOOS=darwin bin/macadam-darwin-amd64: GOARCH=amd64 bin/macadam-darwin-amd64: force-build - @go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam + @go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam bin/macadam-darwin-arm64: GOOS=darwin bin/macadam-darwin-arm64: GOARCH=arm64 bin/macadam-darwin-arm64: force-build - @go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam + @go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam bin/macadam-linux-amd64: GOOS=linux bin/macadam-linux-amd64: GOARCH=amd64 bin/macadam-linux-amd64: force-build - @go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam + @go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam bin/macadam-linux-arm64: GOOS=linux bin/macadam-linux-arm64: GOARCH=arm64 bin/macadam-linux-arm64: force-build - @go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam + @go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam bin/macadam-windows-amd64: GOOS=windows bin/macadam-windows-amd64: GOARCH=amd64 bin/macadam-windows-amd64: force-build - @go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam + @go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam .PHONY: lint lint: $(TOOLS_BINDIR)/golangci-lint diff --git a/cmd/macadam/main.go b/cmd/macadam/main.go index 341b8dba..04bde72d 100644 --- a/cmd/macadam/main.go +++ b/cmd/macadam/main.go @@ -1,9 +1,12 @@ package main import ( + "fmt" "log/slog" + + "github.com/crc-org/macadam/pkg/cmdline" ) func main() { - slog.Info("macadam") + slog.Info(fmt.Sprintf("macadam version %s", cmdline.Version())) } diff --git a/pkg/cmdline/version.go b/pkg/cmdline/version.go new file mode 100644 index 00000000..25c41440 --- /dev/null +++ b/pkg/cmdline/version.go @@ -0,0 +1,40 @@ +package cmdline + +import ( + "runtime/debug" + "strings" +) + +var ( + // set using the '-X github.com/crc-org/macadam/pkg/cmdline.gitVersion' linker flag + gitVersion = "unknown" + + // set through .gitattributes when `git archive` is used + // see https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/ + gitArchiveVersion = "$Format:%(describe)$" +) + +func Version() string { + switch { + // This will be substituted when building from a GitHub tarball + case !strings.HasPrefix(gitArchiveVersion, "$Format:"): + return gitArchiveVersion + // This will be set when building from git using make + case gitVersion != "": + return gitVersion + // moduleVersionFromBuildInfo() will be set when using `go install` + default: + return moduleVersionFromBuildInfo() + } +} + +func moduleVersionFromBuildInfo() string { + info, ok := debug.ReadBuildInfo() + if !ok { + return "" + } + if info.Main.Version == "(devel)" { + return "" + } + return info.Main.Version +}