9
9
"os"
10
10
"os/signal"
11
11
"syscall"
12
+ "time"
12
13
13
14
"cdr.dev/wsep"
14
15
"github.com/spf13/pflag"
@@ -20,10 +21,11 @@ import (
20
21
)
21
22
22
23
type notty struct {
24
+ timeout time.Duration
23
25
}
24
26
25
27
func (c * notty ) Run (fl * pflag.FlagSet ) {
26
- do (fl , false , "" )
28
+ do (fl , false , "" , c . timeout )
27
29
}
28
30
29
31
func (c * notty ) Spec () cli.CommandSpec {
@@ -34,12 +36,17 @@ func (c *notty) Spec() cli.CommandSpec {
34
36
}
35
37
}
36
38
39
+ func (c * notty ) RegisterFlags (fl * pflag.FlagSet ) {
40
+ fl .DurationVar (& c .timeout , "timeout" , 0 , "disconnect after specified timeout" )
41
+ }
42
+
37
43
type tty struct {
38
- id string
44
+ id string
45
+ timeout time.Duration
39
46
}
40
47
41
48
func (c * tty ) Run (fl * pflag.FlagSet ) {
42
- do (fl , true , c .id )
49
+ do (fl , true , c .id , c . timeout )
43
50
}
44
51
45
52
func (c * tty ) Spec () cli.CommandSpec {
@@ -52,9 +59,10 @@ func (c *tty) Spec() cli.CommandSpec {
52
59
53
60
func (c * tty ) RegisterFlags (fl * pflag.FlagSet ) {
54
61
fl .StringVar (& c .id , "id" , "" , "sets id for reconnection" )
62
+ fl .DurationVar (& c .timeout , "timeout" , 0 , "disconnect after the specified timeout" )
55
63
}
56
64
57
- func do (fl * pflag.FlagSet , tty bool , id string ) {
65
+ func do (fl * pflag.FlagSet , tty bool , id string , timeout time. Duration ) {
58
66
ctx , cancel := context .WithCancel (context .Background ())
59
67
defer cancel ()
60
68
@@ -73,12 +81,18 @@ func do(fl *pflag.FlagSet, tty bool, id string) {
73
81
if len (fl .Args ()) > 1 {
74
82
args = fl .Args ()[1 :]
75
83
}
84
+ width , height , err := term .GetSize (int (os .Stdin .Fd ()))
85
+ if err != nil {
86
+ flog .Fatal ("unable to get term size" )
87
+ }
76
88
process , err := executor .Start (ctx , wsep.Command {
77
89
ID : id ,
78
90
Command : fl .Arg (0 ),
79
91
Args : args ,
80
92
TTY : tty ,
81
93
Stdin : true ,
94
+ Rows : uint16 (height ),
95
+ Cols : uint16 (width ),
82
96
})
83
97
if err != nil {
84
98
flog .Fatal ("failed to start remote command: %v" , err )
@@ -112,6 +126,15 @@ func do(fl *pflag.FlagSet, tty bool, id string) {
112
126
io .Copy (stdin , os .Stdin )
113
127
}()
114
128
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
+
115
138
err = process .Wait ()
116
139
if err != nil {
117
140
flog .Error ("process failed: %v" , err )
0 commit comments