Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit 0dbe3c2

Browse files
Add git branch to version package (#151)
* Add git branch to version package Signed-off-by: Iaroslav Ciupin <[email protected]>
1 parent 82d114b commit 0dbe3c2

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

version/version.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package version
22

33
import (
4+
"fmt"
45
"time"
56

67
"github.com/sirupsen/logrus"
@@ -18,12 +19,18 @@ var (
1819
Version = "unknown"
1920
// Build timestamp
2021
BuildTime = time.Now().String()
22+
// Git branch that was used to build the binary
23+
GitBranch = ""
2124
)
2225

2326
// Use this method to log the build information for the current app. The app name should be provided. To inject the build
2427
// and version information refer to the top-level comment in this file
2528
func LogBuildInformation(appName string) {
2629
logrus.Info("------------------------------------------------------------------------")
27-
logrus.Infof("App [%s], Version [%s], BuildSHA [%s], BuildTS [%s]", appName, Version, Build, BuildTime)
30+
msg := fmt.Sprintf("App [%s], Version [%s], BuildSHA [%s], BuildTS [%s]", appName, Version, Build, BuildTime)
31+
if GitBranch != "" {
32+
msg += fmt.Sprintf(", Git Branch [%s]", GitBranch)
33+
}
34+
logrus.Info(msg)
2835
logrus.Info("------------------------------------------------------------------------")
2936
}

version/version_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ func TestLogBuildInformation(t *testing.T) {
2121

2222
n := time.Now()
2323
BuildTime = n.String()
24+
GitBranch = "main"
2425
buf := bytes.NewBufferString("")
2526
logrus.SetFormatter(dFormat{})
2627
logrus.SetOutput(buf)
2728
LogBuildInformation("hello")
28-
assert.Equal(t, buf.String(), fmt.Sprintf("------------------------------------------------------------------------App [hello], Version [unknown], BuildSHA [unknown], BuildTS [%s]------------------------------------------------------------------------", n.String()))
29+
assert.Equal(t, buf.String(), fmt.Sprintf("------------------------------------------------------------------------App [hello], Version [unknown], BuildSHA [unknown], BuildTS [%s], Git Branch [main]------------------------------------------------------------------------", n.String()))
2930
}

0 commit comments

Comments
 (0)