From 52f44f7c2e8437d95c4cd388bd8d9f2f92bd73c2 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 5 Nov 2022 23:05:41 +0100 Subject: [PATCH] with handling for do --- cmd/char/do.go | 2 +- examples/basic/testdo.sh | 2 +- pkg/charcontext/char_context.go | 3 ++- pkg/register/plugin.go | 1 + pkg/register/plugin_client.go | 23 ++++++++++++++++++ pkg/register/plugin_register.go | 41 +++++++++++++++++++++++---------- pkg/register/plugin_server.go | 13 +++++++++++ plugins/gocli/main.go | 7 ++++++ 8 files changed, 77 insertions(+), 15 deletions(-) diff --git a/cmd/char/do.go b/cmd/char/do.go index 2c0ce8b..4ad577e 100644 --- a/cmd/char/do.go +++ b/cmd/char/do.go @@ -44,7 +44,7 @@ func NewDoCommand(charctx *charcontext.CharContext) *cobra.Command { return err } - if err := charctx.Do(cmd.Context(), a.Name, c.Name); err != nil { + if err := charctx.Do(cmd.Context(), a.ClientName, c.Name, nil); err != nil { return err } diff --git a/examples/basic/testdo.sh b/examples/basic/testdo.sh index 99d2db1..a2c7286 100755 --- a/examples/basic/testdo.sh +++ b/examples/basic/testdo.sh @@ -10,4 +10,4 @@ CHAR_DEV_MODE=true ./char do -h echo echo "--------" echo "local_up" -CHAR_DEV_MODE=false ./char do local_up -h +CHAR_DEV_MODE=false ./char do local_up --fish something diff --git a/pkg/charcontext/char_context.go b/pkg/charcontext/char_context.go index 8da8083..2d6dc84 100644 --- a/pkg/charcontext/char_context.go +++ b/pkg/charcontext/char_context.go @@ -65,6 +65,7 @@ func (cc *CharContext) About(ctx context.Context) ([]register.AboutItem, error) return cc.pluginRegister.About(ctx) } -func (cc *CharContext) Do(ctx context.Context, argName string, commandName string) error { +func (cc *CharContext) Do(ctx context.Context, clientName string, commandName string, args map[string]string) error { + cc.pluginRegister.Do(ctx, clientName, commandName, args) return nil } diff --git a/pkg/register/plugin.go b/pkg/register/plugin.go index 56f6618..fd492d9 100644 --- a/pkg/register/plugin.go +++ b/pkg/register/plugin.go @@ -18,6 +18,7 @@ type About struct { type Plugin interface { About(ctx context.Context) (*About, error) + Do(ctx context.Context, commandName string, args map[string]string) error } const PluginKey = "plugin" diff --git a/pkg/register/plugin_client.go b/pkg/register/plugin_client.go index 954acc6..9cead62 100644 --- a/pkg/register/plugin_client.go +++ b/pkg/register/plugin_client.go @@ -10,6 +10,29 @@ type PluginClient struct { client *rpc.Client } +type DoRequest struct { + CommandName string `json:"commandName"` + Args map[string]string `json:"args"` +} + +// Do implements Plugin +func (pc *PluginClient) Do(ctx context.Context, commandName string, args map[string]string) error { + req := &DoRequest{ + CommandName: commandName, + Args: args, + } + doReq, err := json.Marshal(req) + if err != nil { + return err + } + err = pc.client.Call("Plugin.Do", doReq, new(any)) + if err != nil { + return err + } + + return nil +} + var _ Plugin = &PluginClient{} func (pc *PluginClient) About(ctx context.Context) (*About, error) { diff --git a/pkg/register/plugin_register.go b/pkg/register/plugin_register.go index 38b60f1..14d9fcf 100644 --- a/pkg/register/plugin_register.go +++ b/pkg/register/plugin_register.go @@ -185,11 +185,12 @@ func FromAboutCommands(commands []*AboutCommand) CommandAboutItems { } type AboutItem struct { - Name string - Version string - About string - Vars []string - Commands CommandAboutItems + Name string + Version string + About string + Vars []string + Commands CommandAboutItems + ClientName string } func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) { @@ -197,8 +198,8 @@ func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) { errgroup, ctx := errgroup.WithContext(ctx) - for _, c := range pr.clients { - c := c + for name, c := range pr.clients { + name, c := name, c errgroup.Go(func() error { about, err := c.plugin.About(ctx) if err != nil { @@ -206,11 +207,12 @@ func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) { } list = append(list, AboutItem{ - Name: about.Name, - Version: about.Version, - About: about.About, - Vars: about.Vars, - Commands: FromAboutCommands(about.Commands), + Name: about.Name, + Version: about.Version, + About: about.About, + Vars: about.Vars, + Commands: FromAboutCommands(about.Commands), + ClientName: name, }) return nil }) @@ -222,3 +224,18 @@ func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) { return list, nil } + +func (pr *PluginRegister) Do(ctx context.Context, clientName string, commandName string, args map[string]string) error { + errgroup, ctx := errgroup.WithContext(ctx) + + client, ok := pr.clients[clientName] + if !ok { + return fmt.Errorf("plugin was not found: %s", clientName) + } + + errgroup.Go(func() error { + return client.plugin.Do(ctx, commandName, args) + }) + + return nil +} diff --git a/pkg/register/plugin_server.go b/pkg/register/plugin_server.go index 0de527c..98749c5 100644 --- a/pkg/register/plugin_server.go +++ b/pkg/register/plugin_server.go @@ -9,6 +9,19 @@ type PluginServer struct { Impl Plugin } +func (ps *PluginServer) Do(args *string, resp *string) error { + var doReq DoRequest + if err := json.Unmarshal([]byte(*args), &doReq); err != nil { + return err + } + + if err := ps.Impl.Do(context.Background(), doReq.CommandName, doReq.Args); err != nil { + return err + } + + return nil +} + func (ps *PluginServer) About(args any, resp *string) error { r, err := ps.Impl.About(context.Background()) if err != nil { diff --git a/plugins/gocli/main.go b/plugins/gocli/main.go index 7da499e..57b60d0 100644 --- a/plugins/gocli/main.go +++ b/plugins/gocli/main.go @@ -9,6 +9,13 @@ import ( type GoCliPlugin struct{} +// Do implements register.Plugin +func (*GoCliPlugin) Do(ctx context.Context, commandName string, args map[string]string) error { + log.Print("hit do") + + return nil +} + func (*GoCliPlugin) About(ctx context.Context) (*register.About, error) { return ®ister.About{ Name: "gocli",