* fix(agent): installs without Node ask for it the way that works The commands that install Node first, for a machine without it (#727), failed in ways that say nothing about the real problem. On a Mac or Linux, `curl -o- … | bash` keeps going when the download fails: a pipe's status is the last command's, so blocked or 404'd (raw.githubusercontent.com is commonly unreachable), bash reads nothing, still exits 0, and the user is told `nvm.sh: No such file or directory` — the network's fault told as a missing file. The vendor commands beside it all curl with -f (install.go's own scripts do); this one now does too, so a failed download stops the line with curl's own words. On Windows, winget asks to accept the msstore source's agreements the first time it runs, which waits for input a copy-pasted command can't give: Node isn't installed, and npm.cmd isn't found. The command now carries --accept-source-agreements and --accept-package-agreements, the flags Microsoft documents for unattended installs; they are idempotent where the source is already accepted. TestInstallCommandsWithoutNode expects the two commands as they now stand, and fails on the old ones ("curl -o- …" and the winget line without the flags, on darwin, linux and windows alike); TestInstallWithoutNodeParses still reads each line with `bash -n`. Validation on Windows: go test ./internal/agent under a temp HOME leaves the three Unix-only failures the base commit leaves (TestCodexStaleOnly ForMagpiesChange, TestSuffixModesReachAgentFiles, TestWSLProbeScript SaysWhere); go vet ./internal/... and the darwin, linux and windows builds pass. * fix(agent): a failed Node download stops the line where it failed yetone's review of the first cut: `curl -fsSL … | bash` doesn't stop at a failed download either, and my claim that -f alone fixes it was wrong — a pipe's status is the shell's, bash reading nothing exits 0, and the && chain goes on to source nvm.sh, which reports the missing file exactly as before. Reproduced against a 404 server on this machine: `curl: (22) … 404` then `bash: …/.nvm/nvm.sh: No such file or directory`. The command downloads to a file now and runs that: a failed download ends the chain at curl, with curl's own words about why. pipefail isn't an option — the line is pasted into the user's own shell, and not every one of them sets it. TestInstallWithoutNodeStopsWhenTheDownloadFails runs the real line, against a local 404 server with a temporary HOME, on each POSIX shell here: the old line failed with the download's error *and* nvm.sh missing, the new one stops at curl's own error and never mentions nvm.sh. It is skipped where there is no curl. TestInstallCommandsWithoutNode expects the line as it now stands. Validation on Windows: go test ./internal/agent under a temp HOME leaves the three Unix-only failures the base commit leaves (TestCodexStaleOnly ForMagpiesChange, TestSuffixModesReachAgentFiles, TestWSLProbeScript SaysWhere); go vet ./internal/..., the darwin, GOOS=linux and GOOS=windows builds pass.
184 lines
7.0 KiB
Go
184 lines
7.0 KiB
Go
package agent
|
|
|
|
// The agents magpie knows that aren't on this machine, with the commands
|
|
// their vendors give to install them (#727): on a new computer the Agents
|
|
// page said only "Install Claude Code, Codex, Gemini CLI, OpenCode…", and
|
|
// which command installs each was left to be looked up. magpie shows the
|
|
// commands to copy and run in a terminal; it doesn't run them itself, as an
|
|
// install needs what a new machine may not have yet (Node.js for npm,
|
|
// Homebrew), and a global npm prefix that may want sudo.
|
|
//
|
|
// Where no Node.js is found, each npm command installs it first (#727,
|
|
// Sun1090: "curl xxx && npm xxxx"): nvm's installer and its LTS Node on a
|
|
// Mac or Linux, as nodejs.org's download page gives, winget's Node.js LTS
|
|
// on Windows; the npm install follows in the same line.
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// InstallCmd is one way to install an agent's CLI.
|
|
type InstallCmd struct {
|
|
// Via is what runs it: script (the vendor's installer, in a shell),
|
|
// powershell (the vendor's installer, on Windows), brew or npm
|
|
Via string `json:"via"`
|
|
Command string `json:"command"`
|
|
// Node is how the command installs Node.js before npm, where none is
|
|
// here: nvm (Linux), nvm-mac (a Mac, where nvm's installer wants the
|
|
// Xcode Command Line Tools) or winget (Windows); "" when it doesn't
|
|
Node string `json:"node,omitempty"`
|
|
}
|
|
|
|
// Install is an agent not on this machine, and how to install it.
|
|
type Install struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Icon string `json:"icon,omitempty"`
|
|
Commands []InstallCmd `json:"commands"`
|
|
}
|
|
|
|
// vendorInstall is the installer each vendor's own docs give first, where
|
|
// it isn't a package manager's — Claude Code's (code.claude.com/docs/en/setup),
|
|
// Codex's (github.com/openai/codex's README) and OpenCode's
|
|
// (opencode.ai/docs), the ones whose own updaters cliSpecs knows by where
|
|
// they put the binary — and the Homebrew cask and formula the READMEs name
|
|
// (Codex's, Gemini CLI's).
|
|
var vendorInstall = map[string]func(goos string) []InstallCmd{
|
|
"claude": func(goos string) []InstallCmd {
|
|
if goos == "windows" {
|
|
return []InstallCmd{{Via: "powershell", Command: "irm https://claude.ai/install.ps1 | iex"}}
|
|
}
|
|
return []InstallCmd{{Via: "script", Command: "curl -fsSL https://claude.ai/install.sh | bash"}}
|
|
},
|
|
"opencode": func(goos string) []InstallCmd {
|
|
if goos == "windows" {
|
|
return nil
|
|
}
|
|
return []InstallCmd{{Via: "script", Command: "curl -fsSL https://opencode.ai/install | bash"}}
|
|
},
|
|
"codex": func(goos string) []InstallCmd {
|
|
switch goos {
|
|
case "windows":
|
|
return []InstallCmd{{Via: "powershell", Command: `powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"`}}
|
|
case "darwin":
|
|
return []InstallCmd{{Via: "script", Command: "curl -fsSL https://chatgpt.com/codex/install.sh | sh"}, {Via: "brew", Command: "brew install --cask codex"}}
|
|
}
|
|
return []InstallCmd{{Via: "script", Command: "curl -fsSL https://chatgpt.com/codex/install.sh | sh"}}
|
|
},
|
|
"gemini": func(goos string) []InstallCmd {
|
|
if goos == "windows" {
|
|
return nil
|
|
}
|
|
return []InstallCmd{{Via: "brew", Command: "brew install gemini-cli"}}
|
|
},
|
|
}
|
|
|
|
// installCommands is how to install the agent with the given id on goos:
|
|
// the vendor's installer first, then npm's package, the one cliSpecs
|
|
// updates, after Node.js's own install when node is false; nil for one
|
|
// magpie knows no command for.
|
|
func installCommands(id, goos string, node bool) []InstallCmd {
|
|
var out []InstallCmd
|
|
if f := vendorInstall[id]; f != nil {
|
|
out = append(out, f(goos)...)
|
|
}
|
|
if spec, ok := cliSpecs[id]; ok && len(spec.npm) > 0 {
|
|
out = append(out, npmInstall(spec.npm[0], goos, node))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// nvmVersion is the nvm release whose installer the commands fetch
|
|
// (github.com/nvm-sh/nvm/releases/latest).
|
|
const nvmVersion = "v0.40.8"
|
|
|
|
// npmInstall is the command installing pkg with npm on goos, with Node.js
|
|
// installed first when node is false.
|
|
//
|
|
// On a Mac or Linux: nvm's installer, then nvm.sh read into the shell the
|
|
// command runs in (the installer adds it to the profile, which only the
|
|
// terminals opened after read), its LTS Node, then npm. NVM_DIR is set
|
|
// and its folder made, as the installer would go by $XDG_CONFIG_HOME/nvm
|
|
// where that is set, and nvm.sh couldn't be found then. nvm's Node is the
|
|
// user's own, so the global install wants no sudo.
|
|
//
|
|
// On Windows, in PowerShell: winget's Node.js LTS, then PATH read again
|
|
// from the registry (the installer adds Node's folder there, not to this
|
|
// terminal), then npm.cmd — npm alone is npm.ps1 there, which PowerShell's
|
|
// default execution policy refuses to run. Each step is parted by ";", as
|
|
// Windows PowerShell 5.1 has no "&&".
|
|
func npmInstall(pkg, goos string, node bool) InstallCmd {
|
|
plain := "npm install -g " + pkg
|
|
switch {
|
|
case node:
|
|
return InstallCmd{Via: "npm", Command: plain}
|
|
case goos == "windows":
|
|
return InstallCmd{Via: "npm", Node: "winget", Command: "winget install -e --id OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements; " +
|
|
"$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [Environment]::GetEnvironmentVariable('Path','User'); " +
|
|
"npm.cmd install -g " + pkg}
|
|
}
|
|
how := "nvm"
|
|
if goos == "darwin" {
|
|
how = "nvm-mac"
|
|
}
|
|
return InstallCmd{Via: "npm", Node: how, Command: `export NVM_DIR="$HOME/.nvm" && mkdir -p "$NVM_DIR" && ` +
|
|
"t=$(mktemp) && curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/" + nvmVersion + `/install.sh -o "$t" && ` +
|
|
`bash "$t" && . "$NVM_DIR/nvm.sh" && nvm install --lts && ` + plain}
|
|
}
|
|
|
|
// nodeHere says whether this machine has Node.js's npm: on PATH, in one
|
|
// of the folders a user's tools go in (nvm's, fnm's, mise's, volta's,
|
|
// Homebrew's, …) or, on Windows, in the PATH a terminal opened now has
|
|
// and in Node.js's installer's own folder.
|
|
func nodeHere() bool {
|
|
if _, err := exec.LookPath("npm"); err == nil {
|
|
return true
|
|
}
|
|
dirs := proc.UserBinDirs()
|
|
if runtime.GOOS == "windows" {
|
|
dirs = append(dirs, proc.LoginPath()...)
|
|
if pf := os.Getenv("ProgramFiles"); pf != "" {
|
|
dirs = append(dirs, filepath.Join(pf, "nodejs"))
|
|
}
|
|
}
|
|
return npmIn(dirs, runtime.GOOS)
|
|
}
|
|
|
|
// npmIn says whether one of dirs has npm in it.
|
|
func npmIn(dirs []string, goos string) bool {
|
|
name := "npm"
|
|
if goos == "windows" {
|
|
name = "npm.cmd"
|
|
}
|
|
return slices.ContainsFunc(dirs, func(d string) bool {
|
|
return d != "" && isFile(filepath.Join(d, name))
|
|
})
|
|
}
|
|
|
|
// Installs is every agent magpie knows a command for that isn't on this
|
|
// machine, in the Agents page's order.
|
|
func Installs() []Install {
|
|
return installsOf(All(), runtime.GOOS, nodeHere())
|
|
}
|
|
|
|
func installsOf(all []*Agent, goos string, node bool) []Install {
|
|
out := []Install{}
|
|
for _, a := range all {
|
|
if a.WSL != "" || a.Detected() {
|
|
continue
|
|
}
|
|
cmds := installCommands(a.ID, goos, node)
|
|
if len(cmds) == 0 {
|
|
continue
|
|
}
|
|
out = append(out, Install{ID: a.ID, Name: a.Name, Icon: a.Icon, Commands: cmds})
|
|
}
|
|
return out
|
|
}
|