Add version info to help output

This commit is contained in:
Darell Tan
2024-11-22 19:16:13 +08:00
parent 1922f57a6f
commit 9404fa6161

View File

@@ -12,7 +12,9 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"path/filepath"
"regexp" "regexp"
"runtime/debug"
"syscall" "syscall"
) )
@@ -65,7 +67,40 @@ func parseConfig(fname string) (cfg *config, err error) {
return return
} }
func readVcsRevision() string {
ver := ""
dirty := false
if info, ok := debug.ReadBuildInfo(); ok {
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
if len(s.Value) >= 8 {
ver = s.Value[:8]
} else {
ver = s.Value
}
case "vcs.modified":
dirty = true
}
}
if dirty {
ver += "-dirty"
}
return ver
}
return "?"
}
func main() { func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "hapz2m version %s\n"+
"HomeKit <-> zigbee2mqtt Bridge\n"+
"\nUsage: %s [options...]\n",
readVcsRevision(), filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse() flag.Parse()
// check if we are running under systemd, and if so, dont output timestamps // check if we are running under systemd, and if so, dont output timestamps