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:
parent
fa13957511
commit
7344ea2e86
2 changed files with 20 additions and 1 deletions
19
main.go
19
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 `<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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue