Files
magpie/internal/cli/backup_cmd.go
T
yetone 9298d2a7c3 cli: prompts read the stdin Windows' console init reopens, not the one package init saw
tom047784 (Discord, Windows desktop build): `magpie plugin login` in a
terminal failed with "read /dev/stdin: The handle is invalid". The
desktop build is a GUI-subsystem binary; console_windows.go's init
attaches the parent console and reopens CONIN$/CONOUT$ into os.Stdin,
os.Stdout and os.Stderr. But `var stdin = bufio.NewReader(os.Stdin)` in
backup_cmd.go is a package variable, set before any init runs, so it
kept the invalid handle.

That one reader serves every CLI prompt, so all of them broke the same
way: plugin login (askPrompt, askNumber, the code and pasted callback
lines), backup/WebDAV passphrases and passwords piped in (secret), the
library and quota confirmations. stdin is now a func that makes the
reader over os.Stdin as it is when read, and remakes it if os.Stdin
changed; one reader still serves a run, so a second piped secret is
still the second line.

Sibling of the same class: lipgloss's default renderer (termenv's
default output) is made over os.Stdout at its package init, before the
reattach, so the desktop build's CLI printed every style without colour.
The init now points that renderer at CONOUT$ in place; the styles made
at package init hold it. A grep for package-level os.Stdin/os.Stdout/
os.Stderr found no other one in the repo (writers like fmt.Println and
json.NewEncoder(os.Stdout) inside functions resolve at use time).

webdav_cli_test's piped() swapped both os.Stdin and the reader, which is
why no test caught this; it now swaps os.Stdin only.

Run (at this tree, worktree off origin/main 4e3078ee4):
- go test -count=1 -timeout 45m ./internal/cli/: ok (52s)
- go test -race -count=3 TestPromptsReadStdinSetAfterInit|TestAskPrompt|TestWebdav: ok
- gofmt -l internal/cli: empty; go vet ./internal/cli, GOOS=linux go vet
  -tags nogui ./internal/cli, GOOS=windows go vet ./internal/cli: ok
Broken by hand: TestPromptsReadStdinSetAfterInit on the old backup_cmd,
plugins_cli, library_cli and quota_cli (new test file only) is red:
"askPrompt = "", EOF; want blue from the stdin set after init".
Not tried on Windows: the Windows box (10.0.0.132) timed out on ssh, so
the real desktop build's `magpie plugin login` and the lipgloss colour
change were not run there. Not run: go test ./... (only internal/cli
changed).
2026-10-26 22:22:27 +08:00

207 lines
5.8 KiB
Go

package cli
import (
"bufio"
"errors"
"fmt"
"maps"
"os"
"slices"
"strings"
"sync"
"golang.org/x/term"
"github.com/yetone/magpie/internal/backup"
"github.com/yetone/magpie/internal/edit"
)
// backupCmd writes every provider, setting, profile and agent model, and
// the library, to one file sealed with a passphrase:
// magpie backup [--no-keys] [--no-library] [file].
func backupCmd(args []string) error {
keys, lib, file := true, true, ""
for _, a := range args {
switch {
case a == "--no-keys":
keys = false
case a == "--no-library":
lib = false
case strings.HasPrefix(a, "-"):
return fmt.Errorf("unknown flag %s (magpie backup [--no-keys] [--no-library] [file])", a)
case file == "":
file = a
default:
return errors.New("usage: magpie backup [--no-keys] [--no-library] [file]")
}
}
if file == "" {
file = "magpie" + backup.Ext
}
b, err := backup.Collect(keys, version)
if err != nil {
return err
}
if !lib {
b.Library = nil
}
pass, err := passphrase("Passphrase for the backup: ", true)
if err != nil {
return err
}
data, err := backup.Seal(b, pass)
if err != nil {
return err
}
if err := edit.WriteAtomic(file, data); err != nil {
return err
}
os.Chmod(file, 0o600)
what := "with their keys"
if !keys {
what = "without keys"
}
fmt.Printf("%s %s: %d providers %s, %d profiles, %d agent settings",
green.Render("saved"), file, len(b.Providers), what, len(b.Profiles), len(b.Agents))
if l := b.Library; l != nil {
fmt.Printf("; library: %d MCP servers, %d skills", len(l.MCP), len(l.Skills))
}
fmt.Println()
fmt.Println(muted.Render("Subscriptions are not in it: sign in to them on the other machine. Keep the passphrase: without it the file can't be opened."))
return nil
}
// restoreCmd puts a backup in: magpie restore [--no-agents] [--no-library] <file>.
func restoreCmd(args []string) error {
parts, file := backup.All, ""
for _, a := range args {
switch {
case a == "--no-agents":
parts.Agents = false
case a == "--no-library":
parts.Library = false
case strings.HasPrefix(a, "-"):
return fmt.Errorf("unknown flag %s (magpie restore [--no-agents] [--no-library] <file>)", a)
case file == "":
file = a
default:
return errors.New("usage: magpie restore [--no-agents] [--no-library] <file>")
}
}
if file == "" {
return errors.New("usage: magpie restore [--no-agents] [--no-library] <file>")
}
data, err := os.ReadFile(file)
if err != nil {
return err
}
pass, err := passphrase("Passphrase: ", false)
if err != nil {
return err
}
b, err := backup.Open(data, pass)
if err != nil {
return err
}
r, err := backup.Restore(b, parts)
if err != nil {
return err
}
fmt.Printf("%s providers: %d added, %d replaced", green.Render("restored"), r.Added, r.Replaced)
if r.Settings {
fmt.Print("; settings")
}
fmt.Printf("; %d profiles; %d agent settings changed", r.Profiles, r.Agents)
if r.Library {
fmt.Print("; library")
}
fmt.Println()
if len(r.NeedKey) > 0 {
fmt.Println(muted.Render("Needs a key (magpie provider key <id>): " + strings.Join(r.NeedKey, ", ")))
}
if len(r.SignIn) > 0 {
fmt.Println(muted.Render("The backup's settings use these, not signed in on this computer (a backup carries settings, not sign-ins; to use one here: magpie accounts add <id>): " + strings.Join(r.SignIn, ", ")))
}
if len(r.Plugins) > 0 {
fmt.Println(muted.Render("Plugins added: " + strings.Join(r.Plugins, ", ")))
}
for _, spec := range slices.Sorted(maps.Keys(r.PluginErrors)) {
fmt.Println(muted.Render(fmt.Sprintf("Couldn't add the plugin %s: %s", spec, r.PluginErrors[spec])))
}
for _, p := range r.LibraryProblems {
fmt.Println(muted.Render(fmt.Sprintf("Library: %s couldn't get %s: %s", p.Agent, p.What, p.Error)))
}
if len(r.Skipped) > 0 {
fmt.Println(muted.Render("Left as they are (agent not here, or its model can't be reached yet): " + strings.Join(r.Skipped, ", ")))
}
return nil
}
// passphrase reads one from the terminal without echo, asked twice when
// it is being set; piped in, it is the next line of stdin.
func passphrase(prompt string, confirm bool) (string, error) {
return secret("passphrase", prompt, confirm)
}
// stdin is read through one reader, so a second secret piped in is the
// second line and not lost to the first one's buffer. The reader is made
// over os.Stdin as it is when first read, never at package init: package
// variables are set before any init runs, and on Windows the desktop
// build's init (console_windows.go) only then swaps in the terminal's
// CONIN$. A reader made earlier keeps the invalid handle, and every
// prompt fails with "read /dev/stdin: The handle is invalid".
var (
stdinMu sync.Mutex
stdinFile *os.File
stdinBuf *bufio.Reader
)
func stdin() *bufio.Reader {
stdinMu.Lock()
defer stdinMu.Unlock()
if stdinBuf == nil || stdinFile != os.Stdin {
stdinFile, stdinBuf = os.Stdin, bufio.NewReader(os.Stdin)
}
return stdinBuf
}
// secret reads what (a passphrase, a password) as passphrase does; piped
// in, it is the next line of stdin.
func secret(what, prompt string, confirm bool) (string, error) {
fd := int(os.Stdin.Fd())
if !term.IsTerminal(fd) {
line, err := stdin().ReadString('\n')
line = strings.TrimRight(line, "\r\n")
if line == "" {
if err != nil {
return "", fmt.Errorf("no %s on stdin: %w", what, err)
}
return "", fmt.Errorf("the %s is empty", what)
}
return line, nil
}
read := func(p string) (string, error) {
fmt.Fprint(os.Stderr, p)
b, err := term.ReadPassword(fd)
fmt.Fprintln(os.Stderr)
return string(b), err
}
pass, err := read(prompt)
if err != nil {
return "", err
}
if pass == "" {
return "", fmt.Errorf("the %s is empty", what)
}
if confirm {
again, err := read("Again: ")
if err != nil {
return "", err
}
if again != pass {
return "", fmt.Errorf("the %ss differ", what)
}
}
return pass, nil
}