version.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package version
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "runtime"
  19. "strings"
  20. )
  21. // Info contains versioning information.
  22. // TODO: Add []string of api versions supported? It's still unclear
  23. // how we'll want to distribute that information.
  24. type Info struct {
  25. Major string `json:"major"`
  26. Minor string `json:"minor"`
  27. GitVersion string `json:"gitVersion"`
  28. GitBranch string `json:"gitBranch"`
  29. GitCommit string `json:"gitCommit"`
  30. GitTreeState string `json:"gitTreeState"`
  31. BuildDate string `json:"buildDate"`
  32. GoVersion string `json:"goVersion"`
  33. Compiler string `json:"compiler"`
  34. Platform string `json:"platform"`
  35. }
  36. // String returns info as a human-friendly version string.
  37. func (info Info) String() string {
  38. return info.GitVersion
  39. }
  40. // Get returns the overall codebase version. It's for detecting
  41. // what code a binary was built from.
  42. func Get() Info {
  43. // These variables typically come from -ldflags settings and in
  44. // their absence fallback to the settings in pkg/version/base.go
  45. return Info{
  46. Major: gitMajor,
  47. Minor: gitMinor,
  48. GitVersion: gitVersion,
  49. GitBranch: gitBranch,
  50. GitCommit: gitCommit,
  51. GitTreeState: gitTreeState,
  52. BuildDate: buildDate,
  53. GoVersion: runtime.Version(),
  54. Compiler: runtime.Compiler,
  55. Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
  56. }
  57. }
  58. func GetJsonString() string {
  59. bs, _ := json.MarshalIndent(Get(), "", " ")
  60. return string(bs)
  61. }
  62. func shortDate(dateStr string) string {
  63. var buf strings.Builder
  64. for _, c := range dateStr {
  65. if c >= '0' && c <= '9' {
  66. buf.WriteRune(c)
  67. }
  68. }
  69. dateStr = buf.String()
  70. if strings.HasPrefix(dateStr, "20") {
  71. dateStr = dateStr[2:]
  72. }
  73. if len(dateStr) > 8 {
  74. dateStr = dateStr[:8]
  75. }
  76. return dateStr
  77. }
  78. func GetShortString() string {
  79. v := Get()
  80. return fmt.Sprintf("%s(%s%s)", v.GitBranch, v.GitCommit, shortDate(v.BuildDate))
  81. }