Skip to content

Commit

Permalink
Use embedded buildinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
9seconds committed Mar 18, 2022
1 parent 02ad052 commit 5df1f59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ APP_NAME := $(IMAGE_NAME)

GOLANGCI_LINT_VERSION := v1.44.2

VERSION_GO := $(shell go version)
VERSION_DATE := $(shell date -Ru)
VERSION_TAG := $(shell git describe --tags --always)
COMMON_BUILD_FLAGS := -trimpath -mod=readonly -ldflags="-extldflags '-static' -s -w -X 'main.version=$(VERSION_TAG) ($(VERSION_GO)) [$(VERSION_DATE)]'"
VERSION := $(shell git describe --exact-match HEAD 2>/dev/null || git describe --tags --always)
COMMON_BUILD_FLAGS := -trimpath -mod=readonly -ldflags="-extldflags '-static' -s -w -X 'main.version=$(VERSION)'"

FUZZ_FLAGS := -fuzztime=120s

Expand Down
29 changes: 29 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
package main

import (
"fmt"
"math/rand"
"runtime/debug"
"strconv"
"time"

"github.com/9seconds/mtg/v2/internal/cli"
Expand All @@ -26,6 +29,32 @@ func main() {
panic(err)
}

if buildInfo, ok := debug.ReadBuildInfo(); ok {
vcsCommit := "<no-commit>"
vcsDate := time.Now()
vcsDirty := ""

for _, setting := range buildInfo.Settings {
switch setting.Key {
case "vcs.time":
vcsDate, _ = time.Parse(time.RFC3339, setting.Value)
case "vcs.revision":
vcsCommit = setting.Value
case "vcs.modified":
if isDirty, _ := strconv.ParseBool(setting.Value); isDirty {
vcsDirty = " [dirty]"
}
}
}

version = fmt.Sprintf("%s (%s: %s on %s%s)",
version,
buildInfo.GoVersion,
vcsDate.Format(time.RFC3339),
vcsCommit,
vcsDirty)
}

cli := &cli.CLI{}
ctx := kong.Parse(cli, kong.Vars{
"version": version,
Expand Down

0 comments on commit 5df1f59

Please sign in to comment.