Files
magpie/internal/provider/install.go
T
yetone bf05291e2c provider: a Claude sign-in with no Claude Code downloads it first (Kabubu01)
Kabubu01 (X, 2117435171205259713) added a Claude account in the GUI on a
computer with no Claude Code: the card ended "登录未完成" with the English
"install Claude Code first, the `claude` command line … magpie signs in to
Claude through it; … `magpie claude-code install`". Every other
subscription used through its vendor's CLI (Devin, Cursor, Grok) installs
it when its sign-in starts; Claude's only said how.

cliFor("claude") is now Claude Code: found by findClaudeCLI (this
computer's, magpie's download, or WSL's), installed by
downloadClaudeCode, which is what `magpie claude-code install` does:
claudecode.Latest, then claudecode.Install, Anthropic's own build checked
against its manifest's SHA-256 and size, into magpie's cache, not on
PATH. claudeExecutable (claudecode.Find) finds it there, so the sign-in
goes on as before. The card shows the existing "Installing Claude Code…"
state (strings already translated). runInstaller uses a CLI's native
installer on every OS when it has no shell one-liner, and a failed
download's error names `magpie claude-code install` as the manual way.

What was run:
- TestClaudeSignInDownloadsClaudeCode (installing → paste page → done
  after the fake download) and TestClaudeSignInDownloadFails (the error
  carries the cause and `magpie claude-code install`), new, plus every
  -run 'SignIn' test, on mote: ok. isolate() fakes the download, so no
  test reaches downloads.claude.ai.
- Broken by hand: with cliFor's claude case renamed, both new tests red.
- go test ./internal/provider on mote: ok (160s).
- Windows (win-run, xiaomi-gpu): TestClaudeSignInDownloadFails,
  TestSignInInstallFails, TestSignInCanceledWhileInstalling,
  TestWindowsInstallerNotOnCommandLine ok; the download-then-sign-in test
  skips there, as every fake-claude sign-in test does (shell fakes).
- GOOS=linux go vet -tags nogui and GOOS=windows go vet of
  ./internal/provider on mote: ok. gofmt -l clean.
- Real download in a sandbox HOME on mote, PATH=/usr/bin:/bin:
  `magpie claude-code install --yes` (the same Latest+Install calls)
  fetched and checked Claude Code 2.1.317 into .cache/magpie/claude-code,
  and it ran (`2.1.317 (Claude Code)`).
- Rebased on a92e85ab2 (two provider commits): go test ./internal/provider
  on mote again: ok (138s).

Not changed: the gateway's "Claude Code is not installed" (a request
path; a 240MB download doesn't belong under a request).
Not tried: the GUI sign-in end to end with a real download, and a
download slower than installTimeout's 10 minutes (it fails with the
manual command).
2026-11-03 10:44:15 +08:00

239 lines
8.0 KiB
Go

package provider
// Some subscriptions are used through the vendor's own CLI — Claude
// Code, Cursor's, Grok's, Devin's — and signed in with it too. One that isn't installed is
// installed with the vendor's own installer when its sign-in starts, as the
// vendor's page tells a person to, rather than leaving the sign-in to fail
// or, as Devin's did, to finish with nothing to show for it.
import (
"bytes"
"context"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/yetone/magpie/internal/claudecode"
"github.com/yetone/magpie/internal/netproxy"
"github.com/yetone/magpie/internal/proc"
)
// agentCLI is a CLI a subscription runs through: how to find it, and the
// vendor's install one-liners for a POSIX shell and for PowerShell. native,
// when there is one, does what the shell one does without a shell, for a
// magpie with none (its Docker image had no bash, curl or wget until #2834). setup: the
// installer ends by running the CLI's own setup, which asks how to sign in;
// magpie signs in itself once the CLI is there. manual, when set, is how
// a person installs it, told when the install fails, where a CLI with no
// one-liner (Claude Code's, which magpie downloads itself) has native only.
type agentCLI struct {
Name string
find func() string
sh string
ps string
native func(ctx context.Context) error
manual string
setup bool
}
func cliFor(agent string) (agentCLI, bool) {
switch agent {
case "claude":
// Kabubu01 (X): the GUI's Claude sign-in failed with "install
// Claude Code first … or run `magpie claude-code install`". The
// sign-in now downloads it as that command does: Anthropic's own
// build, checked against its manifest, kept in magpie's cache and
// not put on PATH
return agentCLI{Name: "Claude Code", find: func() string {
c, ok := findClaudeCLI()
if ok && c.wsl != nil {
return "wsl:" + c.wsl.Distro
}
return c.path
}, native: downloadClaudeCode, manual: "magpie claude-code install"}, true
case "devin":
return agentCLI{Name: "Devin CLI", find: func() string { return DevinExecutable() },
sh: "curl -fsSL https://cli.devin.ai/install.sh | bash",
ps: "irm https://static.devin.ai/cli/setup.ps1 | iex",
setup: true}, true
case "cursor":
return agentCLI{Name: "Cursor CLI", find: func() string { return CursorExecutable() },
sh: "curl https://cursor.com/install -fsS | bash",
ps: "irm 'https://cursor.com/install?win32=true' | iex"}, true
case "grok":
return agentCLI{Name: "Grok Build", find: func() string { return GrokExecutable() },
sh: "curl -fsSL https://x.ai/cli/install.sh | bash",
ps: "irm https://x.ai/cli/install.ps1 | iex",
native: installGrokBuild}, true
}
return agentCLI{}, false
}
// missingCLI is the CLI an agent's sign-in needs and this machine lacks.
func missingCLI(agent string) (agentCLI, bool) {
c, ok := cliFor(agent)
if !ok || c.find() != "" {
return agentCLI{}, false
}
return c, true
}
// installTimeout bounds an installer: they download a few hundred MB at most.
var installTimeout = 10 * time.Minute
// runInstaller runs a vendor's installer; a var so tests can fake it.
var runInstaller = func(ctx context.Context, c agentCLI) ([]byte, error) {
if c.native != nil && (c.sh == "" && c.ps == "" || runtime.GOOS != "windows" && !shellInstallerRuns()) {
return nil, c.native(ctx)
}
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
// the one-liner goes to PowerShell on stdin, as one typed into it
// does: on PowerShell's command line, Microsoft Defender took
// "irm <url> | iex" for a Trojan (Trojan:Win32/Commando.A!ml) and
// stopped it before it ran, which came back as "Access is denied"
cmd = proc.CommandContext(ctx, "powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", "-")
cmd.Stdin = strings.NewReader(c.ps + "\n")
} else {
cmd = proc.CommandContext(ctx, "bash", "-c", c.sh)
}
cmd.Dir, _ = os.UserHomeDir()
cmd.Env = netproxy.Env(nil)
if runtime.GOOS == "windows" && c.setup {
return runUntilInstalled(ctx, cmd, c)
}
// nothing more on stdin: an installer that would ask something takes
// its default
return cmd.CombinedOutput()
}
// installerTail is how long an installer that ends in its CLI's setup is
// given to end by itself once the CLI is in place; a var so tests can
// shorten it.
var installerTail = 5 * time.Second
// runUntilInstalled runs an installer that ends in its CLI's setup, on
// Windows, and ends it, with what it started, installerTail after the CLI
// is in place. Devin's runs `devin setup`, whose menu asking how to sign in
// waits for a key on the console, and no one can answer the console an
// installer gets from magpie (it has no window, and magpie keeps its
// output): the sign-in waited out installTimeout, then went on with
// `devin setup` still running.
func runUntilInstalled(ctx context.Context, cmd *exec.Cmd, c agentCLI) ([]byte, error) {
var out bytes.Buffer
cmd.Stdout, cmd.Stderr = &out, &out
t, err := proc.StartTree(cmd)
if err != nil {
return nil, err
}
ended := make(chan struct{})
go func() {
tick := time.NewTicker(time.Second)
defer tick.Stop()
var tail <-chan time.Time
for {
select {
case <-ended:
return
case <-ctx.Done():
t.Kill()
return
case <-tail:
t.Kill()
return
case <-tick.C:
if tail != nil {
continue
}
// the installer puts the CLI's folder on the user's PATH,
// which a running program reads from the registry
refreshPath()
if c.find() != "" {
tail = time.After(installerTail)
}
}
}
}()
err = t.Wait()
close(ended)
return out.Bytes(), err
}
// downloadClaudeCode downloads the newest Claude Code into magpie's cache
// (claudecode.Install), where claudecode.Find, and so the sign-in, finds
// it; a var so tests never reach the network.
var downloadClaudeCode = func(ctx context.Context) error {
r, err := claudecode.Latest(ctx)
if err != nil {
return err
}
_, err = claudecode.Install(ctx, r, nil)
return err
}
// shellInstallerRuns is whether a vendor's `curl … | bash` can run here:
// bash, and curl, which every one-liner (and the scripts behind them)
// downloads with. magpie's Docker image had none of them, and the install
// failed there with "exec: bash: not found". Since 7bfe3137 it has bash and
// busybox's wget but still no curl, and the one-liner failed there too.
// Since #2834 it is Debian with bash and curl, and the CLIs the one-liners
// install find the loader and libraries they link. A var so tests can say
// there is none.
var shellInstallerRuns = func() bool {
for _, p := range []string{"bash", "curl"} {
if _, err := exec.LookPath(p); err != nil {
return false
}
}
return true
}
// installCLI installs c with its vendor's installer and makes sure magpie
// finds it afterwards.
func installCLI(ctx context.Context, c agentCLI) error {
ctx, cancel := context.WithTimeout(ctx, installTimeout)
defer cancel()
out, err := runInstaller(ctx, c)
// an installer adds its directory to the user's PATH, which a running
// program never sees
refreshPath()
manual := c.sh
if runtime.GOOS == "windows" {
manual = c.ps
}
if c.manual != "" {
manual = c.manual
}
if ctx.Err() == context.Canceled {
return ctx.Err()
}
// what counts is the CLI being there: an installer may end in a step
// that wants a terminal — Devin's runs `devin setup`, whose sign-in
// gives up without one — and fail after it installed everything
if c.find() != "" {
return nil
}
if err != nil {
msg := lastLine(out)
if msg == "" {
msg = err.Error()
}
return errorf("installing %s failed (%s); install it yourself with `%s`, then sign in again", c.Name, msg, manual)
}
return errorf("the %s installer finished, but magpie can't find it; install it yourself with `%s`, then sign in again", c.Name, manual)
}
// lastLine is the last line of a command's output with something on it,
// without its colours.
func lastLine(out []byte) string {
lines := strings.Split(ansi.ReplaceAllString(string(out), ""), "\n")
for i := len(lines) - 1; i >= 0; i-- {
if l := strings.TrimSpace(lines[i]); l != "" {
return l
}
}
return ""
}