Skip to content

Commit

Permalink
Get version information from embedded build info (#367)
Browse files Browse the repository at this point in the history
The version is provided by the version.json file and the revision information is build into the binary by the compiler. Use this information instead of fetching it from github each build.

- Installed binary has version information.
- Builds work even without connectivity to github.
  • Loading branch information
gammazero authored May 25, 2023
1 parent db6aab9 commit e247a19
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
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
}
}
}

0 comments on commit e247a19

Please sign in to comment.