@@ -16,8 +16,7 @@ import (
16
16
"go.coder.com/cli"
17
17
"go.coder.com/flog"
18
18
19
- client "cdr.dev/coder-cli/internal/entclient"
20
- "cdr.dev/coder-cli/wush"
19
+ "cdr.dev/wsep"
21
20
)
22
21
23
22
type shellCmd struct {
@@ -45,29 +44,32 @@ func enableTerminal(fd int) (restore func(), err error) {
45
44
}, nil
46
45
}
47
46
48
- func sendResizeEvents (termfd int , client * wush. Client ) {
47
+ func sendResizeEvents (ctx context. Context , termfd int , process wsep. Process ) {
49
48
sigs := make (chan os.Signal , 16 )
50
49
signal .Notify (sigs , unix .SIGWINCH )
51
50
52
51
// Limit the frequency of resizes to prevent a stuttering effect.
53
52
resizeLimiter := rate .NewLimiter (rate .Every (time .Millisecond * 100 ), 1 )
54
53
55
54
for {
55
+ if ctx .Err () != nil {
56
+ return
57
+ }
56
58
width , height , err := terminal .GetSize (termfd )
57
59
if err != nil {
58
60
flog .Error ("get term size: %v" , err )
59
61
return
60
62
}
61
63
62
- err = client .Resize (width , height )
64
+ err = process .Resize (ctx , uint16 ( height ), uint16 ( width ) )
63
65
if err != nil {
64
- flog .Error ("get term size: %v" , err )
66
+ flog .Error ("set term size: %v" , err )
65
67
return
66
68
}
67
69
68
70
// Do this last so the first resize is sent.
69
71
<- sigs
70
- resizeLimiter .Wait (context . Background () )
72
+ resizeLimiter .Wait (ctx )
71
73
}
72
74
}
73
75
@@ -78,6 +80,7 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
78
80
var (
79
81
envName = fl .Arg (0 )
80
82
command = fl .Arg (1 )
83
+ ctx = context .Background ()
81
84
)
82
85
83
86
var args []string
@@ -91,14 +94,16 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
91
94
args = []string {"-c" , "exec $(getent passwd $(whoami) | awk -F: '{ print $7 }')" }
92
95
}
93
96
94
- exitCode , err := runCommand (envName , command , args )
97
+ err := runCommand (ctx , envName , command , args )
98
+ if exitErr , ok := err .(wsep.ExitError ); ok {
99
+ os .Exit (exitErr .Code )
100
+ }
95
101
if err != nil {
96
102
flog .Fatal ("run command: %v Is it online?" , err )
97
103
}
98
- os .Exit (exitCode )
99
104
}
100
105
101
- func runCommand (envName string , command string , args []string ) ( int , error ) {
106
+ func runCommand (ctx context. Context , envName string , command string , args []string ) error {
102
107
var (
103
108
entClient = requireAuth ()
104
109
env = findEnv (entClient , envName )
@@ -110,38 +115,37 @@ func runCommand(envName string, command string, args []string) (int, error) {
110
115
if tty {
111
116
restore , err := enableTerminal (termfd )
112
117
if err != nil {
113
- return - 1 , err
118
+ return err
114
119
}
115
120
defer restore ()
116
121
}
117
122
118
- conn , err := entClient .DialWush (
119
- env ,
120
- & client.WushOptions {
121
- TTY : tty ,
122
- Stdin : true ,
123
- }, command , args ... )
123
+ conn , err := entClient .DialWsep (ctx , env )
124
+ if err != nil {
125
+ return err
126
+ }
127
+
128
+ execer := wsep .RemoteExecer (conn )
129
+ process , err := execer .Start (ctx , wsep.Command {
130
+ Command : command ,
131
+ Args : args ,
132
+ TTY : tty ,
133
+ })
124
134
if err != nil {
125
- return - 1 , err
135
+ return err
126
136
}
127
- ctx := context .Background ()
128
137
129
- wc := wush .NewClient (ctx , conn )
130
138
if tty {
131
- go sendResizeEvents (termfd , wc )
139
+ go sendResizeEvents (ctx , termfd , process )
132
140
}
133
141
134
142
go func () {
135
- defer wc .Stdin .Close ()
136
- io .Copy (wc .Stdin , os .Stdin )
143
+ stdin := process .Stdin ()
144
+ defer stdin .Close ()
145
+ io .Copy (stdin , os .Stdin )
137
146
}()
138
- go io .Copy (os .Stdout , wc .Stdout )
139
- go io .Copy (os .Stderr , wc .Stderr )
140
-
141
- exitCode , err := wc .Wait ()
142
- if err != nil {
143
- return - 1 , err
144
- }
147
+ go io .Copy (os .Stdout , process .Stdout ())
148
+ go io .Copy (os .Stderr , process .Stderr ())
145
149
146
- return int ( exitCode ), nil
150
+ return process . Wait ()
147
151
}
0 commit comments