-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.go
54 lines (44 loc) · 1.09 KB
/
upgrade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"os"
"os/exec"
)
func installBinary(filepath string) error {
// Rename the current executable
currentExec, err := os.Executable()
if err != nil {
return err
}
backupPath := currentExec + ".bak"
if err := os.Rename(currentExec, backupPath); err != nil {
return err
}
// Replace the current executable with the new one
if err := os.Rename(filepath, currentExec); err != nil {
// Restore the original executable if there's an error
os.Rename(backupPath, currentExec)
return err
}
// Delete the backup file
os.Remove(backupPath)
// Get the path to the currently running executable
executablePath, err := os.Executable()
if err != nil {
fmt.Println("Error:", err)
return err
}
// Start a new instance of the application
cmd := exec.Command(executablePath, os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Start()
if err != nil {
fmt.Println("Error starting process:", err)
return err
}
// Exit the current instance
os.Exit(0)
return nil
}