Files
magpie/internal/cli/library_project_pi_cli.go
yetone 54b9516473 library: a project's Pi packages, on, off or removed, and adopting its own (#7249)
A project now takes Pi packages: entries of its .pi/settings.json
`packages`, matched by piSourceID. A package is on (its source), off but
kept (every filter empty, as `pi config` writes it) or removed. Project.Pi
is what the project wants, PiRecs what magpie wrote; only an entry still as
magpie wrote it is changed or taken out, and a file magpie made goes with
its last entry. An entry the project had is its own: refused with a problem
until adopted, then switched without a rewrite, and put back as it was (never
deleted) when let go; one that picks some resources is refused.

Surfaces: POST /api/library/projects/pi (how on|off|removed|adopt),
`magpie library project pi [list|add|on|off|rm|adopt] <dir> <source...>`,
the project's own packages in Own["pi"], and a Pi packages block on the
Claude plugins tab's project card (switch, remove, Take over, add field) in
en, zh, zh-TW, ja, de. Pi installs a package itself when it next starts.

Run (mote): gofmt, go vet; go test ./internal/library ./internal/cli
./internal/gui (library, cli ok; gui red only in TestBuiltinsTranslated
#7257 and TestSettingsSaveKeepsWhatItDoesNotSend #7240, both filed);
GUI project-pi (new), project-own, project-plugins, project-hooks,
project-claude-plugins, project-servers, i18n-keys in chromium and webkit.
Not tried: a real Pi starting in a project with these packages; the whole
GUI suite; Windows.
2026-11-20 05:46:18 +08:00

131 lines
3.1 KiB
Go

package cli
import (
"encoding/json"
"fmt"
"os"
"slices"
"strings"
"github.com/yetone/magpie/internal/library"
)
// projectPiCmd is magpie library project pi …: the packages of a project's
// .pi/settings.json (#7249), through the calls the Library page makes.
func projectPiCmd(rest []string, usage func() error) error {
if len(rest) == 0 {
return usage()
}
verb, args := rest[0], rest[1:]
if !slices.Contains([]string{"add", "on", "off", "rm", "remove", "adopt", "list"}, verb) {
verb, args = "list", rest
}
if verb == "list" {
asJSON := slices.Contains(args, "--json")
args = slices.DeleteFunc(slices.Clone(args), func(a string) bool { return a == "--json" })
if len(args) != 1 || strings.HasPrefix(args[0], "-") {
return usage()
}
dir, err := projectDir(args[0])
if err != nil {
return err
}
return listProjectPi(dir, asJSON)
}
if len(args) < 2 || strings.HasPrefix(args[0], "-") {
return usage()
}
dir, err := projectDir(args[0])
if err != nil {
return err
}
var bad []string
for _, src := range args[1:] {
var res *library.Result
switch verb {
case "add", "on":
res, err = library.ProjectPi(dir, src, "on")
case "off":
res, err = library.ProjectPi(dir, src, "off")
case "rm", "remove":
res, err = library.ProjectPi(dir, src, "removed")
case "adopt":
res, err = library.ProjectPiAdopt(dir, src)
}
if err != nil {
fmt.Println(amber.Render("!"), src, muted.Render(err.Error()))
bad = append(bad, src)
continue
}
v, err := library.Read(res.Problems)
if err != nil {
return err
}
p := projectView(v, dir)
if p == nil {
return fmt.Errorf("%s isn't a project", dir)
}
if why := p.Problems["pi:"+src]; why != "" {
fmt.Println(amber.Render("!"), src, muted.Render(why))
bad = append(bad, src)
continue
}
state := "removed"
for _, x := range p.Pi {
if x.Source == src {
state = x.State
}
}
fmt.Println(green.Render("✓"), src, muted.Render(state))
}
if len(bad) > 0 {
return fmt.Errorf("%s not changed in %s", strings.Join(bad, ", "), dir)
}
return nil
}
func listProjectPi(dir string, asJSON bool) error {
v, err := library.Read(nil)
if err != nil {
return err
}
p := projectView(v, dir)
if p == nil {
return fmt.Errorf("%s isn't a project", dir)
}
type item struct {
Source string `json:"source"`
State string `json:"state"`
Adopted bool `json:"adopted,omitempty"`
By string `json:"by"` // "magpie" or "project"
}
items := []item{}
for _, x := range p.Pi {
items = append(items, item{x.Source, x.State, x.Adopted, "magpie"})
}
for _, o := range p.Own["pi"] {
st := "on"
if o.Event == "off" {
st = "off"
}
items = append(items, item{o.Name, st, false, "project"})
}
if asJSON {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(map[string]any{"dir": dir, "items": items})
}
if len(items) == 0 {
fmt.Println(muted.Render("nothing for Pi in " + dir))
return nil
}
for _, it := range items {
who := it.By
if it.Adopted {
who = "adopted"
}
fmt.Println(it.Source, muted.Render(it.State+" · "+who))
}
return nil
}