123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package update
- import (
- "fmt"
- "os"
- "strconv"
- "strings"
- "imuslab.com/zoraxy/mod/utils"
- )
- func RunConfigUpdate(fromVersion int, toVersion int) {
- versionFile := "./conf/version"
- isFirstTimeInit, _ := isFirstTimeInitialize("./conf/proxy/")
- if isFirstTimeInit {
-
- os.MkdirAll("./conf/", 0775)
- os.WriteFile(versionFile, []byte(strconv.Itoa(toVersion)), 0775)
- return
- }
- if fromVersion == 0 {
-
- fromVersion = 307
- if utils.FileExists(versionFile) {
-
- previousVersionText, err := os.ReadFile(versionFile)
- if err != nil {
- panic("Unable to read version file at " + versionFile)
- }
-
- versionInt, err := strconv.Atoi(strings.TrimSpace(string(previousVersionText)))
- if err != nil {
- panic("Unable to read version file at " + versionFile)
- }
- fromVersion = versionInt
- }
- if fromVersion == toVersion {
-
- return
- }
- }
-
- for i := fromVersion; i < toVersion; i++ {
- oldVersion := i
- newVersion := i + 1
- fmt.Println("Updating from v", oldVersion, " to v", newVersion)
- runUpdateRoutineWithVersion(oldVersion, newVersion)
-
- os.WriteFile(versionFile, []byte(strconv.Itoa(newVersion)), 0775)
- }
- fmt.Println("Update completed")
- }
- func GetVersionIntFromVersionNumber(version string) int {
- versionNumberOnly := strings.ReplaceAll(version, ".", "")
- versionInt, _ := strconv.Atoi(versionNumberOnly)
- return versionInt
- }
- func isFirstTimeInitialize(path string) (bool, error) {
-
- info, err := os.Stat(path)
- if os.IsNotExist(err) {
-
- return true, nil
- }
- if err != nil {
-
- return false, err
- }
-
- if !info.IsDir() {
-
- return false, fmt.Errorf("%s is not a directory", path)
- }
-
- files, err := os.ReadDir(path)
- if err != nil {
- return false, err
- }
-
- if len(files) == 0 {
-
- return true, nil
- }
-
- return false, nil
- }
|