go.mod: github.com/mattn/go-shellwords v1.0.11

adds go module support, among others

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2021-03-12 15:59:19 +01:00
parent 59a6259f8c
commit d2d89ddfad
10 changed files with 274 additions and 49 deletions

View File

@ -3,17 +3,27 @@
package shellwords
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
func shellRun(line string) (string, error) {
shell := os.Getenv("SHELL")
b, err := exec.Command(shell, "-c", line).Output()
func shellRun(line, dir string) (string, error) {
var shell string
if shell = os.Getenv("SHELL"); shell == "" {
shell = "/bin/sh"
}
cmd := exec.Command(shell, "-c", line)
if dir != "" {
cmd.Dir = dir
}
b, err := cmd.Output()
if err != nil {
return "", errors.New(err.Error() + ":" + string(b))
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", fmt.Errorf("%s: %w", string(b), err)
}
return strings.TrimSpace(string(b)), nil
}