update.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package update
  2. /*
  3. Update.go
  4. This module handle cross version updates that contains breaking changes
  5. update command should always exit after the update is completed
  6. */
  7. import (
  8. "fmt"
  9. "os"
  10. "strconv"
  11. "strings"
  12. v308 "imuslab.com/zoraxy/mod/update/v308"
  13. "imuslab.com/zoraxy/mod/utils"
  14. )
  15. // Run config update. Version numbers are int. For example
  16. // to update 3.0.7 to 3.0.8, use RunConfigUpdate(307, 308)
  17. // This function support cross versions updates (e.g. 307 -> 310)
  18. func RunConfigUpdate(fromVersion int, toVersion int) {
  19. versionFile := "./conf/version"
  20. if fromVersion == 0 {
  21. //Run auto previous version detection
  22. fromVersion = 307
  23. if utils.FileExists(versionFile) {
  24. //Read the version file
  25. previousVersionText, err := os.ReadFile(versionFile)
  26. if err != nil {
  27. panic("Unable to read version file at " + versionFile)
  28. }
  29. //Convert the version to int
  30. versionInt, err := strconv.Atoi(strings.TrimSpace(string(previousVersionText)))
  31. if err != nil {
  32. panic("Unable to read version file at " + versionFile)
  33. }
  34. fromVersion = versionInt
  35. }
  36. if fromVersion == toVersion {
  37. //No need to update
  38. return
  39. }
  40. }
  41. //Do iterate update
  42. for i := fromVersion; i < toVersion; i++ {
  43. oldVersion := fromVersion
  44. newVersion := fromVersion + 1
  45. fmt.Println("Updating from v", oldVersion, " to v", newVersion)
  46. runUpdateRoutineWithVersion(oldVersion, newVersion)
  47. //Write the updated version to file
  48. os.WriteFile(versionFile, []byte(strconv.Itoa(newVersion)), 0775)
  49. }
  50. fmt.Println("Update completed")
  51. }
  52. func GetVersionIntFromVersionNumber(version string) int {
  53. versionNumberOnly := strings.ReplaceAll(version, ".", "")
  54. versionInt, _ := strconv.Atoi(versionNumberOnly)
  55. return versionInt
  56. }
  57. func runUpdateRoutineWithVersion(fromVersion int, toVersion int) {
  58. if fromVersion == 307 && toVersion == 308 {
  59. //Updating from v3.0.7 to v3.0.8
  60. err := v308.UpdateFrom307To308()
  61. if err != nil {
  62. panic(err)
  63. }
  64. }
  65. }