version.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package version
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gosuri/uitable"
  6. "runtime"
  7. )
  8. var (
  9. // GitVersion is semantic version.
  10. GitVersion = "v0.0.0-master+$Format:%h$"
  11. // BuildDate in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ').
  12. BuildDate = "1970-01-01T00:00:00Z"
  13. // GitCommit sha1 from git, output of $(git rev-parse HEAD).
  14. GitCommit = "$Format:%H$"
  15. // GitTreeState state of git tree, either "clean" or "dirty".
  16. GitTreeState = ""
  17. )
  18. type Info struct {
  19. GitVersion string `json:"gitVersion"`
  20. GitCommit string `json:"gitCommit"`
  21. GitTreeState string `json:"gitTreeState"`
  22. BuildDate string `json:"buildDate"`
  23. GoVersion string `json:"goVersion"`
  24. Compiler string `json:"compiler"`
  25. Platform string `json:"platform"`
  26. }
  27. func (info Info) String() string {
  28. if s, err := info.Text(); err == nil {
  29. return string(s)
  30. }
  31. return info.GitVersion
  32. }
  33. func (info Info) ToJSON() string {
  34. s, _ := json.Marshal(info)
  35. return string(s)
  36. }
  37. func (info Info) Text() ([]byte, error) {
  38. table := uitable.New()
  39. table.RightAlign(0)
  40. table.MaxColWidth = 80
  41. table.Separator = " "
  42. table.AddRow("gitVersion:", info.GitVersion)
  43. table.AddRow("gitCommit:", info.GitCommit)
  44. table.AddRow("gitTreeState:", info.GitTreeState)
  45. table.AddRow("buildDate:", info.BuildDate)
  46. table.AddRow("goVersion:", info.GoVersion)
  47. table.AddRow("compiler:", info.Compiler)
  48. table.AddRow("platform:", info.Platform)
  49. return table.Bytes(), nil
  50. }
  51. func Get() Info {
  52. return Info{
  53. GitVersion: GitVersion,
  54. GitCommit: GitCommit,
  55. GitTreeState: GitTreeState,
  56. BuildDate: BuildDate,
  57. GoVersion: runtime.Version(),
  58. Compiler: runtime.Compiler,
  59. Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
  60. }
  61. }