Skip to content

Commit

Permalink
Merge pull request #2284 from superfly/idomatic_buildinfo-with-branch
Browse files Browse the repository at this point in the history
idomatic buildinfo with branch
  • Loading branch information
tvdfly committed May 15, 2023
2 parents eebf3ba + 6b49ecc commit d5159ea
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
8 changes: 1 addition & 7 deletions Makefile
@@ -1,11 +1,5 @@
NOW_RFC3339 = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
GIT_SHA = $(shell git rev-parse HEAD 2>/dev/null || no_git)
GIT_BRANCH = $(shell git symbolic-ref --short HEAD 2>/dev/null ||:)
ifneq ($(GIT_BRANCH),)
GIT_COMMIT = $(GIT_SHA) ($(GIT_BRANCH))
else
GIT_COMMIT = $(GIT_SHA)
endif

all: build cmddocs

Expand All @@ -15,7 +9,7 @@ generate:

build: generate
@echo Running Build
go build -o bin/flyctl -ldflags="-X 'github.com/superfly/flyctl/internal/buildinfo.buildDate=$(NOW_RFC3339)' -X 'github.com/superfly/flyctl/internal/buildinfo.commit=$(GIT_COMMIT)'" .
go build -o bin/flyctl -ldflags="-X 'github.com/superfly/flyctl/internal/buildinfo.buildDate=$(NOW_RFC3339)' -X 'github.com/superfly/flyctl/internal/buildinfo.branchName=$(GIT_BRANCH)'" .

test: FORCE
go test ./... -ldflags="-X 'github.com/superfly/flyctl/internal/buildinfo.buildDate=$(NOW_RFC3339)'" --run=$(T)
Expand Down
8 changes: 7 additions & 1 deletion internal/buildinfo/buildinfo.go
Expand Up @@ -32,27 +32,33 @@ type info struct {
Name string
Version semver.Version
Commit string
BranchName string
BuildDate time.Time
OS string
Architecture string
Environment string
}

func (i info) String() string {
return fmt.Sprintf("%s v%s %s/%s Commit: %s BuildDate: %s",
res := fmt.Sprintf("%s v%s %s/%s Commit: %s BuildDate: %s",
i.Name,
i.Version,
i.OS,
i.Architecture,
i.Commit,
i.BuildDate.Format(time.RFC3339))
if i.BranchName != "" {
res += fmt.Sprintf(" BranchName: %s", i.BranchName)
}
return res
}

func Info() info {
return info{
Name: Name(),
Version: Version(),
Commit: Commit(),
BranchName: BranchName(),
BuildDate: BuildDate(),
OS: OS(),
Architecture: Arch(),
Expand Down
28 changes: 24 additions & 4 deletions internal/buildinfo/version.go
Expand Up @@ -3,6 +3,7 @@ package buildinfo
import (
"errors"
"os"
"runtime/debug"
"strconv"
"time"

Expand All @@ -11,9 +12,9 @@ import (
)

var (
buildDate = "<date>"
version = "<version>"
commit = "<commit>"
buildDate = "<date>"
version = "<version>"
branchName = ""
)

var (
Expand Down Expand Up @@ -66,7 +67,26 @@ func loadMeta() {
}

func Commit() string {
return commit
info, _ := debug.ReadBuildInfo()
var rev string = "<none>"
var dirty string = ""
for _, v := range info.Settings {
if v.Key == "vcs.revision" {
rev = v.Value
}
if v.Key == "vcs.modified" {
if v.Value == "true" {
dirty = "-dirty"
} else {
dirty = ""
}
}
}
return rev + dirty
}

func BranchName() string {
return branchName
}

func Version() semver.Version {
Expand Down
4 changes: 0 additions & 4 deletions internal/buildinfo/version_test.go
Expand Up @@ -12,22 +12,18 @@ func TestProdMeta(t *testing.T) {
environment = "production"
version = "1.2.3"
buildDate = "2020-06-05T13:32:23Z"
commit = "c8f7b8f"

loadMeta()

assert.Equal(t, "1.2.3", Version().String())
assert.Equal(t, "2020-06-05T13:32:23Z", BuildDate().Format(time.RFC3339))
assert.Equal(t, "c8f7b8f", Commit())
}

func TestDevMeta(t *testing.T) {
environment = "development"
version = "<version>"
commit = "<commit>"

loadMeta()

assert.Equal(t, fmt.Sprintf("0.0.0-%d+dev", BuildDate().Unix()), Version().String())
assert.Equal(t, "<commit>", Commit())
}

0 comments on commit d5159ea

Please sign in to comment.