Skip to content

Commit 35f29f3

Browse files
authored
Merge pull request #3 from phendryx/auto-update
keep the same terminal for macos/linux after update
2 parents 798a4b1 + 558915d commit 35f29f3

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

albiondata-client.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os/exec"
66
"runtime"
77
"strings"
8+
"syscall"
89
"time"
910

1011
"github.com/ao-data/albiondata-client/client"
@@ -96,7 +97,9 @@ func tryUpdate(u *updater.Updater) bool {
9697
return false
9798
}
9899

99-
// restartProcess starts a new instance of the application and exits the current process.
100+
// restartProcess replaces the current process with the updated version.
101+
// On Unix systems (macOS/Linux), it uses syscall.Exec to seamlessly take over the terminal.
102+
// On Windows, it starts a new process and exits since exec-style replacement isn't supported.
100103
func restartProcess() {
101104
execPath, err := os.Executable()
102105
if err != nil {
@@ -106,18 +109,37 @@ func restartProcess() {
106109

107110
log.Info("Restarting with updated version...")
108111

109-
// Start the new process with the same arguments
110-
cmd := exec.Command(execPath, os.Args[1:]...)
111-
cmd.Stdout = os.Stdout
112-
cmd.Stderr = os.Stderr
113-
cmd.Stdin = os.Stdin
112+
if runtime.GOOS == "windows" {
113+
// Windows doesn't support exec-style process replacement
114+
// Start a new process and exit
115+
cmd := exec.Command(execPath, os.Args[1:]...)
116+
cmd.Stdout = os.Stdout
117+
cmd.Stderr = os.Stderr
118+
cmd.Stdin = os.Stdin
114119

115-
err = cmd.Start()
116-
if err != nil {
117-
log.Errorf("Failed to start new process: %v", err)
118-
return
119-
}
120+
err = cmd.Start()
121+
if err != nil {
122+
log.Errorf("Failed to start new process: %v", err)
123+
return
124+
}
120125

121-
log.Info("New process started, exiting current process.")
122-
os.Exit(0)
126+
log.Info("New process started, exiting current process.")
127+
os.Exit(0)
128+
} else {
129+
// On Unix systems (macOS/Linux), use syscall.Exec to replace the current process
130+
// This seamlessly takes over the terminal - same PID, same terminal session
131+
err = syscall.Exec(execPath, os.Args, os.Environ())
132+
if err != nil {
133+
log.Errorf("Failed to exec new process: %v", err)
134+
// Fall back to starting a new process
135+
cmd := exec.Command(execPath, os.Args[1:]...)
136+
cmd.Stdout = os.Stdout
137+
cmd.Stderr = os.Stderr
138+
cmd.Stdin = os.Stdin
139+
_ = cmd.Start()
140+
os.Exit(0)
141+
}
142+
// If syscall.Exec succeeds, this line is never reached
143+
// because the current process is replaced
144+
}
123145
}

0 commit comments

Comments
 (0)