Skip to content

Commit 3c52b05

Browse files
authored
Merge pull request #276 from HyperloopUPV-H8/backend/updater
[Backend] Updater exits
2 parents 65fe58e + 2cc788c commit 3c52b05

2 files changed

Lines changed: 60 additions & 55 deletions

File tree

backend/cmd/main.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,28 +185,35 @@ func main() {
185185
} else {
186186

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

190189
execPath, err := os.Executable()
191190
if err != nil {
192191
fmt.Fprintf(os.Stderr, "Error getting executable path: %v\n", err)
193192
os.Exit(1)
194193
}
195194
execDir := filepath.Dir(execPath)
195+
196+
updaterExe := filepath.Join(execDir, "updater")
197+
// En Windows el ejecutable lleva extensión .exe
198+
if runtime.GOOS == "windows" {
199+
updaterExe += ".exe"
200+
}
196201

197-
updaterExe := filepath.Join(execDir, updaterName)
198-
199-
cmd := exec.Command(updaterExe)
200-
cmd.Dir = execDir
201-
cmd.Stdout = os.Stdout
202-
cmd.Stderr = os.Stderr
203-
if err := cmd.Run(); err != nil {
204-
fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err)
205-
os.Exit(1)
202+
if _, err := os.Stat(updaterExe); err == nil {
203+
cmd := exec.Command(updaterExe)
204+
cmd.Dir = execDir
205+
cmd.Stdout = os.Stdout
206+
cmd.Stderr = os.Stderr
207+
if err := cmd.Run(); err != nil {
208+
fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err)
209+
os.Exit(1)
210+
}
211+
} else {
212+
fmt.Fprintf(os.Stderr, "Updater not found: %s\n", updaterExe)
213+
fmt.Println("Skipping update. Proceeding with the current version.")
206214
}
207215
}
208216

209-
os.Exit(0)
210217
} else {
211218
fmt.Println("Skipping update. Proceeding with the current version.")
212219
}

updater/main.go

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@ const (
2121

2222
func main() {
2323
// Detect the operating system
24-
osType := detectOS()
24+
zipName := getZipName()
2525

2626
// Check if the `../backend/` folder exists
2727
if _, err := os.Stat("../backend"); err == nil {
2828
fmt.Println("Directory '../backend' found. Updating from the repository...")
2929
updateFromGit()
3030
} else {
3131
fmt.Println("Directory '../backend' not found. Checking binaries...")
32-
updateFromBinaries(osType)
32+
updateFromBinaries(zipName)
3333
}
3434
}
3535

36-
func detectOS() string {
36+
func getZipName() string {
3737
switch runtime.GOOS {
3838
case "windows":
39-
return "backend-windows-64.exe" // Incluye la extensión .exe para Windows
39+
return "windows"
4040
case "darwin":
4141
if strings.Contains(runtime.GOARCH, "arm") {
42-
return "backend-macos-m1-64"
42+
return "macos-arm64"
4343
}
44-
return "backend-macos-64"
44+
return "macos-intel"
4545
case "linux":
46-
return "backend-linux-64"
46+
return "linux"
4747
default:
4848
fmt.Fprintf(os.Stderr, "Unsupported operating system: %s\n", runtime.GOOS)
4949
os.Exit(1)
@@ -104,42 +104,41 @@ func stopProcess(processName string) error {
104104
return nil
105105
}
106106

107-
func updateFromBinaries(osType string) {
108-
binaries := []string{"backend-windows-64.exe", "backend-linux-64", "backend-macos-64", "backend-macos-m1-64"}
109-
for _, binary := range binaries {
110-
if _, err := os.Stat("./" + binary); err == nil {
111-
// Check if the backend process is running
112-
isRunning, err := isProcessRunning(binary)
113-
if err != nil {
114-
fmt.Fprintf(os.Stderr, "Error checking if process is running: %v\n", err)
115-
os.Exit(1)
116-
}
117-
118-
// Stop the process if it's running
119-
if isRunning {
120-
fmt.Printf("Process %s is running. Stopping it...\n", binary)
121-
if err := stopProcess(binary); err != nil {
122-
fmt.Fprintf(os.Stderr, "Error stopping process %s: %v\n", binary, err)
123-
os.Exit(1)
124-
}
125-
time.Sleep(500 * time.Millisecond) // waits 1/2 second to ensure the process is stopped
126-
}
107+
func updateFromBinaries(zipBase string) {
108+
binary := "backend"
109+
if runtime.GOOS == "windows" {
110+
binary += ".exe"
111+
}
127112

128-
fmt.Printf("Deleting old binary: %s\n", binary)
129-
deleted := false
130-
for i := 0; i < 5; i++ {
131-
if err := os.Remove("./" + binary); err == nil {
132-
deleted = true
133-
break
134-
} else {
135-
fmt.Printf("Retrying delete (%d/5)...\n", i+1)
136-
time.Sleep(300 * time.Millisecond)
137-
}
138-
}
139-
if !deleted {
140-
fmt.Fprintf(os.Stderr, "Error deleting old binary after multiple attempts.\n")
113+
// Stop and remove old binary if exists
114+
if _, err := os.Stat("./" + binary); err == nil {
115+
isRunning, err := isProcessRunning(binary)
116+
if err != nil {
117+
fmt.Fprintf(os.Stderr, "Error checking if process is running: %v\n", err)
118+
os.Exit(1)
119+
}
120+
if isRunning {
121+
fmt.Printf("Process %s is running. Stopping it...\n", binary)
122+
if err := stopProcess(binary); err != nil {
123+
fmt.Fprintf(os.Stderr, "Error stopping process %s: %v\n", binary, err)
141124
os.Exit(1)
142125
}
126+
time.Sleep(500 * time.Millisecond)
127+
}
128+
fmt.Printf("Deleting old binary: %s\n", binary)
129+
deleted := false
130+
for i := 0; i < 5; i++ {
131+
if err := os.Remove("./" + binary); err == nil {
132+
deleted = true
133+
break
134+
} else {
135+
fmt.Printf("Retrying delete (%d/5)...\n", i+1)
136+
time.Sleep(300 * time.Millisecond)
137+
}
138+
}
139+
if !deleted {
140+
fmt.Fprintf(os.Stderr, "Error deleting old binary after multiple attempts.\n")
141+
os.Exit(1)
143142
}
144143
}
145144

@@ -150,8 +149,7 @@ func updateFromBinaries(osType string) {
150149
os.Exit(1)
151150
}
152151

153-
// Construct the ZIP file URL
154-
zipFileName := fmt.Sprintf("control-station-v%s.zip", strings.ReplaceAll(latestVersion, ".", "-"))
152+
zipFileName := fmt.Sprintf("%s-%s.zip", zipBase, latestVersion)
155153
url := fmt.Sprintf("https://github.com/%s/%s/releases/download/v%s/%s", repoOwner, repoName, latestVersion, zipFileName)
156154
fmt.Printf("Downloading ZIP from: %s\n", url)
157155

@@ -162,8 +160,8 @@ func updateFromBinaries(osType string) {
162160
os.Exit(1)
163161
}
164162

165-
// Extract the binary from the ZIP file
166-
binaryPath, err := extractBinaryFromZip("./"+zipFileName, osType)
163+
// Extract the binary and VERSION.md from the ZIP file
164+
binaryPath, err := extractBinaryFromZip("./"+zipFileName, binary)
167165
if err != nil {
168166
fmt.Fprintf(os.Stderr, "Error extracting the binary: %v\n", err)
169167
os.Exit(1)

0 commit comments

Comments
 (0)