-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathupgrade.go
More file actions
82 lines (71 loc) · 2.27 KB
/
Copy pathupgrade.go
File metadata and controls
82 lines (71 loc) · 2.27 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
const repoURL = "https://github.com/agentsdance/aigit"
// selfUpgrade replaces the running binary with the given release version.
// Homebrew installs are delegated to brew; everything else is built from the
// release tag with the local Go toolchain and swapped in place.
func selfUpgrade(version string) error {
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("locating current binary: %w", err)
}
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
exe = resolved
}
if strings.Contains(exe, "/Cellar/") || strings.Contains(exe, "/linuxbrew/") {
cmd := exec.Command("brew", "upgrade", "aigit")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
goBin, err := exec.LookPath("go")
if err != nil {
return fmt.Errorf("go toolchain not found; cannot build %s from source", version)
}
tmpDir, err := os.MkdirTemp("", "aigit-upgrade-*")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
srcDir := filepath.Join(tmpDir, "src")
if err := runQuiet(exec.Command("git", "clone", "--quiet", "--depth", "1", "--branch", version, repoURL, srcDir)); err != nil {
return fmt.Errorf("downloading %s: %w", version, err)
}
newBin := filepath.Join(tmpDir, "aigit")
build := exec.Command(goBin, "build", "-ldflags", "-s -w -X main.Version="+version, "-o", newBin, ".")
build.Dir = srcDir
if err := runQuiet(build); err != nil {
return fmt.Errorf("building %s: %w", version, err)
}
return replaceBinary(newBin, exe)
}
// replaceBinary swaps dst for newBin via a rename from a sibling temp file,
// so the running executable is replaced atomically rather than truncated.
func replaceBinary(newBin, dst string) error {
data, err := os.ReadFile(newBin)
if err != nil {
return err
}
staged := dst + ".new"
if err := os.WriteFile(staged, data, 0o755); err != nil {
return fmt.Errorf("staging new binary (try upgrading with elevated permissions): %w", err)
}
if err := os.Rename(staged, dst); err != nil {
os.Remove(staged)
return fmt.Errorf("installing new binary: %w", err)
}
return nil
}
func runQuiet(cmd *exec.Cmd) error {
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%v: %s", err, strings.TrimSpace(string(out)))
}
return nil
}