version.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package ircclient
  2. import (
  3. "fmt"
  4. "runtime/debug"
  5. )
  6. // Get build version from BuildInfo
  7. func ShowVersion() {
  8. var buildinfo *debug.BuildInfo
  9. var ok bool
  10. buildinfo, ok = debug.ReadBuildInfo()
  11. if ok {
  12. // We have build info, display it.
  13. fmt.Print("Version: ")
  14. var bs debug.BuildSetting
  15. for _, bs = range buildinfo.Settings {
  16. if bs.Key == "vcs.revision" || bs.Key == "vcs.time" {
  17. fmt.Print(bs.Value + " ")
  18. }
  19. }
  20. fmt.Println()
  21. }
  22. }
  23. // Get Go version, arch, and OS. Get git commit hash.
  24. func GetVersion() (goversion string, gitcommit string, arch string, goos string) {
  25. var buildinfo *debug.BuildInfo
  26. var ok bool
  27. var results string
  28. var garch, gos string
  29. buildinfo, ok = debug.ReadBuildInfo()
  30. if ok {
  31. // We have build info, display it.
  32. goversion := buildinfo.GoVersion
  33. var bs debug.BuildSetting
  34. for _, bs = range buildinfo.Settings {
  35. if bs.Key == "vcs.revision" || bs.Key == "vcs.time" {
  36. results += bs.Value + " "
  37. }
  38. if bs.Key == "GOARCH" {
  39. garch = bs.Value
  40. }
  41. if bs.Key == "GOOS" {
  42. gos = bs.Value
  43. }
  44. }
  45. return goversion, results, garch, gos
  46. }
  47. return "", "", "", ""
  48. }
  49. // Get modules information
  50. //
  51. // Return map of module name and version.
  52. func GetModules() map[string]string {
  53. var buildinfo *debug.BuildInfo
  54. var ok bool
  55. var results map[string]string = make(map[string]string)
  56. buildinfo, ok = debug.ReadBuildInfo()
  57. if ok {
  58. // We have Module info, return it.
  59. for _, mod := range buildinfo.Deps {
  60. results[mod.Path] = mod.Version
  61. }
  62. }
  63. return results
  64. }