github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/shellwords/util_posix.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package shellwords 5 6 import ( 7 "errors" 8 "os" 9 "os/exec" 10 "strings" 11 ) 12 13 func shellRun(line, dir string) (string, error) { 14 var shell string 15 if shell = os.Getenv("SHELL"); shell == "" { 16 shell = "/bin/sh" 17 } 18 19 cmd := exec.Command(shell, "-c", line) 20 21 if dir != "" { 22 cmd.Dir = dir 23 } 24 25 b, err := cmd.Output() 26 if err != nil { 27 if eerr, ok := err.(*exec.ExitError); ok { 28 b = eerr.Stderr 29 } 30 31 return "", errors.New(err.Error() + ":" + string(b)) 32 } 33 34 return strings.TrimSpace(string(b)), nil 35 }