forked from tg123/sshpiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
57 lines (47 loc) · 1.38 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ioconn
import (
"io"
"net"
"os/exec"
)
type cmdconn struct {
conn
cmd *exec.Cmd
}
// Close closes the cmdconn and releases any associated resources.
// It first closes the underlying connection and then kills the process if it is running.
// If an error occurs during the closing of the connection, that error is returned.
// If the process is running and cannot be killed, an error is returned.
func (c *cmdconn) Close() error {
err := c.conn.Close()
if c.cmd.Process != nil {
return c.cmd.Process.Kill()
}
return err
}
// DialCmd is a function that establishes a connection to a command's standard input, output, and error streams.
// It takes a *exec.Cmd as input and returns a net.Conn, io.ReadCloser, and error.
// The net.Conn represents the connection to the command's standard input and output streams.
// The io.ReadCloser represents the command's standard error stream.
// The error represents any error that occurred during the connection establishment.
func DialCmd(cmd *exec.Cmd) (net.Conn, io.ReadCloser, error) {
in, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
out, err := cmd.StdinPipe()
if err != nil {
return nil, nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, err
}
return &cmdconn{
conn: *dial(in, out),
cmd: cmd,
}, stderr, nil
}