mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
db16115037
* add execute files * add protoc-osx * add rpc generation * add rpc generation * add: rpc template generation * update usage * fixed env prepare for project in go path * optimize gomod cache * add README.md * format error * reactor templatex.go * remove waste code
35 lines
604 B
Go
35 lines
604 B
Go
package execx
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"os/exec"
|
|
"runtime"
|
|
)
|
|
|
|
func Run(arg string) (string, error) {
|
|
goos := runtime.GOOS
|
|
var cmd *exec.Cmd
|
|
switch goos {
|
|
case "darwin":
|
|
cmd = exec.Command("sh", "-c", arg)
|
|
case "windows":
|
|
cmd = exec.Command("cmd.exe", "/c", arg)
|
|
default:
|
|
return "", fmt.Errorf("unexpected os: %v", goos)
|
|
}
|
|
dtsout := new(bytes.Buffer)
|
|
stderr := new(bytes.Buffer)
|
|
cmd.Stdout = dtsout
|
|
cmd.Stderr = stderr
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
if stderr.Len() > 0 {
|
|
return "", errors.New(stderr.String())
|
|
}
|
|
return "", err
|
|
}
|
|
return dtsout.String(), nil
|
|
}
|