resource.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package resource
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. //go:embed VERSION
  10. var version string
  11. var Version string
  12. var SemanticVersioning string
  13. //go:embed LICENSE
  14. var License string
  15. //go:embed REPORT
  16. var Report string
  17. //go:embed NAME
  18. var Name string
  19. //go:embed build_date.txt
  20. var buildDateTxt string
  21. var BuildTime time.Time
  22. //go:embed commit_data.txt
  23. var GitCommitHash string
  24. //go:embed tag_data.txt
  25. var GitTag string
  26. //go:embed tag_commit_data.txt
  27. var GitTagCommitHash string
  28. //go:embed random_data.txt
  29. var randomData string
  30. func init() {
  31. initCleanFile()
  32. initBuildDate()
  33. initVersion()
  34. initServiceConfig()
  35. }
  36. func initCleanFile() {
  37. License = utilsClenFileDataMoreLine(License)
  38. Report = utilsClenFileDataMoreLine(Report)
  39. version = utilsClenFileData(version)
  40. Name = utilsClenFileData(Name)
  41. buildDateTxt = utilsClenFileData(buildDateTxt)
  42. GitCommitHash = utilsClenFileData(GitCommitHash)
  43. GitTag = utilsClenFileData(GitTag)
  44. GitTagCommitHash = utilsClenFileData(GitTagCommitHash)
  45. }
  46. func initBuildDate() {
  47. if buildDateTxt == "" {
  48. BuildTime = time.Now()
  49. return
  50. }
  51. res, err := strconv.ParseInt(buildDateTxt, 10, 64)
  52. if err != nil {
  53. panic(fmt.Sprintf("get build timestamp error: %s", err.Error()))
  54. }
  55. BuildTime = time.Unix(res, 0)
  56. }
  57. func initVersion() {
  58. ver := getDefaultVersion()
  59. if ver != "" {
  60. SemanticVersioning = ver
  61. Version = "v" + SemanticVersioning
  62. return
  63. }
  64. ver = getGitTagVersion()
  65. if ver != "" {
  66. SemanticVersioning = ver
  67. Version = "v" + SemanticVersioning
  68. return
  69. }
  70. ver = getRandomVersion()
  71. if ver != "" {
  72. SemanticVersioning = ver
  73. Version = "v" + SemanticVersioning
  74. return
  75. }
  76. panic("Get Version Failed")
  77. }
  78. func getDefaultVersion() (defVer string) {
  79. defVer = strings.TrimPrefix(strings.ToLower(version), "v")
  80. if defVer == "" || !utilsIsSemanticVersion(defVer) {
  81. return ""
  82. }
  83. return defVer
  84. }
  85. func getGitTagVersion() (gitVer string) {
  86. gitVer = strings.TrimPrefix(strings.ToLower(GitTag), "v")
  87. if GitCommitHash != "" && (GitTagCommitHash == "" || gitVer == "") {
  88. return fmt.Sprintf("0.0.0+dev-%d-%s", BuildTime.Unix(), GitCommitHash)
  89. } else if GitCommitHash != "" && GitTagCommitHash != "" && gitVer != "" && utilsIsSemanticVersion(gitVer) {
  90. if (GitCommitHash != GitTagCommitHash || strings.HasPrefix(gitVer, "0.")) && !strings.Contains(gitVer, "dev") {
  91. return gitVer + fmt.Sprintf("+dev-%d-%s", BuildTime.Unix(), GitCommitHash)
  92. }
  93. return gitVer
  94. } else {
  95. return ""
  96. }
  97. }
  98. func getRandomVersion() (randVer string) {
  99. return fmt.Sprintf("0.0.0+dev-%d-%s", BuildTime.Unix(), randomData)
  100. }
  101. func utilsClenFileData(data string) (res string) {
  102. res = utilsCheckAndRemoveBOM(data)
  103. res = strings.Replace(res, "\r", "", -1)
  104. res = strings.Split(res, "\n")[0]
  105. res = strings.TrimSpace(res)
  106. return res
  107. }