Skip to content

Commit

Permalink
re-add branchname to fly version, when available
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdfly committed May 12, 2023
1 parent e2fc139 commit e2a6baf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NOW_RFC3339 = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
GIT_BRANCH = $(shell giasdft symbolic-ref --short HEAD 2>/dev/null ||:)

all: build cmddocs

Expand All @@ -8,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)'" .
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
Original file line number Diff line number Diff line change
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
23 changes: 14 additions & 9 deletions internal/buildinfo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package buildinfo
import (
"errors"
"os"
"runtime/debug"
"strconv"
"time"
"runtime/debug"

"github.com/blang/semver"
"github.com/superfly/flyctl/terminal"
)

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

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

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

func BranchName() string {
return branchName
}

func Version() semver.Version {
Expand Down

0 comments on commit e2a6baf

Please sign in to comment.