yay/pkg/settings/exe/exec.go

37 lines
779 B
Go
Raw Normal View History

package exe
import (
"os"
"os/exec"
"strings"
"github.com/Jguer/yay/v11/pkg/text"
)
type Runner interface {
2021-08-12 18:56:23 +02:00
Capture(cmd *exec.Cmd) (stdout string, stderr string, err error)
Show(cmd *exec.Cmd) error
}
2021-08-11 20:13:28 +02:00
type OSRunner struct{}
func (r *OSRunner) Show(cmd *exec.Cmd) error {
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
text.Debugln("running", cmd.String())
return cmd.Run()
}
2021-08-12 18:56:23 +02:00
func (r *OSRunner) Capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
text.Debugln("capturing", cmd.String())
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
if err != nil {
2021-08-12 18:56:23 +02:00
if exitErr, isExitError := err.(*exec.ExitError); isExitError {
stderr = strings.TrimSpace(string(exitErr.Stderr))
}
}
return stdout, stderr, err
}