From 1899c766cdf80cb52a48277abf9abfc6061b75e4 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Wed, 3 Apr 2024 04:13:47 -0400 Subject: [PATCH] feat: add version (#69) --- main.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/main.go b/main.go index f1a114f..3675a47 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "runtime/debug" "strings" "github.com/alecthomas/chroma/v2" @@ -28,7 +29,19 @@ const ( defaultLineHeight = 1.2 ) +var ( + // Version contains the application version number. It's set via ldflags + // when building. + Version = "" + + // CommitSHA contains the SHA of the commit that this application was built + // against. It's set via ldflags when building. + CommitSHA = "" +) + func main() { + const shaLen = 7 + var ( input string err error @@ -46,6 +59,27 @@ func main() { printErrorFatal("Invalid Usage", err) } + if len(ctx.Args) > 0 { + switch ctx.Args[0] { + + case "version": + if Version == "" { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" { + Version = info.Main.Version + } else { + Version = "unknown (built from source)" + } + } + version := fmt.Sprintf("freeze version %s", Version) + if len(CommitSHA) >= shaLen { + version += " (" + CommitSHA[:shaLen] + ")" + } + + fmt.Println(version) + os.Exit(0) + } + } + // Copy the pty output to buffer if config.Execute != "" { input, err = executeCommand(config)