version.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package main
  2. import (
  3. "fmt"
  4. "runtime/debug"
  5. )
  6. func ShowVersion() {
  7. var buildinfo *debug.BuildInfo
  8. var ok bool
  9. buildinfo, ok = debug.ReadBuildInfo()
  10. if ok {
  11. // We have build info, display it.
  12. fmt.Print("Version: ")
  13. var bs debug.BuildSetting
  14. for _, bs = range buildinfo.Settings {
  15. if bs.Key == "vcs.revision" || bs.Key == "vcs.time" {
  16. fmt.Print(bs.Value + " ")
  17. }
  18. }
  19. fmt.Println()
  20. }
  21. }
  22. func GetVersion() (string, string) {
  23. var buildinfo *debug.BuildInfo
  24. var ok bool
  25. var results string
  26. buildinfo, ok = debug.ReadBuildInfo()
  27. if ok {
  28. // We have build info, display it.
  29. goversion := buildinfo.GoVersion
  30. var bs debug.BuildSetting
  31. for _, bs = range buildinfo.Settings {
  32. if bs.Key == "vcs.revision" || bs.Key == "vcs.time" {
  33. results += bs.Value + " "
  34. }
  35. }
  36. return goversion, results
  37. }
  38. return "", ""
  39. }
  40. func GetModules() map[string]string {
  41. var buildinfo *debug.BuildInfo
  42. var ok bool
  43. var results map[string]string = make(map[string]string)
  44. buildinfo, ok = debug.ReadBuildInfo()
  45. if ok {
  46. // We have Module info, return it.
  47. for _, mod := range buildinfo.Deps {
  48. results[mod.Path] = mod.Version
  49. }
  50. }
  51. return results
  52. }