Skip to content

Commit 698c66a

Browse files
committed
Add timeout flag to dev client
This makes it easier to test reconnects manually.
1 parent f369dfb commit 698c66a

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

dev/client/main.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/signal"
1111
"syscall"
12+
"time"
1213

1314
"cdr.dev/wsep"
1415
"github.com/spf13/pflag"
@@ -20,10 +21,11 @@ import (
2021
)
2122

2223
type notty struct {
24+
timeout time.Duration
2325
}
2426

2527
func (c *notty) Run(fl *pflag.FlagSet) {
26-
do(fl, false, "")
28+
do(fl, false, "", c.timeout)
2729
}
2830

2931
func (c *notty) Spec() cli.CommandSpec {
@@ -34,12 +36,17 @@ func (c *notty) Spec() cli.CommandSpec {
3436
}
3537
}
3638

39+
func (c *notty) RegisterFlags(fl *pflag.FlagSet) {
40+
fl.DurationVar(&c.timeout, "timeout", 0, "disconnect after specified timeout")
41+
}
42+
3743
type tty struct {
38-
id string
44+
id string
45+
timeout time.Duration
3946
}
4047

4148
func (c *tty) Run(fl *pflag.FlagSet) {
42-
do(fl, true, c.id)
49+
do(fl, true, c.id, c.timeout)
4350
}
4451

4552
func (c *tty) Spec() cli.CommandSpec {
@@ -52,9 +59,10 @@ func (c *tty) Spec() cli.CommandSpec {
5259

5360
func (c *tty) RegisterFlags(fl *pflag.FlagSet) {
5461
fl.StringVar(&c.id, "id", "", "sets id for reconnection")
62+
fl.DurationVar(&c.timeout, "timeout", 0, "disconnect after the specified timeout")
5563
}
5664

57-
func do(fl *pflag.FlagSet, tty bool, id string) {
65+
func do(fl *pflag.FlagSet, tty bool, id string, timeout time.Duration) {
5866
ctx, cancel := context.WithCancel(context.Background())
5967
defer cancel()
6068

@@ -73,12 +81,18 @@ func do(fl *pflag.FlagSet, tty bool, id string) {
7381
if len(fl.Args()) > 1 {
7482
args = fl.Args()[1:]
7583
}
84+
width, height, err := term.GetSize(int(os.Stdin.Fd()))
85+
if err != nil {
86+
flog.Fatal("unable to get term size")
87+
}
7688
process, err := executor.Start(ctx, wsep.Command{
7789
ID: id,
7890
Command: fl.Arg(0),
7991
Args: args,
8092
TTY: tty,
8193
Stdin: true,
94+
Rows: uint16(height),
95+
Cols: uint16(width),
8296
})
8397
if err != nil {
8498
flog.Fatal("failed to start remote command: %v", err)
@@ -112,6 +126,15 @@ func do(fl *pflag.FlagSet, tty bool, id string) {
112126
io.Copy(stdin, os.Stdin)
113127
}()
114128

129+
if timeout != 0 {
130+
timer := time.NewTimer(timeout)
131+
defer timer.Stop()
132+
go func() {
133+
<-timer.C
134+
conn.Close(websocket.StatusNormalClosure, "normal closure")
135+
}()
136+
}
137+
115138
err = process.Wait()
116139
if err != nil {
117140
flog.Error("process failed: %v", err)

0 commit comments

Comments
 (0)