Skip to content

Commit

Permalink
use go's own git version info
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed May 10, 2022
1 parent 14cb013 commit e68554c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
33 changes: 32 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -27,6 +32,8 @@ var (
)

func main() {
updateBuildInfo()

info := fmt.Sprintf(
"%s\nDate: %s\nBuildSource: %s\nCommit: %s\nOS: %s\nArch: %s",
version,
Expand Down Expand Up @@ -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
}
}
}
}
2 changes: 1 addition & 1 deletion pkg/gui/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit e68554c

Please sign in to comment.