version.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package version
  2. import (
  3. "flag"
  4. "fmt"
  5. "runtime/debug"
  6. "time"
  7. )
  8. // Version of the app
  9. var Version = "snapshot"
  10. // GitCommit is the GIT commit revision
  11. var GitCommit = "n/a"
  12. // Built is the built date
  13. var Built = "n/a"
  14. // ShowVersion is the flag used to print version
  15. var ShowVersion = flag.Bool("version", false, "Print version")
  16. // Print version to stdout
  17. func Print() {
  18. fmt.Printf(`Version: %s
  19. Git commit: %s
  20. Built: %s
  21. Copyright (C) 2020 Nicolas Carlier
  22. This is free software: you are free to change and redistribute it.
  23. There is NO WARRANTY, to the extent permitted by law.
  24. `, Version, GitCommit, Built)
  25. }
  26. func init() {
  27. if GitCommit != "snapshot" {
  28. return
  29. }
  30. info, ok := debug.ReadBuildInfo()
  31. if !ok {
  32. return
  33. }
  34. Version = info.Main.Version
  35. for _, kv := range info.Settings {
  36. if kv.Value == "" {
  37. continue
  38. }
  39. switch kv.Key {
  40. case "vcs.revision":
  41. GitCommit = kv.Value[:7]
  42. case "vcs.time":
  43. lastCommit, _ := time.Parse(time.RFC3339, kv.Value)
  44. Built = lastCommit.Format(time.RFC1123)
  45. }
  46. }
  47. }