@@ -8,12 +8,13 @@ import (
8
8
"time"
9
9
10
10
"github.com/spf13/pflag"
11
- "go.coder.com/cli"
12
- "go.coder.com/flog"
13
11
"golang.org/x/crypto/ssh/terminal"
14
12
"golang.org/x/sys/unix"
15
13
"golang.org/x/time/rate"
16
14
15
+ "go.coder.com/cli"
16
+ "go.coder.com/flog"
17
+
17
18
client "cdr.dev/coder-cli/internal/entclient"
18
19
"cdr.dev/coder-cli/wush"
19
20
)
@@ -30,14 +31,20 @@ func (cmd *shellCmd) Spec() cli.CommandSpec {
30
31
}
31
32
}
32
33
33
- func enableTerminal (fd int ) {
34
- _ , err := terminal .MakeRaw (fd )
34
+ func enableTerminal (fd int ) ( restore func ()) {
35
+ state , err := terminal .MakeRaw (fd )
35
36
if err != nil {
36
37
flog .Fatal ("make raw term: %v" , err )
37
38
}
39
+ return func () {
40
+ err := terminal .Restore (fd , state )
41
+ if err != nil {
42
+ flog .Fatal ("restore term state: %v" , err )
43
+ }
44
+ }
38
45
}
39
46
40
- func ( cmd * shellCmd ) sendResizeEvents (termfd int , client * wush.Client ) {
47
+ func sendResizeEvents (termfd int , client * wush.Client ) {
41
48
sigs := make (chan os.Signal , 16 )
42
49
signal .Notify (sigs , unix .SIGWINCH )
43
50
@@ -83,6 +90,11 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
83
90
args = []string {"-c" , "exec $(getent passwd $(whoami) | awk -F: '{ print $7 }')" }
84
91
}
85
92
93
+ exitCode := runCommand (envName , command , args )
94
+ os .Exit (exitCode )
95
+ }
96
+
97
+ func runCommand (envName string , command string , args []string ) int {
86
98
var (
87
99
entClient = requireAuth ()
88
100
env = findEnv (entClient , envName )
@@ -92,7 +104,8 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
92
104
93
105
tty := terminal .IsTerminal (termfd )
94
106
if tty {
95
- enableTerminal (termfd )
107
+ restore := enableTerminal (termfd )
108
+ defer restore ()
96
109
}
97
110
98
111
conn , err := entClient .DialWush (
@@ -108,10 +121,10 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
108
121
109
122
wc := wush .NewClient (ctx , conn )
110
123
if tty {
111
- go cmd . sendResizeEvents (termfd , wc )
124
+ go sendResizeEvents (termfd , wc )
112
125
}
113
126
114
- go func (){
127
+ go func () {
115
128
defer wc .Stdin .Close ()
116
129
io .Copy (wc .Stdin , os .Stdin )
117
130
}()
@@ -122,5 +135,6 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
122
135
if err != nil {
123
136
flog .Fatal ("wush error: %v" , err )
124
137
}
125
- os .Exit (int (exitCode ))
138
+
139
+ return int (exitCode )
126
140
}
0 commit comments