From 0dbe3c2e70f255add195d7160a2d2bc85e01caff Mon Sep 17 00:00:00 2001 From: Iaroslav Ciupin Date: Sat, 11 Feb 2023 21:18:58 +0200 Subject: [PATCH] Add git branch to version package (#151) * Add git branch to version package Signed-off-by: Iaroslav Ciupin --- version/version.go | 9 ++++++++- version/version_test.go | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/version/version.go b/version/version.go index 08536c05..edddc4c4 100644 --- a/version/version.go +++ b/version/version.go @@ -1,6 +1,7 @@ package version import ( + "fmt" "time" "github.com/sirupsen/logrus" @@ -18,12 +19,18 @@ var ( Version = "unknown" // Build timestamp BuildTime = time.Now().String() + // Git branch that was used to build the binary + GitBranch = "" ) // Use this method to log the build information for the current app. The app name should be provided. To inject the build // and version information refer to the top-level comment in this file func LogBuildInformation(appName string) { logrus.Info("------------------------------------------------------------------------") - logrus.Infof("App [%s], Version [%s], BuildSHA [%s], BuildTS [%s]", appName, Version, Build, BuildTime) + msg := fmt.Sprintf("App [%s], Version [%s], BuildSHA [%s], BuildTS [%s]", appName, Version, Build, BuildTime) + if GitBranch != "" { + msg += fmt.Sprintf(", Git Branch [%s]", GitBranch) + } + logrus.Info(msg) logrus.Info("------------------------------------------------------------------------") } diff --git a/version/version_test.go b/version/version_test.go index be7826ab..93350756 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -21,9 +21,10 @@ func TestLogBuildInformation(t *testing.T) { n := time.Now() BuildTime = n.String() + GitBranch = "main" buf := bytes.NewBufferString("") logrus.SetFormatter(dFormat{}) logrus.SetOutput(buf) LogBuildInformation("hello") - assert.Equal(t, buf.String(), fmt.Sprintf("------------------------------------------------------------------------App [hello], Version [unknown], BuildSHA [unknown], BuildTS [%s]------------------------------------------------------------------------", n.String())) + assert.Equal(t, buf.String(), fmt.Sprintf("------------------------------------------------------------------------App [hello], Version [unknown], BuildSHA [unknown], BuildTS [%s], Git Branch [main]------------------------------------------------------------------------", n.String())) }