update.go 2.7 KB

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