diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 1df2ae82c..944ecf952 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -185,7 +185,6 @@ func main() { } else { fmt.Println("Backend folder not detected. Launching existing updater...") - updaterName := detectOS() execPath, err := os.Executable() if err != nil { @@ -193,20 +192,28 @@ func main() { os.Exit(1) } execDir := filepath.Dir(execPath) + + updaterExe := filepath.Join(execDir, "updater") + // En Windows el ejecutable lleva extensión .exe + if runtime.GOOS == "windows" { + updaterExe += ".exe" + } - updaterExe := filepath.Join(execDir, updaterName) - - cmd := exec.Command(updaterExe) - cmd.Dir = execDir - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err) - os.Exit(1) + if _, err := os.Stat(updaterExe); err == nil { + cmd := exec.Command(updaterExe) + cmd.Dir = execDir + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err) + os.Exit(1) + } + } else { + fmt.Fprintf(os.Stderr, "Updater not found: %s\n", updaterExe) + fmt.Println("Skipping update. Proceeding with the current version.") } } - os.Exit(0) } else { fmt.Println("Skipping update. Proceeding with the current version.") } diff --git a/updater/main.go b/updater/main.go index ba08f9e4a..8e456b5c5 100644 --- a/updater/main.go +++ b/updater/main.go @@ -21,7 +21,7 @@ const ( func main() { // Detect the operating system - osType := detectOS() + zipName := getZipName() // Check if the `../backend/` folder exists if _, err := os.Stat("../backend"); err == nil { @@ -29,21 +29,21 @@ func main() { updateFromGit() } else { fmt.Println("Directory '../backend' not found. Checking binaries...") - updateFromBinaries(osType) + updateFromBinaries(zipName) } } -func detectOS() string { +func getZipName() string { switch runtime.GOOS { case "windows": - return "backend-windows-64.exe" // Incluye la extensión .exe para Windows + return "windows" case "darwin": if strings.Contains(runtime.GOARCH, "arm") { - return "backend-macos-m1-64" + return "macos-arm64" } - return "backend-macos-64" + return "macos-intel" case "linux": - return "backend-linux-64" + return "linux" default: fmt.Fprintf(os.Stderr, "Unsupported operating system: %s\n", runtime.GOOS) os.Exit(1) @@ -104,42 +104,41 @@ func stopProcess(processName string) error { return nil } -func updateFromBinaries(osType string) { - binaries := []string{"backend-windows-64.exe", "backend-linux-64", "backend-macos-64", "backend-macos-m1-64"} - for _, binary := range binaries { - if _, err := os.Stat("./" + binary); err == nil { - // Check if the backend process is running - isRunning, err := isProcessRunning(binary) - if err != nil { - fmt.Fprintf(os.Stderr, "Error checking if process is running: %v\n", err) - os.Exit(1) - } - - // Stop the process if it's running - if isRunning { - fmt.Printf("Process %s is running. Stopping it...\n", binary) - if err := stopProcess(binary); err != nil { - fmt.Fprintf(os.Stderr, "Error stopping process %s: %v\n", binary, err) - os.Exit(1) - } - time.Sleep(500 * time.Millisecond) // waits 1/2 second to ensure the process is stopped - } +func updateFromBinaries(zipBase string) { + binary := "backend" + if runtime.GOOS == "windows" { + binary += ".exe" + } - fmt.Printf("Deleting old binary: %s\n", binary) - deleted := false - for i := 0; i < 5; i++ { - if err := os.Remove("./" + binary); err == nil { - deleted = true - break - } else { - fmt.Printf("Retrying delete (%d/5)...\n", i+1) - time.Sleep(300 * time.Millisecond) - } - } - if !deleted { - fmt.Fprintf(os.Stderr, "Error deleting old binary after multiple attempts.\n") + // Stop and remove old binary if exists + if _, err := os.Stat("./" + binary); err == nil { + isRunning, err := isProcessRunning(binary) + if err != nil { + fmt.Fprintf(os.Stderr, "Error checking if process is running: %v\n", err) + os.Exit(1) + } + if isRunning { + fmt.Printf("Process %s is running. Stopping it...\n", binary) + if err := stopProcess(binary); err != nil { + fmt.Fprintf(os.Stderr, "Error stopping process %s: %v\n", binary, err) os.Exit(1) } + time.Sleep(500 * time.Millisecond) + } + fmt.Printf("Deleting old binary: %s\n", binary) + deleted := false + for i := 0; i < 5; i++ { + if err := os.Remove("./" + binary); err == nil { + deleted = true + break + } else { + fmt.Printf("Retrying delete (%d/5)...\n", i+1) + time.Sleep(300 * time.Millisecond) + } + } + if !deleted { + fmt.Fprintf(os.Stderr, "Error deleting old binary after multiple attempts.\n") + os.Exit(1) } } @@ -150,8 +149,7 @@ func updateFromBinaries(osType string) { os.Exit(1) } - // Construct the ZIP file URL - zipFileName := fmt.Sprintf("control-station-v%s.zip", strings.ReplaceAll(latestVersion, ".", "-")) + zipFileName := fmt.Sprintf("%s-%s.zip", zipBase, latestVersion) url := fmt.Sprintf("https://github.com/%s/%s/releases/download/v%s/%s", repoOwner, repoName, latestVersion, zipFileName) fmt.Printf("Downloading ZIP from: %s\n", url) @@ -162,8 +160,8 @@ func updateFromBinaries(osType string) { os.Exit(1) } - // Extract the binary from the ZIP file - binaryPath, err := extractBinaryFromZip("./"+zipFileName, osType) + // Extract the binary and VERSION.md from the ZIP file + binaryPath, err := extractBinaryFromZip("./"+zipFileName, binary) if err != nil { fmt.Fprintf(os.Stderr, "Error extracting the binary: %v\n", err) os.Exit(1)