Skip to content

Commit 4e636d6

Browse files
authored
Merge pull request #269 from HyperloopUPV-H8/backend/updater-path
[Backend] Updaters path draft
2 parents f0a1af1 + 2db9dc0 commit 4e636d6

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

backend/cmd/main.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,32 @@ func main() {
184184
} else {
185185

186186
fmt.Println("Backend folder not detected. Launching existing updater...")
187+
osType := detectOS()
188+
189+
execPath, err := os.Executable()
190+
if err != nil {
191+
fmt.Fprintf(os.Stderr, "Error getting executable path: %v\n", err)
192+
os.Exit(1)
193+
}
194+
updatersDir := filepath.Join(filepath.Dir(execPath), "updaters")
195+
196+
var updaterExe string
197+
switch osType {
198+
case "updaters/updater-windows-64.exe":
199+
updaterExe = filepath.Join(updatersDir, "updater-windows-64")
200+
case "updaters/updater-linux":
201+
updaterExe = filepath.Join(updatersDir, "updater-linux")
202+
case "updaters/updater-macos-m1":
203+
updaterExe = filepath.Join(updatersDir, "updater-macos-m1")
204+
case "updaters/updater-macos-64":
205+
updaterExe = filepath.Join(updatersDir, "updater-macos-64")
206+
default:
207+
fmt.Fprintf(os.Stderr, "Unsupported updater: %s\n", osType)
208+
os.Exit(1)
209+
}
187210

188-
updaterExe := filepath.Join(execDir, "updater.exe")
189211
cmd := exec.Command(updaterExe)
190-
cmd.Dir = filepath.Dir(updaterExe)
212+
cmd.Dir = updatersDir
191213
cmd.Stdout = os.Stdout
192214
cmd.Stderr = os.Stderr
193215
if err := cmd.Run(); err != nil {
@@ -715,3 +737,20 @@ func getLatestVersionFromGitHub() (string, error) {
715737
version := strings.TrimPrefix(release.TagName, "v")
716738
return version, nil
717739
}
740+
func detectOS() string {
741+
switch runtime.GOOS {
742+
case "windows":
743+
return "updaters/updater-windows-64.exe"
744+
case "darwin":
745+
if strings.Contains(runtime.GOARCH, "arm") {
746+
return "updaters/updater-macos-m1"
747+
}
748+
return "updaters/updater-macos-64"
749+
case "linux":
750+
return "updaters/updater-linux"
751+
default:
752+
fmt.Fprintf(os.Stderr, "Unsupported operating system: %s\n", runtime.GOOS)
753+
os.Exit(1)
754+
return ""
755+
}
756+
}

updater/main.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,43 +194,46 @@ func downloadFile(filepath string, url string) error {
194194
}
195195

196196
func extractBinaryFromZip(zipPath, binaryName string) (string, error) {
197-
// Open the ZIP file
198197
r, err := zip.OpenReader(zipPath)
199198
if err != nil {
200199
return "", err
201200
}
202201
defer r.Close()
203202

204-
// Iterate through the files in the ZIP
203+
var binaryPath string
204+
parentDir, _ := filepath.Abs(filepath.Join(".", ".."))
205+
205206
for _, f := range r.File {
206-
if f.Name == binaryName {
207-
// Open the file inside the ZIP
207+
baseName := filepath.Base(f.Name)
208+
if baseName == binaryName || baseName == "VERSION.md" {
208209
rc, err := f.Open()
209210
if err != nil {
210211
return "", err
211212
}
212213
defer rc.Close()
213214

214-
// Create the output file
215-
outPath := "./" + binaryName
215+
outPath := filepath.Join(parentDir, baseName)
216216
outFile, err := os.Create(outPath)
217217
if err != nil {
218218
return "", err
219219
}
220220
defer outFile.Close()
221221

222-
// Copy the contents of the file
223222
_, err = io.Copy(outFile, rc)
224223
if err != nil {
225224
return "", err
226225
}
227226

228-
// Return the path to the extracted binary
229-
return outPath, nil
227+
if baseName == binaryName {
228+
binaryPath = outPath
229+
}
230230
}
231231
}
232232

233-
return "", fmt.Errorf("binary %s not found in ZIP", binaryName)
233+
if binaryPath == "" {
234+
return "", fmt.Errorf("binary %s not found in ZIP", binaryName)
235+
}
236+
return binaryPath, nil
234237
}
235238

236239
func getLatestVersion() (string, error) {

0 commit comments

Comments
 (0)