use go's own git version info

pull/328/head
Jesse Duffield 2 years ago
parent 14cb013f5d
commit e68554cee9

@ -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
}
}
}
}

@ -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

@ -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
}
}

Loading…
Cancel
Save