2022-03-09 05:08:35 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExecCmd(cmdStr string) (string, error) {
|
|
|
|
args := splitArgs(cmdStr)
|
|
|
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
if len(args) == 1 {
|
|
|
|
cmd = exec.Command(args[0])
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command(args[0], args[1:]...)
|
|
|
|
|
2022-03-28 19:48:32 +08:00
|
|
|
}
|
|
|
|
prepareBackgroundCommand(cmd)
|
2022-03-09 05:08:35 +08:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%v, %s", err, string(out))
|
|
|
|
}
|
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitArgs(cmd string) []string {
|
|
|
|
args := strings.Split(cmd, " ")
|
|
|
|
|
|
|
|
// use in pipeline
|
|
|
|
if len(args) > 2 && strings.ContainsAny(cmd, "|") {
|
|
|
|
suffix := strings.Join(args[2:], " ")
|
|
|
|
args = append(args[:2], suffix)
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|