diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 01ba862a8..c84a517a9 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -184,10 +184,32 @@ func main() { } else { fmt.Println("Backend folder not detected. Launching existing updater...") + osType := detectOS() + + execPath, err := os.Executable() + if err != nil { + fmt.Fprintf(os.Stderr, "Error getting executable path: %v\n", err) + os.Exit(1) + } + updatersDir := filepath.Join(filepath.Dir(execPath), "updaters") + + var updaterExe string + switch osType { + case "updaters/updater-windows-64.exe": + updaterExe = filepath.Join(updatersDir, "updater-windows-64") + case "updaters/updater-linux": + updaterExe = filepath.Join(updatersDir, "updater-linux") + case "updaters/updater-macos-m1": + updaterExe = filepath.Join(updatersDir, "updater-macos-m1") + case "updaters/updater-macos-64": + updaterExe = filepath.Join(updatersDir, "updater-macos-64") + default: + fmt.Fprintf(os.Stderr, "Unsupported updater: %s\n", osType) + os.Exit(1) + } - updaterExe := filepath.Join(execDir, "updater.exe") cmd := exec.Command(updaterExe) - cmd.Dir = filepath.Dir(updaterExe) + cmd.Dir = updatersDir cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { @@ -715,3 +737,20 @@ func getLatestVersionFromGitHub() (string, error) { version := strings.TrimPrefix(release.TagName, "v") return version, nil } +func detectOS() string { + switch runtime.GOOS { + case "windows": + return "updaters/updater-windows-64.exe" + case "darwin": + if strings.Contains(runtime.GOARCH, "arm") { + return "updaters/updater-macos-m1" + } + return "updaters/updater-macos-64" + case "linux": + return "updaters/updater-linux" + default: + fmt.Fprintf(os.Stderr, "Unsupported operating system: %s\n", runtime.GOOS) + os.Exit(1) + return "" + } +} diff --git a/updater/main.go b/updater/main.go index 7469e4b99..ba08f9e4a 100644 --- a/updater/main.go +++ b/updater/main.go @@ -194,43 +194,46 @@ func downloadFile(filepath string, url string) error { } func extractBinaryFromZip(zipPath, binaryName string) (string, error) { - // Open the ZIP file r, err := zip.OpenReader(zipPath) if err != nil { return "", err } defer r.Close() - // Iterate through the files in the ZIP + var binaryPath string + parentDir, _ := filepath.Abs(filepath.Join(".", "..")) + for _, f := range r.File { - if f.Name == binaryName { - // Open the file inside the ZIP + baseName := filepath.Base(f.Name) + if baseName == binaryName || baseName == "VERSION.md" { rc, err := f.Open() if err != nil { return "", err } defer rc.Close() - // Create the output file - outPath := "./" + binaryName + outPath := filepath.Join(parentDir, baseName) outFile, err := os.Create(outPath) if err != nil { return "", err } defer outFile.Close() - // Copy the contents of the file _, err = io.Copy(outFile, rc) if err != nil { return "", err } - // Return the path to the extracted binary - return outPath, nil + if baseName == binaryName { + binaryPath = outPath + } } } - return "", fmt.Errorf("binary %s not found in ZIP", binaryName) + if binaryPath == "" { + return "", fmt.Errorf("binary %s not found in ZIP", binaryName) + } + return binaryPath, nil } func getLatestVersion() (string, error) {