Fixed incorrect browser exit detection in RunAndWait() which could lead to Browser file corruption due to incomplete updates.
72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
// run_windows.go
|
|
//go:build windows
|
|
// +build windows
|
|
|
|
package spm
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// Run locates and starts the installed Spitfire browser without waiting for it to exit.
|
|
func Run() error {
|
|
installDir, err := GetInstallDir()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
exePath := filepath.Join(installDir, "browser", "spitfire.exe")
|
|
|
|
cmd := exec.Command(exePath)
|
|
cmd.Dir = filepath.Join(installDir, "browser")
|
|
return cmd.Start()
|
|
}
|
|
|
|
// RunAndWait locates and starts the installed Spitfire browser and waits for it to exit.
|
|
func RunAndWait() error {
|
|
installDir, err := GetInstallDir()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get install directory: %w", err)
|
|
}
|
|
|
|
exePath := filepath.Join(installDir, "browser", "spitfire.exe")
|
|
if _, err := os.Stat(exePath); err != nil {
|
|
return fmt.Errorf("browser executable not found at %s: %w", exePath, err)
|
|
}
|
|
|
|
cmd := exec.Command(exePath)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
cmd.Dir = filepath.Join(installDir, "browser")
|
|
|
|
// Create job object starting the process
|
|
job, err := windows.CreateJobObject(nil, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create job object: %w", err)
|
|
}
|
|
defer windows.CloseHandle(job)
|
|
|
|
fmt.Printf("Starting browser: %s\n", exePath)
|
|
if err := cmd.Start(); err != nil {
|
|
return fmt.Errorf("failed to start browser: %w", err)
|
|
}
|
|
|
|
for {
|
|
cmd := exec.Command("tasklist", "/FI", "IMAGENAME eq spitfire.exe")
|
|
output, _ := cmd.Output()
|
|
if !strings.Contains(string(output), "spitfire.exe") {
|
|
break
|
|
}
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
|
|
fmt.Println("Browser exited.")
|
|
return nil
|
|
}
|