update.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "imuslab.com/zoraxy/mod/utils"
  13. )
  14. // Run config update. Version numbers are int. For example
  15. // to update 3.0.7 to 3.0.8, use RunConfigUpdate(307, 308)
  16. // This function support cross versions updates (e.g. 307 -> 310)
  17. func RunConfigUpdate(fromVersion int, toVersion int) {
  18. versionFile := "./conf/version"
  19. isFirstTimeInit, _ := isFirstTimeInitialize("./conf/proxy/")
  20. if isFirstTimeInit {
  21. //Create version file and exit
  22. os.MkdirAll("./conf/", 0775)
  23. os.WriteFile(versionFile, []byte(strconv.Itoa(toVersion)), 0775)
  24. return
  25. }
  26. if fromVersion == 0 {
  27. //Run auto previous version detection
  28. fromVersion = 307
  29. if utils.FileExists(versionFile) {
  30. //Read the version file
  31. previousVersionText, err := os.ReadFile(versionFile)
  32. if err != nil {
  33. panic("Unable to read version file at " + versionFile)
  34. }
  35. //Convert the version to int
  36. versionInt, err := strconv.Atoi(strings.TrimSpace(string(previousVersionText)))
  37. if err != nil {
  38. panic("Unable to read version file at " + versionFile)
  39. }
  40. fromVersion = versionInt
  41. }
  42. if fromVersion == toVersion {
  43. //No need to update
  44. return
  45. }
  46. }
  47. //Do iterate update
  48. for i := fromVersion; i < toVersion; i++ {
  49. oldVersion := i
  50. newVersion := i + 1
  51. fmt.Println("Updating from v", oldVersion, " to v", newVersion)
  52. runUpdateRoutineWithVersion(oldVersion, newVersion)
  53. //Write the updated version to file
  54. os.WriteFile(versionFile, []byte(strconv.Itoa(newVersion)), 0775)
  55. }
  56. fmt.Println("Update completed")
  57. }
  58. func GetVersionIntFromVersionNumber(version string) int {
  59. versionNumberOnly := strings.ReplaceAll(version, ".", "")
  60. versionInt, _ := strconv.Atoi(versionNumberOnly)
  61. return versionInt
  62. }
  63. // Check if the folder "./conf/proxy/" exists and contains files
  64. func isFirstTimeInitialize(path string) (bool, error) {
  65. // Check if the folder exists
  66. info, err := os.Stat(path)
  67. if os.IsNotExist(err) {
  68. // The folder does not exist
  69. return true, nil
  70. }
  71. if err != nil {
  72. // Some other error occurred
  73. return false, err
  74. }
  75. // Check if it is a directory
  76. if !info.IsDir() {
  77. // The path is not a directory
  78. return false, fmt.Errorf("%s is not a directory", path)
  79. }
  80. // Read the directory contents
  81. files, err := os.ReadDir(path)
  82. if err != nil {
  83. return false, err
  84. }
  85. // Check if the directory is empty
  86. if len(files) == 0 {
  87. // The folder exists but is empty
  88. return true, nil
  89. }
  90. // The folder exists and contains files
  91. return false, nil
  92. }