/
main.go
171 lines (147 loc) · 3.48 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//go:build !windows
// +build !windows
package main
import (
"context"
"io"
"os"
"os/signal"
"syscall"
"time"
"cdr.dev/wsep"
"github.com/spf13/pflag"
"golang.org/x/term"
"nhooyr.io/websocket"
"go.coder.com/cli"
"go.coder.com/flog"
)
type notty struct {
timeout time.Duration
}
func (c *notty) Run(fl *pflag.FlagSet) {
do(fl, false, "", c.timeout)
}
func (c *notty) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "notty",
Usage: "[flags] <command>",
Desc: `Run a command without tty enabled.`,
}
}
func (c *notty) RegisterFlags(fl *pflag.FlagSet) {
fl.DurationVar(&c.timeout, "timeout", 0, "disconnect after specified timeout")
}
type tty struct {
id string
timeout time.Duration
}
func (c *tty) Run(fl *pflag.FlagSet) {
do(fl, true, c.id, c.timeout)
}
func (c *tty) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "tty",
Usage: "[flags] <command>",
Desc: `Run a command with tty enabled. Use the same ID to reconnect.`,
}
}
func (c *tty) RegisterFlags(fl *pflag.FlagSet) {
fl.StringVar(&c.id, "id", "", "sets id for reconnection")
fl.DurationVar(&c.timeout, "timeout", 0, "disconnect after the specified timeout")
}
func do(fl *pflag.FlagSet, tty bool, id string, timeout time.Duration) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
if err != nil {
flog.Fatal("failed to dial remote executor: %v", err)
}
defer conn.Close(websocket.StatusNormalClosure, "terminate process")
executor := wsep.RemoteExecer(conn)
var args []string
if len(fl.Args()) < 1 {
flog.Fatal("a command argument is required")
}
if len(fl.Args()) > 1 {
args = fl.Args()[1:]
}
width, height, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
flog.Fatal("unable to get term size")
}
process, err := executor.Start(ctx, wsep.Command{
ID: id,
Command: fl.Arg(0),
Args: args,
TTY: tty,
Stdin: true,
Rows: uint16(height),
Cols: uint16(width),
Env: []string{"TERM=" + os.Getenv("TERM")},
})
if err != nil {
flog.Fatal("failed to start remote command: %v", err)
}
if tty {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
width, height, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
continue
}
process.Resize(ctx, uint16(height), uint16(width))
}
}()
ch <- syscall.SIGWINCH
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
flog.Fatal("failed to make terminal raw for tty: %w", err)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
}
go io.Copy(os.Stdout, process.Stdout())
go io.Copy(os.Stderr, process.Stderr())
go func() {
stdin := process.Stdin()
defer stdin.Close()
io.Copy(stdin, os.Stdin)
}()
if timeout != 0 {
timer := time.NewTimer(timeout)
defer timer.Stop()
go func() {
<-timer.C
conn.Close(websocket.StatusNormalClosure, "normal closure")
}()
}
err = process.Wait()
if err != nil {
flog.Error("process failed: %v", err)
}
conn.Close(websocket.StatusNormalClosure, "normal closure")
}
type cmd struct {
}
func (c *cmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "wsep-client",
Usage: "[flags]",
Desc: `Run a simple wsep client for testing.`,
RawArgs: true,
}
}
func (c *cmd) Run(fl *pflag.FlagSet) {
fl.Usage()
os.Exit(1)
}
func (c *cmd) Subcommands() []cli.Command {
return []cli.Command{
¬ty{},
&tty{},
}
}
func main() {
cli.RunRoot(&cmd{})
}