Skip to content

Commit 2888401

Browse files
committed
Improve docs
1 parent dedbab5 commit 2888401

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea/

README.md

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,56 @@
44
<strong style="font-size: 1.5em; text-decoration: underline;">w</strong>eb <strong style="font-size: 1.5em;text-decoration: underline;">s</strong>ocket command <strong style="font-size: 1.5em;text-decoration: underline;">e</strong>xecution <strong style="font-size: 1.5em;text-decoration: underline;">p</strong>rotocol. It can be thought of as SSH without encryption.
55

66
It's useful in cases where you want to provide a command exec interface into a remote environment. It's implemented
7-
with WebSocket so it may be used directly by a browser frontend.
7+
with WebSocket so it may be used directly by a browser frontend. Its symmetric design satisfies
8+
`wsep.Execer` for local and remote execution.
89

910
## Examples
1011

12+
Error handling is omitted for brevity.
13+
1114
### Client
1215

1316
```golang
14-
ws, _, err := websocket.Dial(ctx, "ws://remote.exec.addr", nil)
15-
if err != nil {
16-
// handle error
17-
}
17+
conn, _, _ := websocket.Dial(ctx, "ws://remote.exec.addr", nil)
1818
defer conn.Close(websocket.StatusAbnormalClosure, "terminate process")
1919

20-
executor := wsep.RemoteExecer(ws)
21-
process, err := executor.Start(ctx, proto.Command{
20+
execer := wsep.RemoteExecer(conn)
21+
process, _ := execer.Start(ctx, wsep.Command{
2222
Command: "cat",
23-
Args: []string{"go.mod"},
23+
Args: []string{"go.mod"},
24+
Stdin: false,
2425
})
25-
if err != nil {
26-
// handle error
27-
}
2826

29-
io.Copy(os.Stderr, process.Stderr())
27+
go io.Copy(os.Stderr, process.Stderr())
28+
go io.Copy(os.Stdout, process.Stdout())
3029

31-
err = process.Wait()
32-
if err != nil {
33-
// process failed
34-
} else {
35-
// process succeeded
36-
}
30+
process.Wait()
3731
conn.Close(websocket.StatusNormalClosure, "normal closure")
3832
```
3933

4034
### Server
4135

4236
```golang
4337
func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
44-
ws, err := websocket.Accept(w, r, nil)
45-
if err != nil {
46-
w.WriteHeader(http.StatusInternalServerError)
47-
return
48-
}
49-
err = wsep.Serve(r.Context(), ws, wsep.LocalExecer{})
50-
if err != nil {
51-
ws.Close(websocket.StatusAbnormalClosure, "failed to serve execer")
52-
return
53-
}
54-
ws.Close(websocket.StatusNormalClosure, "normal closure")
38+
conn, _ := websocket.Accept(w, r, nil)
39+
40+
wsep.Serve(r.Context(), conn, wsep.LocalExecer{})
41+
42+
ws.Close(websocket.StatusNormalClosure, "normal closure")
5543
}
5644
```
5745

5846
### Development / Testing
5947

6048
Start a local executor:
6149

62-
```
50+
```sh
6351
go run ./dev/server
6452
```
6553

6654
Start a client:
6755

68-
```
56+
```sh
6957
go run ./dev/client tty bash
7058
go run ./dev/client notty ls
7159
```
@@ -74,7 +62,7 @@ go run ./dev/client notty ls
7462

7563
Local `sh` through a local `wsep` connection
7664

77-
```shellscript
65+
```shell script
7866
$ head -c 100000000 /dev/urandom > /tmp/random; cat /tmp/random | pv | time ./bin/client notty sh -c "cat > /dev/null"
7967

8068
95.4MiB 0:00:00 [ 269MiB/s] [ <=> ]

0 commit comments

Comments
 (0)