feat: Add buildinfo package to embed version (#840)

This also resolves build time and commit hash using the
Go 1.18 debug/buildinfo package. An external URL is outputted
on running version as well to easily route the caller to a
release or commit.
This commit is contained in:
Kyle Carberry
2022-04-05 01:35:03 +00:00
committed by GitHub
parent d4e26ff8c2
commit 5ae71f0958
6 changed files with 137 additions and 3 deletions
+15
View File
@@ -4,12 +4,14 @@ import (
"net/url"
"os"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/kirsle/configdir"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"github.com/coder/coder/cli/buildinfo"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/cli/config"
"github.com/coder/coder/codersdk"
@@ -28,6 +30,7 @@ const (
func Root() *cobra.Command {
cmd := &cobra.Command{
Use: "coder",
Version: buildinfo.Version(),
SilenceUsage: true,
Long: ` ▄█▀ ▀█▄
▄▄ ▀▀▀ █▌ ██▀▀█▄ ▐█
@@ -55,6 +58,7 @@ func Root() *cobra.Command {
`Flags:`, header.Render("Flags:"),
`Additional help topics:`, header.Render("Additional help:"),
).Replace(cmd.UsageTemplate()))
cmd.SetVersionTemplate(versionTemplate())
cmd.AddCommand(
configSSH(),
@@ -142,3 +146,14 @@ func isTTY(cmd *cobra.Command) bool {
}
return isatty.IsTerminal(file.Fd())
}
func versionTemplate() string {
template := `Coder {{printf "%s" .Version}}`
buildTime, valid := buildinfo.Time()
if valid {
template += " " + buildTime.Format(time.UnixDate)
}
template += "\r\n" + buildinfo.ExternalURL()
template += "\r\n"
return template
}