diff --git a/Makefile b/Makefile index 6f58af0..3a71e87 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,10 @@ CGO_ENABLED=0 VERSION=$(shell git describe --abbrev=0 --tags) COMMIT=$(shell git rev-parse --short HEAD) +BUILD=$(shell git show -s --pretty=format:%cI) +GOCMD=go + +DESTDIR=/usr/local/bin all: dev @@ -11,21 +15,21 @@ dev: build @./bitcaskd --version build: clean generate - @go build \ + @$(GOCMD) build \ -tags "netgo static_build" -installsuffix netgo \ - -ldflags "-w -X $(shell go list)/internal.Version=$(VERSION) -X $(shell go list)/internal.Commit=$(COMMIT)" \ + -ldflags "-w -X $(shell go list)/internal.Version=$(VERSION) -X $(shell go list)/internal.Commit=$(COMMIT) -X $(shell go list)/internal.Build=$(BUILD)" \ ./cmd/bitcask/... - @go build \ + @$(GOCMD) build \ -tags "netgo static_build" -installsuffix netgo \ - -ldflags "-w -X $(shell go list)/internal.Version=$(VERSION) -X $(shell go list)/internal.Commit=$(COMMIT)" \ + -ldflags "-w -X $(shell go list)/internal.Version=$(VERSION) -X $(shell go list)/internal.Commit=$(COMMIT) -X $(shell go list)/internal.Build=$(BUILD)" \ ./cmd/bitcaskd/... generate: - @go generate $(shell go list)/... + @$(GOCMD) generate $(shell go list)/... install: build - @go install ./cmd/bitcask/... - @go install ./cmd/bitcaskd/... + @install -D -m 755 yarnd $(DESTDIR)/bitcask + @install -D -m 755 yarnc $(DESTDIR)/bitcaskd ifeq ($(PUBLISH), 1) image: @@ -40,23 +44,23 @@ release: @./tools/release.sh profile: build - @go test -cpuprofile cpu.prof -memprofile mem.prof -v -bench . + @$(GOCMD) test -cpuprofile cpu.prof -memprofile mem.prof -v -bench . bench: build - @go test -v -run=XXX -benchmem -bench=. . + @$(GOCMD) test -v -run=XXX -benchmem -bench=. . mocks: @mockery -all -case underscore -output ./internal/mocks -recursive test: build - @go test -v \ + @$(GOCMD) test -v \ -cover -coverprofile=coverage.txt -covermode=atomic \ -coverpkg=$(shell go list) \ -race \ . setup: - @go get github.com/vektra/mockery/... + @$(GOCMD) get github.com/vektra/mockery/... clean: @git clean -f -d -X diff --git a/internal/version.go b/internal/version.go index 79a1bbf..592c171 100644 --- a/internal/version.go +++ b/internal/version.go @@ -2,17 +2,49 @@ package internal import ( "fmt" + "runtime/debug" + "strings" +) + +const ( + defaultVersion = "0.0.0" + defaultCommit = "HEAD" + defaultBuild = "0000-01-01:00:00+00:00" ) var ( - // Version release version - Version = "0.0.1" + // Version is the tagged release version in the form .. + // following semantic versioning and is overwritten by the build system. + Version = defaultVersion - // Commit will be overwritten automatically by the build system - Commit = "HEAD" + // Commit is the commit sha of the build (normally from Git) and is overwritten + // by the build system. + Commit = defaultCommit + + // Build is the date and time of the build as an RFC3339 formatted string + // and is overwritten by the build system. + Build = defaultBuild ) -// FullVersion returns the full version and commit hash +// FullVersion display the full version and build func FullVersion() string { - return fmt.Sprintf("%s@%s", Version, Commit) + var sb strings.Builder + + isDefault := Version == defaultVersion && Commit == defaultCommit && Build == defaultBuild + + if !isDefault { + sb.WriteString(fmt.Sprintf("%s@%s %s", Version, Commit, Build)) + } + + if info, ok := debug.ReadBuildInfo(); ok { + if isDefault { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Version)) + } + sb.WriteString(fmt.Sprintf(" %s", info.GoVersion)) + if info.Main.Sum != "" { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Sum)) + } + } + + return sb.String() }