1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 18:28:00 -05:00
v2fly/common/platform/ctlcmd/ctlcmd.go

45 lines
929 B
Go
Raw Normal View History

2018-04-08 17:22:55 -04:00
package ctlcmd
import (
"io"
"os"
"os/exec"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/platform"
)
2018-09-30 17:08:41 -04:00
//go:generate errorgen
2018-04-08 17:22:55 -04:00
func Run(args []string, input io.Reader) (buf.MultiBuffer, error) {
v2ctl := platform.GetToolLocation("v2ctl")
if _, err := os.Stat(v2ctl); err != nil {
2018-04-13 05:25:41 -04:00
return nil, newError("v2ctl doesn't exist").Base(err)
2018-04-08 17:22:55 -04:00
}
2018-11-18 13:36:36 -05:00
var errBuffer buf.MultiBufferContainer
var outBuffer buf.MultiBufferContainer
2018-04-08 17:22:55 -04:00
cmd := exec.Command(v2ctl, args...)
2018-04-13 07:54:36 -04:00
cmd.Stderr = &errBuffer
cmd.Stdout = &outBuffer
2018-04-08 17:22:55 -04:00
cmd.SysProcAttr = getSysProcAttr()
if input != nil {
cmd.Stdin = input
}
if err := cmd.Start(); err != nil {
2018-04-13 05:25:41 -04:00
return nil, newError("failed to start v2ctl").Base(err)
2018-04-08 17:22:55 -04:00
}
2018-04-13 07:54:36 -04:00
if err := cmd.Wait(); err != nil {
msg := "failed to execute v2ctl"
if errBuffer.Len() > 0 {
2018-11-18 13:36:36 -05:00
msg += ": " + errBuffer.MultiBuffer.String()
2018-04-08 17:22:55 -04:00
}
2018-04-13 07:54:36 -04:00
return nil, newError(msg).Base(err)
2018-04-08 17:22:55 -04:00
}
2018-11-18 13:36:36 -05:00
return outBuffer.MultiBuffer, nil
2018-04-08 17:22:55 -04:00
}