Files
magpie/internal/cli/library_project_export.go
T
yetone 06db81530e library: a project's choices exported from the CLI and applied at another folder (#7278)
jinpwng asked to carry one project's Library choices, with what they
need, to a new path (a new checkout, another machine) from the CLI.

- `magpie library project export <dir> [file|-]` writes ExportProject's
  JSON (kind magpie-project, version 1): the project's skills, servers,
  subagents, hooks and plugins by agent, its Claude Code plugins, Pi
  packages, servers and skills turned off, and its instructions, plus
  need: each hook's and subagent's definition, each skill's and plugin's
  remote source; a plugin's own hooks and subagents go by their plugin.
  No ownership record (Placed, HookRecs, AgentFiles, PiRecs, ClaudeWrote,
  InstrFiles), key, sign-in or other project goes in; a server goes by
  name only, since its settings can hold a key. The file is 0600.
- `apply <file|-> <dir>` (also `import`) makes the folder a project,
  saves a hook or subagent the library here lacks from the export (for no
  agent of its own), and gives each choice through the setters the page
  uses: the agents the folder has for it kept, the export's added, so a
  second apply changes nothing and the folder's own entries stay. A Claude
  plugin or Pi package the folder has already is left as it is;
  instructions only when it has none. What this library lacks (a skill,
  server or plugin) is named with its source and exits 1; nothing is
  fetched.

Run (mote): go test ./internal/library -run 'TestProjectExportApply'
-count=1: ok; the full suite is in the push record.
Real CLI (built binary, two sandbox HOMEs with stub claude and pi on
PATH): in A, project add, hook bark and subagent checker for claude, Pi
npm:pi-demo, export; A deleted; in B (its own "echo mine" Stop hook in
.claude/settings.local.json) apply saved bark and checker and printed
"has what the export has", a second import printed the same and added
nothing; B's settings.local.json held SessionStart "echo bark" beside
"echo mine", .claude/agents/checker.md was written and .pi/settings.json
listed npm:pi-demo.
Broken by hand: with the hook save in ApplyProject skipped,
TestProjectExportApply is red.
Not tried: the reporter's own projects; a Windows path in an export.
2026-11-20 12:06:44 +08:00

90 lines
2.1 KiB
Go

package cli
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/yetone/magpie/internal/library"
)
// projectExportCmd is magpie library project export <dir> [file] (#7278).
func projectExportCmd(rest []string, usage func() error) error {
if len(rest) < 1 || len(rest) > 2 || strings.HasPrefix(rest[0], "-") {
return usage()
}
dir, err := projectDir(rest[0])
if err != nil {
return err
}
e, err := library.ExportProject(dir)
if err != nil {
return err
}
b, err := json.MarshalIndent(e, "", " ")
if err != nil {
return err
}
b = append(b, '\n')
if len(rest) == 1 || rest[1] == "-" {
_, err = os.Stdout.Write(b)
return err
}
if err := os.WriteFile(rest[1], b, 0o600); err != nil {
return err
}
fmt.Println(green.Render("✓"), dir, "→", rest[1])
return nil
}
// projectApplyCmd is magpie library project apply <file|-> <dir>.
func projectApplyCmd(rest []string, usage func() error) error {
if len(rest) != 2 || rest[0] != "-" && strings.HasPrefix(rest[0], "-") || strings.HasPrefix(rest[1], "-") {
return usage()
}
var (
b []byte
err error
)
if rest[0] == "-" {
b, err = io.ReadAll(os.Stdin)
} else {
b, err = os.ReadFile(rest[0])
}
if err != nil {
return err
}
var e library.ProjectExport
if err := json.Unmarshal(b, &e); err != nil {
return fmt.Errorf("%s: %w", rest[0], err)
}
dir, err := library.ProjectPath(rest[1])
if err != nil {
return err
}
out, err := library.ApplyProject(dir, &e)
if err != nil {
return err
}
var problems []library.Problem
for _, r := range out.Res {
problems = append(problems, r.Problems...)
}
for _, s := range out.Saved {
fmt.Println(green.Render("+"), s, muted.Render("saved in the library, for no agent of its own"))
}
if err := projectOutcome(dir, nil, &library.Result{Problems: problems}); err != nil {
return err
}
for _, m := range out.Missing {
fmt.Println(amber.Render("!"), m)
}
if len(out.Missing) > 0 {
return fmt.Errorf("%d of the export's choices left out of %s", len(out.Missing), dir)
}
fmt.Println(green.Render("✓"), dir, "has what the export has")
return nil
}