mukan-ignite/ignite/internal/plugin/execute.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

62 lines
1.4 KiB
Go

package plugininternal
import (
"bytes"
"context"
"sync"
"google.golang.org/grpc/status"
pluginsconfig "git.cw.tr/mukan-network/mukan-ignite/ignite/config/plugins"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/errors"
"git.cw.tr/mukan-network/mukan-ignite/ignite/services/plugin"
)
type synchronizedBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}
func (b *synchronizedBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}
func (b *synchronizedBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}
// Execute starts and executes a plugin, then shutdowns it.
func Execute(ctx context.Context, path string, args []string, options ...plugin.APIOption) (string, error) {
var buf synchronizedBuffer
plugins, err := plugin.Load(
ctx,
[]pluginsconfig.Plugin{{Path: path}},
plugin.RedirectStdout(&buf),
)
if err != nil {
return "", err
}
defer plugins[0].KillClient()
if plugins[0].Error != nil {
return "", plugins[0].Error
}
err = plugins[0].Interface.Execute(
ctx,
&plugin.ExecutedCommand{Args: args},
plugin.NewClientAPI(options...),
)
if err != nil {
// Extract the rpc status message and create a simple error from it.
// We don't want Execute to return rpc errors.
return "", errors.New(status.Convert(err).Message())
}
plugins[0].KillClient()
return buf.String(), err
}