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

Commit 7ba07e9

Browse files
committed
Migrate coder sync to wsep
1 parent f0e7f9c commit 7ba07e9

File tree

7 files changed

+22
-328
lines changed

7 files changed

+22
-328
lines changed

Diff for: internal/entclient/env.go

-50
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package entclient
22

33
import (
44
"context"
5-
"net/url"
6-
"strconv"
75
"time"
86

97
"nhooyr.io/websocket"
@@ -24,16 +22,6 @@ func (c Client) Envs(user *User, org Org) ([]Environment, error) {
2422
return envs, err
2523
}
2624

27-
type WushOptions struct {
28-
TTY bool
29-
Stdin bool
30-
}
31-
32-
var defaultWushOptions = WushOptions{
33-
TTY: false,
34-
Stdin: true,
35-
}
36-
3725
func (c Client) DialWsep(ctx context.Context, env Environment) (*websocket.Conn, error) {
3826
u := c.copyURL()
3927
if c.BaseURL.Scheme == "https" {
@@ -61,41 +49,3 @@ func (c Client) DialWsep(ctx context.Context, env Environment) (*websocket.Conn,
6149
}
6250
return conn, nil
6351
}
64-
65-
func (c Client) DialWush(env Environment, opts *WushOptions, cmd string, args ...string) (*websocket.Conn, error) {
66-
u := c.copyURL()
67-
if c.BaseURL.Scheme == "https" {
68-
u.Scheme = "wss"
69-
} else {
70-
u.Scheme = "ws"
71-
}
72-
u.Path = "/proxy/environments/" + env.ID + "/wush-lite"
73-
query := make(url.Values)
74-
query.Set("command", cmd)
75-
query["args[]"] = args
76-
if opts == nil {
77-
opts = &defaultWushOptions
78-
}
79-
query.Set("tty", strconv.FormatBool(opts.TTY))
80-
query.Set("stdin", strconv.FormatBool(opts.Stdin))
81-
82-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
83-
defer cancel()
84-
85-
fullURL := u.String() + "?" + query.Encode()
86-
87-
conn, resp, err := websocket.Dial(ctx, fullURL,
88-
&websocket.DialOptions{
89-
HTTPHeader: map[string][]string{
90-
"Cookie": {"session_token=" + c.Token},
91-
},
92-
},
93-
)
94-
if err != nil {
95-
if resp != nil {
96-
return nil, bodyError(resp)
97-
}
98-
return nil, err
99-
}
100-
return conn, nil
101-
}

Diff for: internal/sync/sync.go

+22-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"go.coder.com/flog"
2323

2424
"cdr.dev/coder-cli/internal/entclient"
25-
"cdr.dev/coder-cli/wush"
25+
"cdr.dev/wsep"
2626
)
2727

2828
// Sync runs a live sync daemon.
@@ -67,22 +67,31 @@ func (s Sync) syncPaths(delete bool, local, remote string) error {
6767
return nil
6868
}
6969

70-
func (s Sync) remoteRm(remote string) error {
71-
conn, err := s.Client.DialWush(s.Environment, nil, "rm", "-rf", remote)
70+
func (s Sync) remoteRm(ctx context.Context, remote string) error {
71+
conn, err := s.Client.DialWsep(ctx, s.Environment)
7272
if err != nil {
7373
return err
7474
}
7575
defer conn.Close(websocket.CloseNormalClosure, "")
76-
wc := wush.NewClient(context.Background(), conn)
77-
go io.Copy(os.Stdout, wc.Stderr)
78-
go io.Copy(os.Stderr, wc.Stdout)
79-
code, err := wc.Wait()
76+
77+
execer := wsep.RemoteExecer(conn)
78+
process, err := execer.Start(ctx, wsep.Command{
79+
Command: "rm",
80+
Args: []string{"-rf", remote},
81+
})
8082
if err != nil {
81-
return xerrors.Errorf("wush failure: %w", err)
83+
return err
8284
}
83-
if code != 0 {
85+
go io.Copy(os.Stdout, process.Stderr())
86+
go io.Copy(os.Stderr, process.Stdout())
87+
88+
err = process.Wait()
89+
if code, ok := err.(wsep.ExitError); ok {
8490
return fmt.Errorf("rm exit status: %v", code)
8591
}
92+
if err != nil {
93+
return xerrors.Errorf("execution failure: %w", err)
94+
}
8695
return nil
8796
}
8897

@@ -128,7 +137,10 @@ func (s Sync) handleCreate(localPath string) error {
128137
}
129138

130139
func (s Sync) handleDelete(localPath string) error {
131-
return s.remoteRm(s.convertPath(localPath))
140+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
141+
defer cancel()
142+
143+
return s.remoteRm(ctx, s.convertPath(localPath))
132144
}
133145

134146
func (s Sync) handleRename(localPath string) error {

Diff for: wush/client.go

-197
This file was deleted.

Diff for: wush/clientmessage.go

-24
This file was deleted.

Diff for: wush/doc.go

-6
This file was deleted.

Diff for: wush/promise.go

-19
This file was deleted.

0 commit comments

Comments
 (0)