| 1 | package stdio |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "strconv" |
| 8 | ) |
| 9 | |
| 10 | func Dial(endpoint string, arg ...string) (net.Conn, error) { |
| 11 | cmd := exec.Command(endpoint, arg...) |
| 12 | cmd.Stderr = os.Stderr |
| 13 | |
| 14 | stdin, err := cmd.StdinPipe() |
| 15 | if err != nil { |
| 16 | return nil, err |
| 17 | } |
| 18 | |
| 19 | stdout, err := cmd.StdoutPipe() |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | |
| 24 | err = cmd.Start() |
| 25 | if err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | |
| 29 | local := IoAddr{path: strconv.Itoa(os.Getpid())} |
| 30 | remote := IoAddr{path: strconv.Itoa(cmd.Process.Pid)} |
| 31 | conn := IoConn{ |
| 32 | reader: stdout, |
| 33 | writer: stdin, |
| 34 | local: local, |
| 35 | remote: remote, |
| 36 | close: cmd.Process.Kill, |
| 37 | } |
| 38 | return conn, nil |
| 39 | } |
| 40 | |
| 41 | func GetStdioConn() net.Conn { |
| 42 | local := IoAddr{path: strconv.Itoa(os.Getpid())} |
| 43 | remote := IoAddr{path: "remote"} |
| 44 | conn := IoConn{ |
| 45 | writer: os.Stdout, |
| 46 | reader: os.Stdin, |
| 47 | local: local, |
| 48 | remote: remote, |
| 49 | } |
| 50 | return conn |
| 51 | } |