forked from coder/wsep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
70 lines (62 loc) · 1.77 KB
/
exec.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
58
59
60
61
62
63
64
65
66
67
68
69
70
package wsep
import (
"context"
"fmt"
"io"
"cdr.dev/wsep/internal/proto"
)
// ExitError is sent when the command terminates.
type ExitError struct {
Code int
}
func (e ExitError) Error() string {
return fmt.Sprintf("process exited with code %v", e.Code)
}
// Process represents a started command.
type Process interface {
// Pid is populated immediately during a successful start with the process ID.
Pid() int
// Stdout returns an io.WriteCloser that will pipe writes to the remote command.
// Closure of stdin sends the corresponding close message.
Stdin() io.WriteCloser
// Stdout returns an io.Reader that is connected to the command's standard output.
Stdout() io.Reader
// Stderr returns an io.Reader that is connected to the command's standard error.
Stderr() io.Reader
// Resize resizes the TTY if a TTY is enabled.
Resize(ctx context.Context, rows, cols uint16) error
// Wait returns ExitError when the command terminates with a non-zero exit code.
Wait() error
// Close terminates the process and underlying connection(s).
// It must be called otherwise a connection or process may leak.
Close() error
}
// Execer starts commands.
type Execer interface {
Start(ctx context.Context, c Command) (Process, error)
}
// theses maps are needed to prevent an import cycle
func mapToProtoCmd(c Command) proto.Command {
return proto.Command{
Command: c.Command,
Args: c.Args,
Stdin: c.Stdin,
TTY: c.TTY,
UID: c.UID,
GID: c.GID,
Env: c.Env,
WorkingDir: c.WorkingDir,
}
}
func mapToClientCmd(c proto.Command) Command {
return Command{
Command: c.Command,
Args: c.Args,
Stdin: c.Stdin,
TTY: c.TTY,
UID: c.UID,
GID: c.GID,
Env: c.Env,
WorkingDir: c.WorkingDir,
}
}