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