From e68554cee9935732bac39cac25d6541233389e7b Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 10 May 2022 20:47:33 +1000 Subject: [PATCH] use go's own git version info --- main.go | 33 ++++++++++++++++++++++++++++++++- pkg/gui/layout.go | 2 +- pkg/utils/utils.go | 8 ++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index fa687aa3..0cb3dc2a 100644 --- a/main.go +++ b/main.go @@ -6,18 +6,23 @@ import ( "log" "os" "runtime" + "runtime/debug" "github.com/docker/docker/client" "github.com/go-errors/errors" "github.com/integrii/flaggy" "github.com/jesseduffield/lazydocker/pkg/app" "github.com/jesseduffield/lazydocker/pkg/config" + "github.com/jesseduffield/lazydocker/pkg/utils" "github.com/jesseduffield/yaml" + "github.com/samber/lo" ) +const DEFAULT_VERSION = "unversioned" + var ( commit string - version = "unversioned" + version = DEFAULT_VERSION date string buildSource = "unknown" @@ -27,6 +32,8 @@ var ( ) func main() { + updateBuildInfo() + info := fmt.Sprintf( "%s\nDate: %s\nBuildSource: %s\nCommit: %s\nOS: %s\nArch: %s", version, @@ -93,3 +100,27 @@ func main() { log.Fatal(fmt.Sprintf("%s\n\n%s", app.Tr.ErrorOccurred, stackTrace)) } } + +func updateBuildInfo() { + if version == DEFAULT_VERSION { + if buildInfo, ok := debug.ReadBuildInfo(); ok { + revision, ok := lo.Find(buildInfo.Settings, func(setting debug.BuildSetting) bool { + return setting.Key == "vcs.revision" + }) + if ok { + commit = revision.Value + // if lazydocker was built from source we'll show the version as the + // abbreviated commit hash + version = utils.SafeTruncate(revision.Value, 7) + } + + // if version hasn't been set we assume that neither has the date + time, ok := lo.Find(buildInfo.Settings, func(setting debug.BuildSetting) bool { + return setting.Key == "vcs.time" + }) + if ok { + date = time.Value + } + } + } +} diff --git a/pkg/gui/layout.go b/pkg/gui/layout.go index 0d97245c..99c00201 100644 --- a/pkg/gui/layout.go +++ b/pkg/gui/layout.go @@ -62,7 +62,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { g.Highlight = true width, height := g.Size() - information := "lazydocker " + gui.Config.Version + information := gui.Config.Version if gui.g.Mouse { donate := color.New(color.FgMagenta, color.Underline).Sprint(gui.Tr.Donate) information = donate + " " + information diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 828195da..cc0d54e7 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -379,3 +379,11 @@ func CloseMany(closers []io.Closer) error { } return nil } + +func SafeTruncate(str string, limit int) string { + if len(str) > limit { + return str[0:limit] + } else { + return str + } +}