2020-08-21 02:39:52 +02:00
|
|
|
package exe
|
|
|
|
|
|
|
|
import (
|
2023-01-03 22:43:56 +01:00
|
|
|
"errors"
|
2020-08-21 02:39:52 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2023-02-21 03:49:33 +01:00
|
|
|
"syscall"
|
2022-11-20 01:51:55 +01:00
|
|
|
|
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
2020-08-21 02:39:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Runner interface {
|
2021-08-12 18:56:23 +02:00
|
|
|
Capture(cmd *exec.Cmd) (stdout string, stderr string, err error)
|
2020-08-21 02:39:52 +02:00
|
|
|
Show(cmd *exec.Cmd) error
|
|
|
|
}
|
|
|
|
|
2023-03-06 10:19:21 +01:00
|
|
|
type OSRunner struct {
|
|
|
|
Log *text.Logger
|
|
|
|
}
|
2020-08-21 02:39:52 +02:00
|
|
|
|
|
|
|
func (r *OSRunner) Show(cmd *exec.Cmd) error {
|
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
2023-02-21 03:49:33 +01:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Pdeathsig: syscall.SIGTERM,
|
|
|
|
}
|
2023-03-06 10:19:21 +01:00
|
|
|
r.Log.Debugln("running", cmd.String())
|
2020-12-23 18:45:24 +01:00
|
|
|
return cmd.Run()
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
func (r *OSRunner) Capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
|
2023-03-06 10:19:21 +01:00
|
|
|
r.Log.Debugln("capturing", cmd.String())
|
2023-02-21 03:49:33 +01:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Pdeathsig: syscall.SIGTERM,
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
outbuf, err := cmd.Output()
|
|
|
|
stdout = strings.TrimSpace(string(outbuf))
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-10-27 13:59:00 +01:00
|
|
|
if err != nil {
|
2023-01-03 22:43:56 +01:00
|
|
|
exitErr := &exec.ExitError{}
|
|
|
|
if errors.As(err, &exitErr) {
|
2021-08-12 18:56:23 +02:00
|
|
|
stderr = strings.TrimSpace(string(exitErr.Stderr))
|
|
|
|
}
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, stderr, err
|
|
|
|
}
|