@@ -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,7 +44,7 @@ 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
@@ -59,15 +58,15 @@ func sendResizeEvents(termfd int, client *wush.Client) {
59
58
return
60
59
}
61
60
62
- err = client .Resize (width , height )
61
+ err = process .Resize (ctx , uint16 ( height ), uint16 ( width ) )
63
62
if err != nil {
64
- flog .Error ("get term size: %v" , err )
63
+ flog .Error ("set term size: %v" , err )
65
64
return
66
65
}
67
66
68
67
// Do this last so the first resize is sent.
69
68
<- sigs
70
- resizeLimiter .Wait (context . Background () )
69
+ resizeLimiter .Wait (ctx )
71
70
}
72
71
}
73
72
@@ -78,6 +77,7 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
78
77
var (
79
78
envName = fl .Arg (0 )
80
79
command = fl .Arg (1 )
80
+ ctx = context .Background ()
81
81
)
82
82
83
83
var args []string
@@ -91,14 +91,16 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
91
91
args = []string {"-c" , "exec $(getent passwd $(whoami) | awk -F: '{ print $7 }')" }
92
92
}
93
93
94
- exitCode , err := runCommand (envName , command , args )
94
+ err := runCommand (ctx , envName , command , args )
95
+ if exitErr , ok := err .(wsep.ExitError ); ok {
96
+ os .Exit (exitErr .Code )
97
+ }
95
98
if err != nil {
96
99
flog .Fatal ("run command: %v Is it online?" , err )
97
100
}
98
- os .Exit (exitCode )
99
101
}
100
102
101
- func runCommand (envName string , command string , args []string ) ( int , error ) {
103
+ func runCommand (ctx context. Context , envName string , command string , args []string ) error {
102
104
var (
103
105
entClient = requireAuth ()
104
106
env = findEnv (entClient , envName )
@@ -110,38 +112,42 @@ func runCommand(envName string, command string, args []string) (int, error) {
110
112
if tty {
111
113
restore , err := enableTerminal (termfd )
112
114
if err != nil {
113
- return - 1 , err
115
+ return err
114
116
}
115
117
defer restore ()
116
118
}
117
119
118
- conn , err := entClient .DialWush (
119
- env ,
120
- & client.WushOptions {
121
- TTY : tty ,
122
- Stdin : true ,
123
- }, command , args ... )
120
+ conn , err := entClient .DialWsep (ctx , env )
124
121
if err != nil {
125
- return - 1 , err
122
+ return err
123
+ }
124
+
125
+ execer := wsep .RemoteExecer (conn )
126
+ process , err := execer .Start (ctx , wsep.Command {
127
+ Command : command ,
128
+ Args : args ,
129
+ TTY : true ,
130
+ })
131
+ if err != nil {
132
+ return err
126
133
}
127
- ctx := context .Background ()
128
134
129
- wc := wush .NewClient (ctx , conn )
130
135
if tty {
131
- go sendResizeEvents (termfd , wc )
136
+ go sendResizeEvents (ctx , termfd , process )
132
137
}
133
138
134
139
go func () {
135
- defer wc .Stdin .Close ()
136
- io .Copy (wc .Stdin , os .Stdin )
140
+ stdin := process .Stdin ()
141
+ defer stdin .Close ()
142
+ io .Copy (stdin , os .Stdin )
137
143
}()
138
- go io .Copy (os .Stdout , wc .Stdout )
139
- go io .Copy (os .Stderr , wc .Stderr )
140
144
141
- exitCode , err := wc .Wait ()
145
+ go io .Copy (os .Stdout , process .Stdout ())
146
+ go io .Copy (os .Stderr , process .Stderr ())
147
+
148
+ err = process .Wait ()
142
149
if err != nil {
143
- return - 1 , err
150
+ return err
144
151
}
145
-
146
- return int (exitCode ), nil
152
+ return nil
147
153
}
0 commit comments