Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions backend/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,35 @@ func main() {
} else {

fmt.Println("Backend folder not detected. Launching existing updater...")
updaterName := detectOS()

execPath, err := os.Executable()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting executable path: %v\n", err)
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.")
}
Expand Down
86 changes: 42 additions & 44 deletions updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ const (

func main() {
// Detect the operating system
osType := detectOS()
zipName := getZipName()

// Check if the `../backend/` folder exists
if _, err := os.Stat("../backend"); err == nil {
fmt.Println("Directory '../backend' found. Updating from the repository...")
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)
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)

Expand All @@ -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)
Expand Down
Loading