with handling for do

This commit is contained in:
Kasper Juul Hermansen 2022-11-05 23:05:41 +01:00
parent fd826827f6
commit 52f44f7c2e
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
8 changed files with 77 additions and 15 deletions

View File

@ -44,7 +44,7 @@ func NewDoCommand(charctx *charcontext.CharContext) *cobra.Command {
return err 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 return err
} }

View File

@ -10,4 +10,4 @@ CHAR_DEV_MODE=true ./char do -h
echo echo
echo "--------" echo "--------"
echo "local_up" echo "local_up"
CHAR_DEV_MODE=false ./char do local_up -h CHAR_DEV_MODE=false ./char do local_up --fish something

View File

@ -65,6 +65,7 @@ func (cc *CharContext) About(ctx context.Context) ([]register.AboutItem, error)
return cc.pluginRegister.About(ctx) 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 return nil
} }

View File

@ -18,6 +18,7 @@ type About struct {
type Plugin interface { type Plugin interface {
About(ctx context.Context) (*About, error) About(ctx context.Context) (*About, error)
Do(ctx context.Context, commandName string, args map[string]string) error
} }
const PluginKey = "plugin" const PluginKey = "plugin"

View File

@ -10,6 +10,29 @@ type PluginClient struct {
client *rpc.Client 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{} var _ Plugin = &PluginClient{}
func (pc *PluginClient) About(ctx context.Context) (*About, error) { func (pc *PluginClient) About(ctx context.Context) (*About, error) {

View File

@ -185,11 +185,12 @@ func FromAboutCommands(commands []*AboutCommand) CommandAboutItems {
} }
type AboutItem struct { type AboutItem struct {
Name string Name string
Version string Version string
About string About string
Vars []string Vars []string
Commands CommandAboutItems Commands CommandAboutItems
ClientName string
} }
func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) { 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) errgroup, ctx := errgroup.WithContext(ctx)
for _, c := range pr.clients { for name, c := range pr.clients {
c := c name, c := name, c
errgroup.Go(func() error { errgroup.Go(func() error {
about, err := c.plugin.About(ctx) about, err := c.plugin.About(ctx)
if err != nil { if err != nil {
@ -206,11 +207,12 @@ func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) {
} }
list = append(list, AboutItem{ list = append(list, AboutItem{
Name: about.Name, Name: about.Name,
Version: about.Version, Version: about.Version,
About: about.About, About: about.About,
Vars: about.Vars, Vars: about.Vars,
Commands: FromAboutCommands(about.Commands), Commands: FromAboutCommands(about.Commands),
ClientName: name,
}) })
return nil return nil
}) })
@ -222,3 +224,18 @@ func (pr *PluginRegister) About(ctx context.Context) ([]AboutItem, error) {
return list, nil 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
}

View File

@ -9,6 +9,19 @@ type PluginServer struct {
Impl Plugin 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 { func (ps *PluginServer) About(args any, resp *string) error {
r, err := ps.Impl.About(context.Background()) r, err := ps.Impl.About(context.Background())
if err != nil { if err != nil {

View File

@ -9,6 +9,13 @@ import (
type GoCliPlugin struct{} 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) { func (*GoCliPlugin) About(ctx context.Context) (*register.About, error) {
return &register.About{ return &register.About{
Name: "gocli", Name: "gocli",