From db812921a6fbb6b12559b11951dec15e1a01752b Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 22:08:24 +0800 Subject: [PATCH] =?UTF-8?q?plugins=5Fcli:=20magpie=20plugin=20off,=20on=20?= =?UTF-8?q?and=20rm=20take=20a=20plugin's=20short=20name,=20as=20options?= =?UTF-8?q?=20does=20(the=20owner,=20testing=20for=20real)=20=E2=80=94=20`?= =?UTF-8?q?magpie=20plugin=20off=20think-tags`=20answered=20`no=20plugin?= =?UTF-8?q?=20"think-tags"`=20while=20`magpie=20plugin=20options=20think-t?= =?UTF-8?q?ags`=20found=20it,=20so=20the=20names=20the=20community=20READM?= =?UTF-8?q?Es=20and=20usemagpie.ai's=20docs=20write=20worked=20for=20one?= =?UTF-8?q?=20command=20only.=20installedName=20turns=20a=20short=20name?= =?UTF-8?q?=20(think-tags=20for=20@magpie-community/middleware-think-tags,?= =?UTF-8?q?=20plugin.ShortName)=20into=20the=20installed=20plugin's=20pack?= =?UTF-8?q?age=20name=20for=20on,=20off=20and=20rm;=20a=20name=20a=20plugi?= =?UTF-8?q?n=20has=20itself,=20or=20one=20no=20plugin=20has,=20is=20kept?= =?UTF-8?q?=20as=20it=20was.=20Found=20and=20tested=20end=20to=20end=20in?= =?UTF-8?q?=20a=20sandbox=20HOME=20with=20a=20real=20DeepSeek=20account:?= =?UTF-8?q?=20Model=20Map=20and=20Think=20Tags=20installed=20by=20clicking?= =?UTF-8?q?=20Install=20under=20Plugins=20=E2=80=BA=20Discover=20=E2=80=BA?= =?UTF-8?q?=20Gateway=20middleware=20in=20the=20real=20app=20(from=20a=20l?= =?UTF-8?q?ocal=20verdaccio=20registry=20holding=20the=20real=20packages,?= =?UTF-8?q?=20until=20their=20first=20npm=20publish;=20magpie=20built=20wi?= =?UTF-8?q?th=20npmRegistry=20pointed=20at=20it),=20an=20alias=20set=20in?= =?UTF-8?q?=20Model=20Map's=20Options=20editor=20served=20by=20deepseek-fl?= =?UTF-8?q?ash=20and=20named=20back,=20think-tags=20off=20by=20short=20nam?= =?UTF-8?q?e=20let=20the=20model's=20=E2=80=A6=20through=20?= =?UTF-8?q?and=20on=20took=20it=20out=20("51"),=20rm=20think-tags=20by=20s?= =?UTF-8?q?hort=20name=20and=20Model=20Map's=20Remove=20in=20the=20app=20l?= =?UTF-8?q?eft=20plugins.json=20empty=20and=20the=20alias=20unknown.=20Tes?= =?UTF-8?q?tPluginOnOffRmShortName=20is=20new=20and=20fails=20without=20th?= =?UTF-8?q?e=20change.=20go=20vet,=20go=20build=20(GOOS=3Dlinux=20nogui,?= =?UTF-8?q?=20GOOS=3Dwindows)=20and=20go=20test=20-tags=20nogui=20./...=20?= =?UTF-8?q?under=20a=20temp=20HOME=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin_middleware_cli_test.go | 26 ++++++++++++++++++++++++++ plugins_cli.go | 21 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/plugin_middleware_cli_test.go b/plugin_middleware_cli_test.go index 5ba394722..3c825bc1a 100644 --- a/plugin_middleware_cli_test.go +++ b/plugin_middleware_cli_test.go @@ -42,3 +42,29 @@ func TestPluginListSaysMiddleware(t *testing.T) { t.Errorf("--json has no middleware:\n%s", js) } } + +// magpie plugin off, on and rm take a plugin's short name, as options +// does: `magpie plugin off think-tags` said there was no such plugin. +func TestPluginOnOffRmShortName(t *testing.T) { + groupsHome(t) + dir := filepath.Join(t.TempDir(), "middleware-zz-tags") + os.MkdirAll(dir, 0o755) + os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"name": "@magpie-community/middleware-zz-tags", "magpie": {"middleware": "./zz.middleware.js"}}`), 0o644) + os.WriteFile(filepath.Join(dir, "zz.middleware.js"), []byte("export function onRequest(b) {}\n"), 0o644) + if _, err := plugin.Add(context.Background(), dir); err != nil { + t.Fatal(err) + } + off := func() bool { return plugin.Load().Plugins[0].Off } + if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "off", "zz-tags"}) }); err != nil || !off() { + t.Fatalf("off by short name: err %v, off %v", err, off()) + } + if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "on", "zz-tags"}) }); err != nil || off() { + t.Fatalf("on by short name: err %v, off %v", err, off()) + } + if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "rm", "zz-tags"}) }); err != nil || len(plugin.Load().Plugins) != 0 { + t.Fatalf("rm by short name: err %v, left %v", err, plugin.Load().Plugins) + } + if err := pluginCmd([]string{"plugin", "off", "nothing-here"}); err == nil { + t.Error("a name no plugin has was taken") + } +} diff --git a/plugins_cli.go b/plugins_cli.go index 898dd032e..be677b819 100644 --- a/plugins_cli.go +++ b/plugins_cli.go @@ -58,6 +58,7 @@ func pluginCmd(args []string) error { if len(rest) != 1 { return errors.New(pluginUsage) } + rest[0] = installedName(rest[0]) back := provider.MovedOnto(rest[0]) if err := provider.RemovePlugin(ctx, rest[0]); err != nil { return err @@ -78,6 +79,7 @@ func pluginCmd(args []string) error { if len(rest) != 1 { return errors.New(pluginUsage) } + rest[0] = installedName(rest[0]) var back []string if sub == "off" { back = provider.MovedOnto(rest[0]) @@ -227,6 +229,25 @@ func listPlugins(ctx context.Context, asJSON bool) error { // pluginOptions prints a plugin's options, or sets them from a JSON // object; off takes them away. With none set, it prints what its // package suggests. +// installedName is the package name of the plugin a short name, as the +// community's READMEs write it (think-tags for +// @magpie-community/middleware-think-tags), stands for; a name a plugin +// has itself, or one no plugin has, is kept as it is. +func installedName(name string) string { + ps := plugin.Load().Plugins + for _, x := range ps { + if plugin.Name(x.Spec) == name || x.Spec == name { + return name + } + } + for _, x := range ps { + if plugin.ShortName(plugin.Name(x.Spec)) == name { + return plugin.Name(x.Spec) + } + } + return name +} + func pluginOptions(name string, set []string) error { var e *plugin.Entry ps := plugin.Load().Plugins