changelog_data.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package changelog
  5. import (
  6. "github.com/SongZihuan/BackendServerTemplate/tool/utils/cleanstringutils"
  7. "os"
  8. "strings"
  9. "sync"
  10. )
  11. const FileChangelog = "./CHANGELOG.md"
  12. var lastChangeLog = ""
  13. var once sync.Once
  14. func GetLastChangLog() string {
  15. once.Do(func() {
  16. lastChangeLog = getLastChangLog()
  17. })
  18. return lastChangeLog
  19. }
  20. func getLastChangLog() string {
  21. dat, err := os.ReadFile(FileChangelog)
  22. if err != nil {
  23. panic(err)
  24. }
  25. logSrc := strings.Split(cleanstringutils.GetString(string(dat)), "\n")
  26. res := new(strings.Builder)
  27. index := 0
  28. // 定位最新版本
  29. FindVersionCycle:
  30. for {
  31. if index >= len(logSrc) {
  32. panic("Error CHANGELOG.md: can not find the log")
  33. }
  34. s := logSrc[index]
  35. if strings.HasPrefix(s, "## [") && !strings.HasPrefix(s, "## [未") {
  36. res.WriteString(s + "\n")
  37. break FindVersionCycle
  38. }
  39. }
  40. GetVersionLogCycle:
  41. for {
  42. if index >= len(logSrc) {
  43. break GetVersionLogCycle
  44. }
  45. s := logSrc[index]
  46. if strings.HasPrefix(s, "## [") {
  47. break GetVersionLogCycle
  48. }
  49. res.WriteString(s + "\n")
  50. }
  51. return res.String()
  52. }