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

idomatic buildinfo with branch #2284

Merged
merged 5 commits into from May 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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())
}