From 7344ea2e865e69bb69eb9441723387f173ea3cf8 Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Sun, 27 Mar 2022 01:16:01 +0100 Subject: [PATCH] Add version flag go 1.18 embeds vcs info into binaries by default [1] allowing us to provide a rudimentary version cmd in just a few lines. (version) vcs tags are not embedded for now - just the commit hash should be good enough for now though. [1] https://tip.golang.org/doc/go1.18 --- go.mod | 2 +- main.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 83b1fde..ea085ed 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/niklasfasching/go-org -go 1.17 +go 1.18 require ( github.com/alecthomas/chroma v0.10.0 diff --git a/main.go b/main.go index 94b710f..4cb20fa 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "os" + "runtime/debug" "strings" "github.com/alecthomas/chroma" @@ -37,6 +38,8 @@ func main() { render(args) case "blorg": runBlorg(args) + case "version": + printVersion() default: log.Fatal(usage) } @@ -130,3 +133,19 @@ func highlightCodeBlock(source, lang string, inline bool) string { } return `
` + "\n" + w.String() + "\n" + `
` } + +func printVersion() { + bi, ok := debug.ReadBuildInfo() + if !ok { + log.Fatal("not build info available") + } + revision, modified := "", false + for _, s := range bi.Settings { + if s.Key == "vcs.revision" { + revision = s.Value + } else if s.Key == "vcs.modified" { + modified = s.Value == "true" + } + } + log.Printf("%s (modified: %v)", revision, modified) +}