Skip to content

Commit f0e7f9c

Browse files
committed
Use wsep for coder sh
1 parent ba7ea67 commit f0e7f9c

File tree

4 files changed

+326
-40
lines changed

4 files changed

+326
-40
lines changed

cmd/coder/shell.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import (
1616
"go.coder.com/cli"
1717
"go.coder.com/flog"
1818

19-
client "cdr.dev/coder-cli/internal/entclient"
20-
"cdr.dev/coder-cli/wush"
19+
"cdr.dev/wsep"
2120
)
2221

2322
type shellCmd struct {
@@ -45,29 +44,32 @@ func enableTerminal(fd int) (restore func(), err error) {
4544
}, nil
4645
}
4746

48-
func sendResizeEvents(termfd int, client *wush.Client) {
47+
func sendResizeEvents(ctx context.Context, termfd int, process wsep.Process) {
4948
sigs := make(chan os.Signal, 16)
5049
signal.Notify(sigs, unix.SIGWINCH)
5150

5251
// Limit the frequency of resizes to prevent a stuttering effect.
5352
resizeLimiter := rate.NewLimiter(rate.Every(time.Millisecond*100), 1)
5453

5554
for {
55+
if ctx.Err() != nil {
56+
return
57+
}
5658
width, height, err := terminal.GetSize(termfd)
5759
if err != nil {
5860
flog.Error("get term size: %v", err)
5961
return
6062
}
6163

62-
err = client.Resize(width, height)
64+
err = process.Resize(ctx, uint16(height), uint16(width))
6365
if err != nil {
64-
flog.Error("get term size: %v", err)
66+
flog.Error("set term size: %v", err)
6567
return
6668
}
6769

6870
// Do this last so the first resize is sent.
6971
<-sigs
70-
resizeLimiter.Wait(context.Background())
72+
resizeLimiter.Wait(ctx)
7173
}
7274
}
7375

@@ -78,6 +80,7 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
7880
var (
7981
envName = fl.Arg(0)
8082
command = fl.Arg(1)
83+
ctx = context.Background()
8184
)
8285

8386
var args []string
@@ -91,14 +94,16 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
9194
args = []string{"-c", "exec $(getent passwd $(whoami) | awk -F: '{ print $7 }')"}
9295
}
9396

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+
}
95101
if err != nil {
96102
flog.Fatal("run command: %v Is it online?", err)
97103
}
98-
os.Exit(exitCode)
99104
}
100105

101-
func runCommand(envName string, command string, args []string) (int, error) {
106+
func runCommand(ctx context.Context, envName string, command string, args []string) error {
102107
var (
103108
entClient = requireAuth()
104109
env = findEnv(entClient, envName)
@@ -110,38 +115,37 @@ func runCommand(envName string, command string, args []string) (int, error) {
110115
if tty {
111116
restore, err := enableTerminal(termfd)
112117
if err != nil {
113-
return -1, err
118+
return err
114119
}
115120
defer restore()
116121
}
117122

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+
})
124134
if err != nil {
125-
return -1, err
135+
return err
126136
}
127-
ctx := context.Background()
128137

129-
wc := wush.NewClient(ctx, conn)
130138
if tty {
131-
go sendResizeEvents(termfd, wc)
139+
go sendResizeEvents(ctx, termfd, process)
132140
}
133141

134142
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)
137146
}()
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())
145149

146-
return int(exitCode), nil
150+
return process.Wait()
147151
}

go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ module cdr.dev/coder-cli
33
go 1.14
44

55
require (
6+
cdr.dev/wsep v0.0.0-20200602025116-5cbe721683df
7+
github.com/fatih/color v1.9.0 // indirect
68
github.com/gorilla/websocket v1.4.1
79
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
8-
github.com/mattn/go-colorable v0.1.2 // indirect
9-
github.com/mattn/go-isatty v0.0.12 // indirect
10+
github.com/klauspost/compress v1.10.7 // indirect
11+
github.com/mattn/go-colorable v0.1.6 // indirect
1012
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
1113
github.com/rjeczalik/notify v0.9.2
1214
github.com/spf13/pflag v1.0.5
1315
go.coder.com/cli v0.4.0
1416
go.coder.com/flog v0.0.0-20190906214207-47dd47ea0512
1517
golang.org/x/crypto v0.0.0-20200422194213-44a606286825
1618
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
17-
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4
19+
golang.org/x/sys v0.0.0-20200523222454-059865788121
1820
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
1921
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
20-
nhooyr.io/websocket v1.8.5
22+
nhooyr.io/websocket v1.8.6
2123
)

0 commit comments

Comments
 (0)