resource.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package resource
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "github.com/SongZihuan/BackendServerTemplate/src/utils/reutils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. //go:embed VERSION
  11. var version string
  12. var Version string
  13. var SemanticVersioning string
  14. //go:embed LICENSE
  15. var License string
  16. //go:embed REPORT
  17. var Report string
  18. //go:embed NAME
  19. var Name string
  20. //go:embed build_date.txt
  21. var buildDateTxt string
  22. var BuildTime time.Time
  23. //go:embed commit_data.txt
  24. var GitCommitHash string
  25. //go:embed tag_data.txt
  26. var GitTag string
  27. //go:embed tag_commit_data.txt
  28. var GitTagCommitHash string
  29. func init() {
  30. initCleanFile()
  31. initName()
  32. initBuildDate()
  33. initVersion()
  34. }
  35. func initCleanFile() {
  36. License = utilsClenFileDataMoreLine(License)
  37. Report = utilsClenFileDataMoreLine(Report)
  38. version = utilsClenFileData(version)
  39. Name = utilsClenFileData(Name)
  40. buildDateTxt = utilsClenFileData(buildDateTxt)
  41. GitCommitHash = utilsClenFileData(GitCommitHash)
  42. GitTag = utilsClenFileData(GitTag)
  43. GitTagCommitHash = utilsClenFileData(GitTagCommitHash)
  44. }
  45. func initName() {
  46. if Name == "" {
  47. Name = "Backend-Server"
  48. }
  49. }
  50. func initBuildDate() {
  51. if buildDateTxt == "" {
  52. BuildTime = time.Now()
  53. return
  54. }
  55. res, err := strconv.ParseInt(buildDateTxt, 10, 64)
  56. if err != nil {
  57. panic(fmt.Sprintf("get build timestamp error: %s", err.Error()))
  58. }
  59. BuildTime = time.Unix(res, 0)
  60. }
  61. func initVersion() {
  62. SemanticVersioning = strings.TrimPrefix(strings.ToLower(version), "v")
  63. if SemanticVersioning == "" {
  64. SemanticVersioning = strings.TrimPrefix(strings.ToLower(GitTag), "v")
  65. if SemanticVersioning == "" {
  66. if GitCommitHash != "" {
  67. SemanticVersioning = fmt.Sprintf("0.0.0+dev-%d-%s", BuildTime.Unix(), GitCommitHash)
  68. } else {
  69. SemanticVersioning = fmt.Sprintf("0.0.0+dev-%d", BuildTime.Unix())
  70. }
  71. Version = "v" + SemanticVersioning
  72. } else if reutils.IsSemanticVersion(SemanticVersioning) {
  73. if GitCommitHash != "" && GitTagCommitHash != "" && GitCommitHash != GitTagCommitHash && !strings.Contains(SemanticVersioning, "dev") {
  74. SemanticVersioning = SemanticVersioning + fmt.Sprintf("+dev-%s", GitTagCommitHash)
  75. } else if strings.HasPrefix(SemanticVersioning, "0.") {
  76. SemanticVersioning = SemanticVersioning + "-dev"
  77. }
  78. Version = "v" + SemanticVersioning
  79. } else {
  80. panic(fmt.Sprintf("%s is not a semantic versioning.", SemanticVersioning))
  81. }
  82. } else if reutils.IsSemanticVersion(SemanticVersioning) {
  83. Version = "v" + SemanticVersioning
  84. } else {
  85. panic(fmt.Sprintf("%s is not a semantic versioning.", SemanticVersioning))
  86. }
  87. }
  88. func utilsClenFileData(data string) (res string) {
  89. res = utilsCheckAndRemoveBOM(data)
  90. res = strings.Replace(res, "\r", "", -1)
  91. res = strings.Split(res, "\n")[0]
  92. res = strings.TrimSpace(res)
  93. return res
  94. }
  95. func utilsClenFileDataMoreLine(data string) (res string) {
  96. res = utilsCheckAndRemoveBOM(data)
  97. res = strings.Replace(res, "\r", "", -1)
  98. return res
  99. }
  100. func utilsCheckAndRemoveBOM(s string) string {
  101. // UTF-8 BOM 的字节序列为 0xEF, 0xBB, 0xBF
  102. bom := []byte{0xEF, 0xBB, 0xBF}
  103. // 将字符串转换为字节切片
  104. bytes := []byte(s)
  105. // 检查前三个字节是否是 BOM
  106. if len(bytes) >= 3 && bytes[0] == bom[0] && bytes[1] == bom[1] && bytes[2] == bom[2] {
  107. // 如果存在 BOM,则删除它
  108. return string(bytes[3:])
  109. }
  110. // 如果不存在 BOM,则返回原始字符串
  111. return s
  112. }