Skip to content

Instantly share code, notes, and snippets.

@linnv
Last active September 2, 2017 23:34
Show Gist options
  • Save linnv/70b44b2b1326f8f27cfe337c24e687df to your computer and use it in GitHub Desktop.
Save linnv/70b44b2b1326f8f27cfe337c24e687df to your computer and use it in GitHub Desktop.
auto update app version from git for Golang
package main
import (
"flag"
"os"
"strings"
)
var (
Version = "1.0.0"
BuildTime = "2017-08-23_11:44:38"
)
//go build -ldflags "-X 'main.Version=`git rev-parse HEAD`' -X 'main.BuildTime=`date '+%Y-%m-%d_%H:%M:%S'`' " main.go
func VersionGuide() {
if !flag.Parsed() {
os.Stderr.Write([]byte("ERROR: do flag.Parse() first!"))
return
}
args := flag.Args()
versionList := [...]string{
"V", "-V", "--V",
"VERSION", "-VERSION", "--VERSION",
}
buildTimeList := [...]string{
"T", "-T", "--T",
"BUILDTIME", "-BUILDTIME", "--BUILDTIME",
}
for _, v := range args {
upperV := strings.ToUpper(v)
for _, ver := range versionList {
if ver == upperV {
os.Stdout.Write([]byte("Version: " + Version + "\n"))
}
}
for _, ver := range buildTimeList {
if ver == upperV {
os.Stdout.Write([]byte("BuildTime: " + BuildTime + "\n"))
}
}
}
}
func main() {
flag.Parse()
VersionGuide()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment