resource.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. initName()
  33. initBuildDate()
  34. initVersion()
  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 initName() {
  47. if Name == "" {
  48. Name = "Backend-Server"
  49. }
  50. }
  51. func initBuildDate() {
  52. if buildDateTxt == "" {
  53. BuildTime = time.Now()
  54. return
  55. }
  56. res, err := strconv.ParseInt(buildDateTxt, 10, 64)
  57. if err != nil {
  58. panic(fmt.Sprintf("get build timestamp error: %s", err.Error()))
  59. }
  60. BuildTime = time.Unix(res, 0)
  61. }
  62. func initVersion() {
  63. ver := getDefaultVersion()
  64. if ver != "" {
  65. SemanticVersioning = ver
  66. Version = "v" + SemanticVersioning
  67. return
  68. }
  69. ver = getGitTagVersion()
  70. if ver != "" {
  71. SemanticVersioning = ver
  72. Version = "v" + SemanticVersioning
  73. return
  74. }
  75. ver = getRandomVersion()
  76. if ver != "" {
  77. SemanticVersioning = ver
  78. Version = "v" + SemanticVersioning
  79. return
  80. }
  81. panic("Get Version Failed")
  82. }
  83. func getDefaultVersion() (defVer string) {
  84. defVer = strings.TrimPrefix(strings.ToLower(version), "v")
  85. if defVer == "" || !utilsIsSemanticVersion(defVer) {
  86. return ""
  87. }
  88. return defVer
  89. }
  90. func getGitTagVersion() (gitVer string) {
  91. gitVer = strings.TrimPrefix(strings.ToLower(GitTag), "v")
  92. if GitCommitHash != "" && (GitTagCommitHash == "" || gitVer == "") {
  93. return fmt.Sprintf("0.0.0+dev-%d-%s", BuildTime.Unix(), GitCommitHash)
  94. } else if GitCommitHash != "" && GitTagCommitHash != "" && gitVer != "" && utilsIsSemanticVersion(gitVer) {
  95. if (GitCommitHash != GitTagCommitHash || strings.HasPrefix(gitVer, "0.")) && !strings.Contains(gitVer, "dev") {
  96. return gitVer + fmt.Sprintf("+dev-%d-%s", BuildTime.Unix(), GitCommitHash)
  97. }
  98. return gitVer
  99. } else {
  100. return ""
  101. }
  102. }
  103. func getRandomVersion() (randVer string) {
  104. return fmt.Sprintf("0.0.0+dev-%d-%s", BuildTime.Unix(), randomData)
  105. }
  106. func utilsClenFileData(data string) (res string) {
  107. res = utilsCheckAndRemoveBOM(data)
  108. res = strings.Replace(res, "\r", "", -1)
  109. res = strings.Split(res, "\n")[0]
  110. res = strings.TrimSpace(res)
  111. return res
  112. }