From b58ac2cb935e1340398c0076976b64dbf321eacf Mon Sep 17 00:00:00 2001 From: Kreato Date: Fri, 14 Feb 2025 11:03:36 +0300 Subject: [PATCH] updater: remove migration steps (never used) --- common/migrate.go | 31 ----- common/updater.go | 294 ++++++++++++++++++++++------------------------ 2 files changed, 140 insertions(+), 185 deletions(-) delete mode 100644 common/migrate.go diff --git a/common/migrate.go b/common/migrate.go deleted file mode 100644 index 5bdfe47..0000000 --- a/common/migrate.go +++ /dev/null @@ -1,31 +0,0 @@ -package common - -import ( - "fmt" - "strings" - "github.com/spf13/cobra" -) - -var MigrateCmd = &cobra.Command{ - Use: "migrate", - Short: "Migrate Monokit from one version to another", - Run: func(cmd *cobra.Command, args []string) { - fromVersion, _ := cmd.Flags().GetString("from") - Migrate(fromVersion) - }, -} - -func MigrateV5ToV6() { - return // Placeholder -} - -func Migrate(fromVersion string) { - fromVersionSplit := strings.Split(fromVersion, ".")[0] // Should be v1, v2, etc. - - if fromVersionSplit == "v5" { - fmt.Println("Migrating from v5 to v6") - MigrateV5ToV6() - } else { - fmt.Println("No migration path exists for this version") - } -} diff --git a/common/updater.go b/common/updater.go index b5664a6..3cbd8f3 100644 --- a/common/updater.go +++ b/common/updater.go @@ -1,166 +1,152 @@ package common import ( - "os" - "fmt" - "strings" - "os/exec" - "runtime" - "net/http" - "archive/tar" - "encoding/json" - "compress/gzip" - "github.com/spf13/cobra" + "archive/tar" + "compress/gzip" + "encoding/json" + "fmt" + "net/http" + "os" + "runtime" + "strings" + + "github.com/spf13/cobra" ) var UpdateCmd = &cobra.Command{ - Use: "update", - Short: "Update Monokit", - Run: func(cmd *cobra.Command, args []string) { - specificVersion, _ := cmd.Flags().GetString("version") - force, _ := cmd.Flags().GetBool("force") - - Update(specificVersion, force) - }, + Use: "update", + Short: "Update Monokit", + Run: func(cmd *cobra.Command, args []string) { + specificVersion, _ := cmd.Flags().GetString("version") + force, _ := cmd.Flags().GetBool("force") + + Update(specificVersion, force) + }, } -func DownloadAndExtract(url string, runMigrations bool, oldVersion string) { - MonokitPath, err := os.Executable() - - if err != nil { - LogError("Couldn't get executable path: " + err.Error()) - } - - // Download the release - resp, err := http.Get(url) - if err != nil { - LogError("Couldn't download the release: " + err.Error()) - } - defer resp.Body.Close() - - // Extract the release - gzr, err := gzip.NewReader(resp.Body) - - if err != nil { - LogError("Couldn't extract the release: " + err.Error()) - } - - defer gzr.Close() - - tr := tar.NewReader(gzr) - for { - hdr, err := tr.Next() - if err != nil { - break - } - - if hdr.Name == "monokit" { - f, err := os.Create(TmpDir + "monokit") - if err != nil { - LogError("Couldn't create monokit binary: " + err.Error()) - } - defer f.Close() - - _, err = f.ReadFrom(tr) - if err != nil { - LogError("Couldn't write monokit binary: " + err.Error()) - } - } - } - - os.Rename(MonokitPath, MonokitPath + ".bak") - - // Move monokit binary to the correct path - os.Rename(TmpDir + "monokit", MonokitPath) - os.Chmod(MonokitPath, 0755) - - if runMigrations { - cmd, err := exec.Command(MonokitPath, "migrate", "--from", oldVersion).Output() - if err != nil { - fmt.Println("Migration failed:", err) - fmt.Println("Returning to the previous version") - os.Rename(MonokitPath + ".bak", MonokitPath) - } else { - fmt.Println(string(cmd)) - } - } +func DownloadAndExtract(url string) { + MonokitPath, err := os.Executable() + + if err != nil { + LogError("Couldn't get executable path: " + err.Error()) + } + + // Download the release + resp, err := http.Get(url) + if err != nil { + LogError("Couldn't download the release: " + err.Error()) + } + defer resp.Body.Close() + + // Extract the release + gzr, err := gzip.NewReader(resp.Body) + + if err != nil { + LogError("Couldn't extract the release: " + err.Error()) + } + + defer gzr.Close() + + tr := tar.NewReader(gzr) + for { + hdr, err := tr.Next() + if err != nil { + break + } + + if hdr.Name == "monokit" { + f, err := os.Create(TmpDir + "monokit") + if err != nil { + LogError("Couldn't create monokit binary: " + err.Error()) + } + defer f.Close() + + _, err = f.ReadFrom(tr) + if err != nil { + LogError("Couldn't write monokit binary: " + err.Error()) + } + } + } + + os.Rename(MonokitPath, MonokitPath+".bak") + + // Move monokit binary to the correct path + os.Rename(TmpDir+"monokit", MonokitPath) + os.Chmod(MonokitPath, 0755) } func Update(specificVersion string, force bool) { - var url string - var version string - os := runtime.GOOS - arch := runtime.GOARCH - runMigrations := false - - if specificVersion != "" { - version = specificVersion - url = "https://github.com/monobilisim/monokit/releases/download/v" + specificVersion + "/monokit_" + specificVersion + "_" + os + "_" + arch + ".tar.gz" - } else { - // Get latest release - url = "https://api.github.com/repos/monobilisim/monokit/releases/latest" - resp, err := http.Get(url) - if err != nil { - LogError("Couldn't get latest release: " + err.Error()) - } - defer resp.Body.Close() - - var release map[string]interface{} - err = json.NewDecoder(resp.Body).Decode(&release) - if err != nil { - LogError("Couldn't decode latest release: " + err.Error()) - } - - assets := release["assets"].([]interface{}) - for _, asset := range assets { - assetMap := asset.(map[string]interface{}) - if strings.Contains(assetMap["name"].(string), os) && strings.Contains(assetMap["name"].(string), arch) { - url = assetMap["browser_download_url"].(string) - version = release["tag_name"].(string) - version = strings.TrimPrefix(version, "v") - break - } - } - } - - if url == "" { - fmt.Println("No release found for your OS and architecture") - return - } - - if (version == MonokitVersion || MonokitVersion == "devel") && !force { - - if MonokitVersion == "devel" { - fmt.Println("Monokit is a development version, run with --force to update anyway") - } else { - fmt.Println("Monokit is already up to date, run with --force to update anyway") - } - - return - } - - fmt.Println("Current Monokit version:", MonokitVersion) - - if MonokitVersion != "devel" { - monokitVersionSplit := strings.Split(MonokitVersion, ".") - versionSplit := strings.Split(version, ".") - - if strings.Contains(versionSplit[0], "v") { - versionSplit[0] = strings.TrimPrefix(versionSplit[0], "v") - } - - if monokitVersionSplit[0] != versionSplit[0] { - if !force { - fmt.Println("A new major version is available. This might bring breaking changes. Monokit will attempt to migrate to the new vesrion. You can run with --force to update") - return - } else { - runMigrations = true - } - } - } - - fmt.Println("Downloading Monokit version", version) - DownloadAndExtract(url, runMigrations, MonokitVersion) - - fmt.Println("Monokit updated to version", version) + var url string + var version string + os := runtime.GOOS + arch := runtime.GOARCH + + if specificVersion != "" { + version = specificVersion + url = "https://github.com/monobilisim/monokit/releases/download/v" + specificVersion + "/monokit_" + specificVersion + "_" + os + "_" + arch + ".tar.gz" + } else { + // Get latest release + url = "https://api.github.com/repos/monobilisim/monokit/releases/latest" + resp, err := http.Get(url) + if err != nil { + LogError("Couldn't get latest release: " + err.Error()) + } + defer resp.Body.Close() + + var release map[string]interface{} + err = json.NewDecoder(resp.Body).Decode(&release) + if err != nil { + LogError("Couldn't decode latest release: " + err.Error()) + } + + assets := release["assets"].([]interface{}) + for _, asset := range assets { + assetMap := asset.(map[string]interface{}) + if strings.Contains(assetMap["name"].(string), os) && strings.Contains(assetMap["name"].(string), arch) { + url = assetMap["browser_download_url"].(string) + version = release["tag_name"].(string) + version = strings.TrimPrefix(version, "v") + break + } + } + } + + if url == "" { + fmt.Println("No release found for your OS and architecture") + return + } + + if (version == MonokitVersion || MonokitVersion == "devel") && !force { + + if MonokitVersion == "devel" { + fmt.Println("Monokit is a development version, run with --force to update anyway") + } else { + fmt.Println("Monokit is already up to date, run with --force to update anyway") + } + + return + } + + fmt.Println("Current Monokit version:", MonokitVersion) + + if MonokitVersion != "devel" { + monokitVersionSplit := strings.Split(MonokitVersion, ".") + versionSplit := strings.Split(version, ".") + + if strings.Contains(versionSplit[0], "v") { + versionSplit[0] = strings.TrimPrefix(versionSplit[0], "v") + } + + if monokitVersionSplit[0] != versionSplit[0] { + if !force { + fmt.Println("A new major version is available. This might bring breaking changes. Monokit will attempt to migrate to the new vesrion. You can run with --force to update") + return + } + } + } + + fmt.Println("Downloading Monokit version", version) + DownloadAndExtract(url) + + fmt.Println("Monokit updated to version", version) }