Skip to content

Commit 4fa4342

Browse files
committed
Add dev helpers and docs
1 parent 8507776 commit 4fa4342

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5656
}
5757
```
5858

59+
### Development / Testing
60+
61+
Start a local executor:
62+
63+
```
64+
go run ./dev/server
65+
```
66+
67+
Start a client:
68+
69+
```
70+
go run ./dev/client tty bash
71+
go run ./dev/client notty ls
72+
```
73+
5974
### Performance Goals
6075

6176
Test command

dev/client/main.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"io"
6+
"os"
7+
8+
"cdr.dev/wsep"
9+
"cdr.dev/wsep/internal/proto"
10+
"github.com/spf13/pflag"
11+
"go.coder.com/cli"
12+
"go.coder.com/flog"
13+
"nhooyr.io/websocket"
14+
)
15+
16+
type notty struct {
17+
}
18+
19+
func (c *notty) Run(fl *pflag.FlagSet) {
20+
do(fl, false)
21+
}
22+
23+
func (c *notty) Spec() cli.CommandSpec {
24+
return cli.CommandSpec{
25+
Name: "notty",
26+
Usage: "[flags]",
27+
Desc: `Run a command without tty enabled.`,
28+
RawArgs: true,
29+
}
30+
}
31+
32+
type tty struct {
33+
}
34+
35+
func (c *tty) Run(fl *pflag.FlagSet) {
36+
do(fl, true)
37+
}
38+
39+
func (c *tty) Spec() cli.CommandSpec {
40+
return cli.CommandSpec{
41+
Name: "tty",
42+
Usage: "[flags]",
43+
Desc: `Run a command with tty enabled.`,
44+
RawArgs: true,
45+
}
46+
}
47+
48+
func do(fl *pflag.FlagSet, tty bool) {
49+
ctx, cancel := context.WithCancel(context.Background())
50+
defer cancel()
51+
52+
conn, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
53+
if err != nil {
54+
flog.Fatal("failed to dial remote executor: %v", err)
55+
}
56+
defer conn.Close(websocket.StatusAbnormalClosure, "terminate process")
57+
58+
executor := wsep.RemoteExecer(conn)
59+
60+
var args []string
61+
if len(fl.Args()) < 1 {
62+
flog.Fatal("a command argument is required")
63+
}
64+
if len(fl.Args()) > 1 {
65+
args = fl.Args()[1:]
66+
}
67+
process, err := executor.Start(ctx, proto.Command{
68+
Command: fl.Arg(0),
69+
Args: args,
70+
TTY: tty,
71+
})
72+
if err != nil {
73+
flog.Fatal("failed to start remote command: %v", err)
74+
}
75+
go io.Copy(os.Stdout, process.Stdout())
76+
go io.Copy(os.Stderr, process.Stderr())
77+
go io.Copy(process.Stdin(), os.Stdin)
78+
79+
err = process.Wait()
80+
if err != nil {
81+
flog.Error("process failed: %v", err)
82+
} else {
83+
flog.Info("process finished successfully")
84+
}
85+
conn.Close(websocket.StatusNormalClosure, "normal closure")
86+
}
87+
88+
type cmd struct {
89+
}
90+
91+
func (c *cmd) Spec() cli.CommandSpec {
92+
return cli.CommandSpec{
93+
Name: "wsep-client",
94+
Usage: "[flags]",
95+
Desc: `Run a simple wsep client for testing.`,
96+
RawArgs: true,
97+
}
98+
}
99+
100+
func (c *cmd) Run(fl *pflag.FlagSet) {
101+
fl.Usage()
102+
os.Exit(1)
103+
}
104+
func (c *cmd) Subcommands() []cli.Command {
105+
return []cli.Command{
106+
&notty{},
107+
&tty{},
108+
}
109+
}
110+
111+
func main() {
112+
cli.RunRoot(&cmd{})
113+
}

dev/server/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"cdr.dev/wsep"
7+
"go.coder.com/flog"
8+
"nhooyr.io/websocket"
9+
)
10+
11+
func main() {
12+
server := http.Server{
13+
Addr: ":8080",
14+
Handler: server{},
15+
}
16+
err := server.ListenAndServe()
17+
flog.Fatal("failed to listen: %v", err)
18+
}
19+
20+
type server struct{}
21+
22+
func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
23+
ws, err := websocket.Accept(w, r, &websocket.AcceptOptions{InsecureSkipVerify: true})
24+
if err != nil {
25+
w.WriteHeader(http.StatusInternalServerError)
26+
return
27+
}
28+
err = wsep.Serve(r.Context(), ws, wsep.LocalExecer{})
29+
if err != nil {
30+
flog.Fatal("failed to serve local execer: %v", err)
31+
}
32+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go 1.14
44

55
require (
66
github.com/creack/pty v1.1.11
7+
github.com/spf13/pflag v1.0.5
8+
go.coder.com/cli v0.4.0
79
go.coder.com/flog v0.0.0-20190906214207-47dd47ea0512
810
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
911
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
3030
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
3131
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
3232
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
33+
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
34+
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
35+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
36+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
3337
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3438
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3539
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
3640
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
3741
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
42+
go.coder.com/cli v0.4.0 h1:PruDGwm/CPFndyK/eMowZG3vzg5CgohRWeXWCTr3zi8=
43+
go.coder.com/cli v0.4.0/go.mod h1:hRTOURCR3LJF1FRW9arecgrzX+AHG7mfYMwThPIgq+w=
3844
go.coder.com/flog v0.0.0-20190906214207-47dd47ea0512 h1:DjCS6dRQh+1PlfiBmnabxfdrzenb0tAwJqFxDEH/s9g=
3945
go.coder.com/flog v0.0.0-20190906214207-47dd47ea0512/go.mod h1:83JsYgXYv0EOaXjIMnaZ1Fl6ddNB3fJnDZ/8845mUJ8=
4046
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=

0 commit comments

Comments
 (0)