| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "io/fs" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "regexp" |
| 14 | "runtime" |
| 15 | "strings" |
| 16 | "syscall" |
| 17 | |
| 18 | "github.com/sylvinus/agent-vm/internal/paths" |
| 19 | "github.com/sylvinus/agent-vm/internal/state" |
| 20 | "github.com/sylvinus/agent-vm/internal/version" |
| 21 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 22 | ) |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | func binName() string { |
| 30 | if runtime.GOOS == "windows" { |
| 31 | return "agent-vm.exe" |
| 32 | } |
| 33 | return "agent-vm" |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | func binLink() (string, error) { |
| 39 | dir := os.Getenv("AGENT_VM_BIN_DIR") |
| 40 | if dir != "" { |
| 41 | abs, err := filepath.Abs(dir) |
| 42 | if err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | return filepath.Join(abs, binName()), nil |
| 46 | } |
| 47 | home, err := os.UserHomeDir() |
| 48 | if err != nil || home == "" { |
| 49 | return "", errors.New("cannot find your home folder: set AGENT_VM_BIN_DIR to the folder to install in") |
| 50 | } |
| 51 | return filepath.Join(home, ".local", "bin", binName()), nil |
| 52 | } |
| 53 | |
| 54 | |
| 55 | func self() (string, error) { |
| 56 | exe, err := os.Executable() |
| 57 | if err != nil { |
| 58 | return "", err |
| 59 | } |
| 60 | return filepath.EvalSymlinks(exe) |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | func ours(link, exe string) bool { |
| 66 | if t, err := os.Readlink(link); err == nil { |
| 67 | return t == exe |
| 68 | } |
| 69 | return sameContent(link, exe) |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | func sameContent(a, b string) bool { |
| 75 | fa, err := os.Open(a) |
| 76 | if err != nil { |
| 77 | return false |
| 78 | } |
| 79 | defer fa.Close() |
| 80 | fb, err := os.Open(b) |
| 81 | if err != nil { |
| 82 | return false |
| 83 | } |
| 84 | defer fb.Close() |
| 85 | sa, erra := fa.Stat() |
| 86 | sb, errb := fb.Stat() |
| 87 | if erra != nil || errb != nil || !sa.Mode().IsRegular() || !sb.Mode().IsRegular() || sa.Size() != sb.Size() { |
| 88 | return false |
| 89 | } |
| 90 | eof := func(err error) bool { return err == io.EOF || err == io.ErrUnexpectedEOF } |
| 91 | ba, bb := make([]byte, 1<<16), make([]byte, 1<<16) |
| 92 | for { |
| 93 | na, erra := io.ReadFull(fa, ba) |
| 94 | nb, errb := io.ReadFull(fb, bb) |
| 95 | if na != nb || !bytes.Equal(ba[:na], bb[:nb]) { |
| 96 | return false |
| 97 | } |
| 98 | if erra != nil || errb != nil { |
| 99 | return eof(erra) && eof(errb) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | const installedCopy = "installed-copy.sha256" |
| 107 | |
| 108 | func sha256Of(p string) string { |
| 109 | f, err := os.Open(p) |
| 110 | if err != nil { |
| 111 | return "" |
| 112 | } |
| 113 | defer f.Close() |
| 114 | h := sha256.New() |
| 115 | if _, err := io.Copy(h, f); err != nil { |
| 116 | return "" |
| 117 | } |
| 118 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | func (e *app) earlier(link string) bool { |
| 125 | if t, err := os.Readlink(link); err == nil { |
| 126 | if filepath.Base(t) == "agent-vm.sh" { |
| 127 | return true |
| 128 | } |
| 129 | _, err := os.Stat(link) |
| 130 | return errors.Is(err, fs.ErrNotExist) && (filepath.Base(t) == "agent-vm" || filepath.Base(t) == "agent-vm.exe") |
| 131 | } |
| 132 | h := sha256Of(link) |
| 133 | return h != "" && h == strings.TrimSpace(e.state.Read(installedCopy)) |
| 134 | } |
| 135 | |
| 136 | var rcSourcesRe = regexp.MustCompile(`(?m)^[^#\n]*agent-vm\.sh`) |
| 137 | |
| 138 | |
| 139 | |
| 140 | func onPath(dir string) bool { |
| 141 | norm := func(p string) string { return strings.TrimSuffix(paths.Host(p), "/") } |
| 142 | for _, p := range filepath.SplitList(os.Getenv("PATH")) { |
| 143 | if paths.Equal(norm(p), norm(dir)) { |
| 144 | return true |
| 145 | } |
| 146 | } |
| 147 | return false |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | func rcSourcesUs() bool { |
| 153 | home, err := os.UserHomeDir() |
| 154 | if err != nil || home == "" { |
| 155 | return false |
| 156 | } |
| 157 | for _, f := range []string{".zshrc", ".bashrc", ".bash_profile", ".profile", ".zshenv"} { |
| 158 | if b, err := os.ReadFile(filepath.Join(home, f)); err == nil && rcSourcesRe.Match(b) { |
| 159 | return true |
| 160 | } |
| 161 | } |
| 162 | return false |
| 163 | } |
| 164 | |
| 165 | func (e *app) installCmd(ctx context.Context, args []string) int { |
| 166 | if len(args) > 0 { |
| 167 | fmt.Fprintln(e.io.Stderr, "Usage: agent-vm install") |
| 168 | return 2 |
| 169 | } |
| 170 | w := e.io.Stdout |
| 171 | exe, err := self() |
| 172 | if err != nil { |
| 173 | fmt.Fprintf(e.io.Stderr, "Error: cannot find this executable: %v\n", err) |
| 174 | return 1 |
| 175 | } |
| 176 | link, err := binLink() |
| 177 | if err != nil { |
| 178 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 179 | return 1 |
| 180 | } |
| 181 | dir := filepath.Dir(link) |
| 182 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 183 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 184 | return 1 |
| 185 | } |
| 186 | _, lerr := os.Lstat(link) |
| 187 | isOurs := lerr == nil && ours(link, exe) |
| 188 | if lerr == nil && !isOurs && e.earlier(link) { |
| 189 | if err := os.Remove(link); err != nil { |
| 190 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 191 | return 1 |
| 192 | } |
| 193 | fmt.Fprintf(w, "Replacing %s, from an earlier install\n", link) |
| 194 | lerr = os.ErrNotExist |
| 195 | } |
| 196 | var serr error |
| 197 | if lerr != nil { |
| 198 | serr = symlink(exe, link) |
| 199 | } |
| 200 | switch { |
| 201 | case isOurs: |
| 202 | fmt.Fprintf(w, "agent-vm is already linked at %s\n", link) |
| 203 | case lerr == nil: |
| 204 | fmt.Fprintf(e.io.Stderr, "Error: %s already exists and is not a link to %s.\n Move it aside, or set AGENT_VM_BIN_DIR to another directory.\n", link, exe) |
| 205 | return 1 |
| 206 | case serr == nil: |
| 207 | fmt.Fprintf(w, "Linked %s -> %s\n", link, exe) |
| 208 | case !symlinkUnsupported(serr): |
| 209 | fmt.Fprintf(e.io.Stderr, "Error: could not link %s to %s: %v\n", link, exe, serr) |
| 210 | return 1 |
| 211 | default: |
| 212 | if err := copyFile(exe, link); err != nil { |
| 213 | fmt.Fprintf(e.io.Stderr, "Error: could not copy %s to %s: %v\n", exe, link, err) |
| 214 | return 1 |
| 215 | } |
| 216 | if err := e.state.Write(installedCopy, []byte(sha256Of(link)+"\n")); err != nil { |
| 217 | fmt.Fprintf(e.io.Stderr, "Warning: %v: the next install will not recognise this copy.\n", err) |
| 218 | } |
| 219 | fmt.Fprintf(w, "Installed %s, a copy of %s (links are not allowed here: %v)\n", link, exe, serr) |
| 220 | } |
| 221 | if !onPath(dir) { |
| 222 | if paths.Windows() { |
| 223 | fmt.Fprintf(w, "%s is not on your PATH. Add it from PowerShell, then open a new terminal:\n [Environment]::SetEnvironmentVariable('Path', '%s;' + [Environment]::GetEnvironmentVariable('Path', 'User'), 'User')\n", |
| 224 | dir, strings.ReplaceAll(dir, "'", "''")) |
| 225 | } else { |
| 226 | fmt.Fprintf(w, "%s is not on your PATH. Add this line to your shell rc:\n export PATH=\"%s:$PATH\"\n", dir, dir) |
| 227 | } |
| 228 | } |
| 229 | if rcSourcesUs() { |
| 230 | fmt.Fprintln(w, "Your shell rc still sources agent-vm.sh, which this agent-vm replaces: remove that line.") |
| 231 | } |
| 232 | fmt.Fprintf(w, "\nagent-vm %s installed.\n", version.Version) |
| 233 | |
| 234 | if b, err := e.readOnlyBackend(); err == nil { |
| 235 | if base, err := b.Get(ctx, vmname.Template); err == nil && base != nil { |
| 236 | if _, err := os.Stat(e.state.Path(state.BaseVersion)); err == nil { |
| 237 | fmt.Fprintln(w, "Next: cd your-project && agent-vm claude # or opencode, codex, vibe, pi") |
| 238 | return 0 |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | if e.ui.CanAsk() && e.ui.AskYN("Build the base VM now with 'agent-vm setup'? It takes a few minutes.", true) { |
| 243 | fmt.Fprintln(w) |
| 244 | return e.setupCmd(ctx, nil) |
| 245 | } |
| 246 | fmt.Fprintln(w, "Next:\n agent-vm setup # build the base VM, once") |
| 247 | return 0 |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | var symlink = os.Symlink |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | func symlinkUnsupported(err error) bool { |
| 259 | const errPrivilegeNotHeld = syscall.Errno(1314) |
| 260 | return errors.Is(err, errors.ErrUnsupported) || errors.Is(err, syscall.EPERM) || errors.Is(err, errPrivilegeNotHeld) |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | func copyFile(src, dst string) error { |
| 267 | in, err := os.Open(src) |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | defer in.Close() |
| 272 | out, err := os.CreateTemp(filepath.Dir(dst), ".agent-vm-install-*") |
| 273 | if err != nil { |
| 274 | return err |
| 275 | } |
| 276 | _, err = io.Copy(out, in) |
| 277 | if err == nil { |
| 278 | err = out.Chmod(0o755) |
| 279 | } |
| 280 | if cerr := out.Close(); err == nil { |
| 281 | err = cerr |
| 282 | } |
| 283 | if err == nil { |
| 284 | err = os.Rename(out.Name(), dst) |
| 285 | } |
| 286 | if err != nil { |
| 287 | os.Remove(out.Name()) |
| 288 | } |
| 289 | return err |
| 290 | } |
| 291 | |
| 292 | |
| 293 | |
| 294 | func (e *app) uninstallCmd(args []string) int { |
| 295 | if len(args) > 0 { |
| 296 | fmt.Fprintln(e.io.Stderr, "Usage: agent-vm uninstall") |
| 297 | return 2 |
| 298 | } |
| 299 | w := e.io.Stdout |
| 300 | link, err := binLink() |
| 301 | if err != nil { |
| 302 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 303 | return 1 |
| 304 | } |
| 305 | exe, _ := self() |
| 306 | _, lerr := os.Lstat(link) |
| 307 | switch { |
| 308 | case lerr == nil && (exe != "" && ours(link, exe) || e.earlier(link)): |
| 309 | if err := os.Remove(link); err != nil { |
| 310 | fmt.Fprintf(e.io.Stderr, "Error: %v\n", err) |
| 311 | return 1 |
| 312 | } |
| 313 | fmt.Fprintf(w, "Removed %s\n", link) |
| 314 | case lerr == nil: |
| 315 | fmt.Fprintf(w, "%s is not a link to this agent-vm: left alone.\n", link) |
| 316 | default: |
| 317 | fmt.Fprintf(w, "No link to remove at %s\n", link) |
| 318 | } |
| 319 | if rcSourcesUs() { |
| 320 | fmt.Fprintln(w, "Your shell rc still sources agent-vm.sh: remove that line to drop the shell function.") |
| 321 | } |
| 322 | fmt.Fprintf(w, "Left as they are: the VMs ('agent-vm destroy-all' deletes them, run it first), ~/.agent-vm, and agent-vm itself at %s.\n", exe) |
| 323 | return 0 |
| 324 | } |