Skip to content

Commit c776ac4

Browse files
authored
fix buildinfo print (#51)
1 parent e6853be commit c776ac4

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

internal/buildinfo/info.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package buildinfo
33
import (
44
"fmt"
55
"runtime/debug"
6+
"strings"
67
)
78

89
// build vars
@@ -11,22 +12,13 @@ var (
1112
BuildDate = ""
1213
AppVersion = ""
1314
Commit = ""
14-
GoVersion = ""
1515
)
1616

1717
// Returns a formatted build info string
1818
func Long() string {
1919
info, ok := debug.ReadBuildInfo()
2020
if !ok {
21-
return fmt.Sprintf("%s: error reading debug information", AppName)
22-
}
23-
24-
if GoVersion == "" {
25-
GoVersion = info.GoVersion
26-
}
27-
28-
if AppVersion == "" {
29-
AppVersion = info.Main.Version
21+
return fmt.Sprintf("%s: error reading build information", AppName)
3022
}
3123

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

42-
return fmt.Sprintf(
43-
"%s %s (%s) built %s, %s",
44-
AppName, AppVersion, Commit, BuildDate, GoVersion,
45-
)
34+
parts := []string{AppName, " ", AppVersion}
35+
if AppVersion == "" {
36+
parts[2] = info.Main.Version
37+
}
38+
39+
if Commit != "" {
40+
parts = append(parts, " ", fmt.Sprintf("(%s)", Commit))
41+
}
42+
43+
if BuildDate != "" {
44+
parts = append(parts, " ", fmt.Sprintf("built at %s", BuildDate))
45+
}
46+
47+
parts = append(parts, ", ", info.GoVersion)
48+
49+
return strings.Join(parts, "")
4650
}
4751

4852
func Short() string {

internal/buildinfo/info_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@ package buildinfo
22

33
import (
44
"fmt"
5+
"runtime/debug"
56
"testing"
7+
"time"
68
)
79

810
func TestFull(t *testing.T) {
11+
912
AppVersion = "2.0.0-test"
10-
Commit = "test"
11-
GoVersion = "1.x"
13+
Commit = "123456"
14+
BuildDate = time.Now().Format(time.RFC3339)
15+
16+
info, ok := debug.ReadBuildInfo()
17+
if !ok {
18+
t.Fatal("could not read debug info")
19+
}
1220

1321
tests := []struct{ name, want string }{
1422
{
1523
"prints full info",
1624
fmt.Sprintf(
17-
"%s %s (%s) built %s, %s",
18-
AppName, AppVersion, Commit, BuildDate, GoVersion,
25+
"%s %s (%s) built at %s, %s",
26+
AppName, AppVersion, Commit, BuildDate, info.GoVersion,
1927
),
2028
},
2129
}
2230

2331
for _, tt := range tests {
2432
t.Run(tt.name, func(t *testing.T) {
2533
if got := Long(); got != tt.want {
26-
t.Errorf("Full() = %v, want %v", got, tt.want)
34+
t.Errorf("Full() = %v want %v", got, tt.want)
2735
}
2836
})
2937
}

0 commit comments

Comments
 (0)