Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit c68b7e4

Browse files
committed
Use wsep for coder sh
1 parent fd7e11e commit c68b7e4

File tree

4 files changed

+326
-38
lines changed

4 files changed

+326
-38
lines changed

cmd/coder/shell.go

Lines changed: 34 additions & 28 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,7 +44,7 @@ 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

@@ -59,15 +58,15 @@ func sendResizeEvents(termfd int, client *wush.Client) {
5958
return
6059
}
6160

62-
err = client.Resize(width, height)
61+
err = process.Resize(ctx, uint16(height), uint16(width))
6362
if err != nil {
64-
flog.Error("get term size: %v", err)
63+
flog.Error("set term size: %v", err)
6564
return
6665
}
6766

6867
// Do this last so the first resize is sent.
6968
<-sigs
70-
resizeLimiter.Wait(context.Background())
69+
resizeLimiter.Wait(ctx)
7170
}
7271
}
7372

@@ -78,6 +77,7 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
7877
var (
7978
envName = fl.Arg(0)
8079
command = fl.Arg(1)
80+
ctx = context.Background()
8181
)
8282

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

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+
}
9598
if err != nil {
9699
flog.Fatal("run command: %v Is it online?", err)
97100
}
98-
os.Exit(exitCode)
99101
}
100102

101-
func runCommand(envName string, command string, args []string) (int, error) {
103+
func runCommand(ctx context.Context, envName string, command string, args []string) error {
102104
var (
103105
entClient = requireAuth()
104106
env = findEnv(entClient, envName)
@@ -110,38 +112,42 @@ func runCommand(envName string, command string, args []string) (int, error) {
110112
if tty {
111113
restore, err := enableTerminal(termfd)
112114
if err != nil {
113-
return -1, err
115+
return err
114116
}
115117
defer restore()
116118
}
117119

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)
124121
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
126133
}
127-
ctx := context.Background()
128134

129-
wc := wush.NewClient(ctx, conn)
130135
if tty {
131-
go sendResizeEvents(termfd, wc)
136+
go sendResizeEvents(ctx, termfd, process)
132137
}
133138

134139
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)
137143
}()
138-
go io.Copy(os.Stdout, wc.Stdout)
139-
go io.Copy(os.Stderr, wc.Stderr)
140144

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()
142149
if err != nil {
143-
return -1, err
150+
return err
144151
}
145-
146-
return int(exitCode), nil
152+
return nil
147153
}

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)