Skip to content

Commit b2b9c71

Browse files
committed
Add build date to version banner
1 parent 811b732 commit b2b9c71

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

internal/templates/game.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@
454454
rel="noopener"
455455
>{{COMMIT}}</a
456456
>
457+
({{BUILD_DATE}})
457458
</div>
458459
</footer>
459460
<script

internal/templates/home.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ <h2>Recent games (this browser)</h2>
271271
rel="noopener"
272272
>{{COMMIT}}</a
273273
>
274+
({{BUILD_DATE}})
274275
</div>
275276
</footer>
276277
<script

internal/templates/templates.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import (
88
)
99

1010
var commit = "dev"
11+
var buildDate = ""
1112

12-
func SetCommit(c string) { commit = c }
13+
func SetVersion(c, d string) {
14+
commit = c
15+
buildDate = d
16+
}
1317

1418
// WriteHomeHTML serves the home page template
1519
func WriteHomeHTML(w http.ResponseWriter) {
@@ -23,6 +27,7 @@ func WriteHomeHTML(w http.ResponseWriter) {
2327
return
2428
}
2529
html := strings.ReplaceAll(string(content), "{{COMMIT}}", commit)
30+
html = strings.ReplaceAll(html, "{{BUILD_DATE}}", buildDate)
2631
_, _ = w.Write([]byte(html))
2732
}
2833

@@ -40,6 +45,7 @@ func WriteGameHTML(w http.ResponseWriter, gameID string) {
4045

4146
html := strings.ReplaceAll(string(content), "{{GAME_ID}}", gameID)
4247
html = strings.ReplaceAll(html, "{{COMMIT}}", commit)
48+
html = strings.ReplaceAll(html, "{{BUILD_DATE}}", buildDate)
4349
_, _ = w.Write([]byte(html))
4450
}
4551

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func main() {
1818
flag.Parse()
1919
logging.Debug = *debug
2020

21-
templates.SetCommit(commit)
21+
templates.SetVersion(commit, buildDate)
2222

2323
if dsn := os.Getenv("DATABASE_URL"); dsn != "" {
2424
if _, err := storage.New(dsn); err != nil {

version.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,38 @@ import (
44
"os/exec"
55
"runtime/debug"
66
"strings"
7+
"time"
78
)
89

910
var commit = "dev"
11+
var buildDate = ""
1012

1113
func init() {
12-
if commit != "dev" {
13-
return
14-
}
1514
if info, ok := debug.ReadBuildInfo(); ok {
1615
for _, s := range info.Settings {
17-
if s.Key == "vcs.revision" && s.Value != "" {
18-
commit = s.Value
19-
if len(commit) > 7 {
20-
commit = commit[:7]
16+
switch s.Key {
17+
case "vcs.revision":
18+
if commit == "dev" && s.Value != "" {
19+
commit = s.Value
20+
if len(commit) > 7 {
21+
commit = commit[:7]
22+
}
23+
}
24+
case "vcs.time":
25+
if buildDate == "" && s.Value != "" {
26+
if t, err := time.Parse(time.RFC3339, s.Value); err == nil {
27+
buildDate = t.Format("1-2-2006")
28+
}
2129
}
22-
return
2330
}
2431
}
2532
}
26-
if c, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output(); err == nil {
27-
commit = strings.TrimSpace(string(c))
33+
if commit == "dev" {
34+
if c, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output(); err == nil {
35+
commit = strings.TrimSpace(string(c))
36+
}
37+
}
38+
if buildDate == "" {
39+
buildDate = time.Now().Format("1-2-2006")
2840
}
2941
}

0 commit comments

Comments
 (0)