2020-08-21 02:39:52 +02:00
|
|
|
package exe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
type OSRunner struct{}
|
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
|
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) {
|
|
|
|
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 {
|
2021-08-12 18:56:23 +02:00
|
|
|
if exitErr, isExitError := err.(*exec.ExitError); isExitError {
|
|
|
|
stderr = strings.TrimSpace(string(exitErr.Stderr))
|
|
|
|
}
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, stderr, err
|
|
|
|
}
|