feat: add alternative implementation for calling plugin triggers

Refs #6422
This commit is contained in:
Jose Diaz-Gonzalez
2024-01-18 21:46:29 -05:00
parent e093b04511
commit 1a5e17bd68
37 changed files with 343 additions and 0 deletions

73
plugins/common/plugn.go Normal file
View File

@@ -0,0 +1,73 @@
package common
import (
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"context"
execute "github.com/alexellis/go-execute/v2"
"github.com/fatih/color"
)
type PlugnTriggerInput struct {
Args []string
CaptureOutput bool
StreamStdio bool
Trigger string
}
func CallPlugnTrigger(input PlugnTriggerInput) (execute.ExecResult, error) {
LogDebug(fmt.Sprintf("plugn trigger %s %v", input.Trigger, input.Args))
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-signals
cancel()
}()
// hack: colors do not work natively with io.MultiWriter
// as it isn't detected as a tty. If the output isn't
// being captured, then color output can be forced.
isatty := !color.NoColor
env := os.Environ()
if isatty && !input.CaptureOutput {
env = append(env, "FORCE_TTY=1")
}
args := []string{"trigger", input.Trigger}
args = append(args, input.Args...)
cmd := execute.ExecTask{
Command: "plugn",
Args: args,
Env: env,
DisableStdioBuffer: !input.CaptureOutput,
}
if isatty {
cmd.Stdin = os.Stdin
}
if input.StreamStdio {
cmd.StreamStdio = true
}
res, err := cmd.Execute(ctx)
if err != nil {
return res, err
}
if res.ExitCode != 0 {
return res, errors.New(res.Stderr)
}
return res, nil
}