Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions internal/buildinfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package buildinfo
import (
"fmt"
"runtime/debug"
"strings"
)

// build vars
Expand All @@ -11,22 +12,13 @@ var (
BuildDate = ""
AppVersion = ""
Commit = ""
GoVersion = ""
)

// Returns a formatted build info string
func Long() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return fmt.Sprintf("%s: error reading debug information", AppName)
}

if GoVersion == "" {
GoVersion = info.GoVersion
}

if AppVersion == "" {
AppVersion = info.Main.Version
return fmt.Sprintf("%s: error reading build information", AppName)
}

for _, kv := range info.Settings {
Expand All @@ -39,10 +31,22 @@ func Long() string {
}
}

return fmt.Sprintf(
"%s %s (%s) built %s, %s",
AppName, AppVersion, Commit, BuildDate, GoVersion,
)
parts := []string{AppName, " ", AppVersion}
if AppVersion == "" {
parts[2] = info.Main.Version
}

if Commit != "" {
parts = append(parts, " ", fmt.Sprintf("(%s)", Commit))
}

if BuildDate != "" {
parts = append(parts, " ", fmt.Sprintf("built at %s", BuildDate))
}

parts = append(parts, ", ", info.GoVersion)

return strings.Join(parts, "")
}

func Short() string {
Expand Down
18 changes: 13 additions & 5 deletions internal/buildinfo/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@ package buildinfo

import (
"fmt"
"runtime/debug"
"testing"
"time"
)

func TestFull(t *testing.T) {

AppVersion = "2.0.0-test"
Commit = "test"
GoVersion = "1.x"
Commit = "123456"
BuildDate = time.Now().Format(time.RFC3339)

info, ok := debug.ReadBuildInfo()
if !ok {
t.Fatal("could not read debug info")
}

tests := []struct{ name, want string }{
{
"prints full info",
fmt.Sprintf(
"%s %s (%s) built %s, %s",
AppName, AppVersion, Commit, BuildDate, GoVersion,
"%s %s (%s) built at %s, %s",
AppName, AppVersion, Commit, BuildDate, info.GoVersion,
),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Long(); got != tt.want {
t.Errorf("Full() = %v, want %v", got, tt.want)
t.Errorf("Full() = %v want %v", got, tt.want)
}
})
}
Expand Down