version.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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() (goversion string, gitcommit string, arch string, goos string) {
  23. var buildinfo *debug.BuildInfo
  24. var ok bool
  25. var results string
  26. var garch, gos string
  27. buildinfo, ok = debug.ReadBuildInfo()
  28. if ok {
  29. // We have build info, display it.
  30. goversion := buildinfo.GoVersion
  31. var bs debug.BuildSetting
  32. for _, bs = range buildinfo.Settings {
  33. if bs.Key == "vcs.revision" || bs.Key == "vcs.time" {
  34. results += bs.Value + " "
  35. }
  36. if bs.Key == "GOARCH" {
  37. garch = bs.Value
  38. }
  39. if bs.Key == "GOOS" {
  40. gos = bs.Value
  41. }
  42. }
  43. return goversion, results, garch, gos
  44. }
  45. return "", "", "", ""
  46. }
  47. func GetModules() map[string]string {
  48. var buildinfo *debug.BuildInfo
  49. var ok bool
  50. var results map[string]string = make(map[string]string)
  51. buildinfo, ok = debug.ReadBuildInfo()
  52. if ok {
  53. // We have Module info, return it.
  54. for _, mod := range buildinfo.Deps {
  55. results[mod.Path] = mod.Version
  56. }
  57. }
  58. return results
  59. }