util_posix.go 461 B

1234567891011121314151617181920212223242526
  1. // +build !windows,go1.6
  2. package shellwords
  3. import (
  4. "errors"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. )
  9. func shellRun(line, dir string) (string, error) {
  10. shell := os.Getenv("SHELL")
  11. cmd := exec.Command(shell, "-c", line)
  12. if dir != "" {
  13. cmd.Dir = dir
  14. }
  15. b, err := cmd.Output()
  16. if err != nil {
  17. if eerr, ok := err.(*exec.ExitError); ok {
  18. b = eerr.Stderr
  19. }
  20. return "", errors.New(err.Error() + ":" + string(b))
  21. }
  22. return strings.TrimSpace(string(b)), nil
  23. }