with handling for do
This commit is contained in:
parent
fd826827f6
commit
52f44f7c2e
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user