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
This commit is contained in:
Niklas Fasching 2022-03-27 01:16:01 +01:00
parent fa13957511
commit 7344ea2e86
2 changed files with 20 additions and 1 deletions

2
go.mod
View file

@ -1,6 +1,6 @@
module github.com/niklasfasching/go-org module github.com/niklasfasching/go-org
go 1.17 go 1.18
require ( require (
github.com/alecthomas/chroma v0.10.0 github.com/alecthomas/chroma v0.10.0

19
main.go
View file

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"runtime/debug"
"strings" "strings"
"github.com/alecthomas/chroma" "github.com/alecthomas/chroma"
@ -37,6 +38,8 @@ func main() {
render(args) render(args)
case "blorg": case "blorg":
runBlorg(args) runBlorg(args)
case "version":
printVersion()
default: default:
log.Fatal(usage) log.Fatal(usage)
} }
@ -130,3 +133,19 @@ func highlightCodeBlock(source, lang string, inline bool) string {
} }
return `<div class="highlight">` + "\n" + w.String() + "\n" + `</div>` return `<div class="highlight">` + "\n" + w.String() + "\n" + `</div>`
} }
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)
}