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

Get version information from embedded build info #367

Merged
merged 1 commit into from
May 25, 2023
Merged
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
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

TAG ?= $(shell git fetch --tags && git describe --tags --abbrev=0 | sed 's/v//')
COMMIT ?= $(shell git rev-parse --short HEAD)
CLEAN ?= $(shell git diff --quiet --exit-code || echo '-SNAPSHOT')
VERSION = $(TAG)$(CLEAN)-$(COMMIT)

.PHONY: all
all: vet test build

.PHONY: build
build:
go build -ldflags="-X 'main.version=$(VERSION)'" ./cmd/provider
go build ./cmd/provider

.PHONY: docker
docker: clean
docker build . --force-rm -f Dockerfile -t index-provider:$(VERSION)
docker build . --force-rm -f Dockerfile -t index-provider:$(shell git rev-parse --short HEAD)

.PHONY: install
install:
Expand Down
14 changes: 12 additions & 2 deletions cmd/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"os/signal"
"syscall"

"github.com/ipni/index-provider"
"github.com/urfave/cli/v2"
)

var version = "unknown"

func main() {
os.Exit(run())
}
Expand All @@ -36,6 +35,17 @@ func run() int {
signal.Stop(interrupt)
}()

var rev string
if provider.Modified {
rev = "SNAPSHOT"
} else {
rev = provider.Revision
if len(rev) > 7 {
rev = rev[:7]
}
}
version := provider.Release + "-" + rev

app := &cli.App{
Name: "provider",
Usage: "Indexer Reference Provider Implementation",
Expand Down
52 changes: 52 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package provider

import (
_ "embed"
"encoding/json"
"runtime/debug"
)

var (
// Release is the release version tag value, e.g. "v1.2.3"
Release string
// Revision is the git commit hash.
Revision string
// Version is the full version string: Release-Revision.
Version string
// Modified indicates if the source tree had local modifications.
Modified bool
)

//go:embed version.json
var versionJSON []byte

func init() {
// Read version from embedded JSON file.
var verMap map[string]string
json.Unmarshal(versionJSON, &verMap)
Release = verMap["version"]

// If running from a module, try to get the build info.
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}

var found int

// Append the revision to the version.
for i := range bi.Settings {
switch bi.Settings[i].Key {
case "vcs.revision":
Revision = bi.Settings[i].Value
Version = Release + "-" + Revision
found++
case "vcs.modified":
Modified = bi.Settings[i].Value == "true"
found++
}
if found == 2 {
break
}
}
}