Skip to content

Commit

Permalink
cmd/image-builder/main: restructure gitRev for reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
schuellerf committed Sep 5, 2024
1 parent def1eb8 commit 96e5ee2
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions cmd/image-builder/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"runtime/debug"

Expand Down Expand Up @@ -29,19 +30,18 @@ import (
"github.com/sirupsen/logrus"
)

func gitRev() string {
// gitRev returns the gitHash of the current running binary
func gitRev() (string, error) {
info, ok := debug.ReadBuildInfo()
if !ok {
logrus.Warn("cannot read build info")
return "unknown"
return "", errors.New("cannot read build info")
}
for _, bs := range info.Settings {
if bs.Key == "vcs.revision" {
return bs.Value
return bs.Value, nil
}
}
logrus.Warn("vcs.revision not found in debug.ReadBuildInfo()")
return "unknown"
return "", errors.New("vcs.revision not found in debug.ReadBuildInfo()")
}

func main() {
Expand Down Expand Up @@ -76,7 +76,12 @@ func main() {
}
logrus.AddHook(&ctxHook{})

gitRev := gitRev()
gitRev, err := gitRev()
if err != nil {
logrus.Warn(err.Error())
gitRev = "unknown"
}

logrus.Infof("Starting image-builder from Git Hash: %s", gitRev)
logrus.Infof("Changelog: https://github.com/osbuild/image-builder/commits/%s", gitRev)

Expand Down

0 comments on commit 96e5ee2

Please sign in to comment.