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

Commit 6df7494

Browse files
authored
Add coder-sdk executor interface (#177)
1 parent 82282b1 commit 6df7494

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

coder-sdk/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ type configSetupMode struct {
100100
func (c Client) SiteSetupModeEnabled(ctx context.Context) (bool, error) {
101101
var conf configSetupMode
102102
if err := c.requestBody(ctx, http.MethodGet, "/api/config/setup-mode", nil, &conf); err != nil {
103-
return false, nil
103+
return false, err
104104
}
105105
return conf.SetupMode, nil
106106
}

coder-sdk/env.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"time"
77

8+
"cdr.dev/wsep"
89
"golang.org/x/xerrors"
910
"nhooyr.io/websocket"
1011
"nhooyr.io/websocket/wsjson"
@@ -142,8 +143,17 @@ func (c Client) EditEnvironment(ctx context.Context, envID string, req UpdateEnv
142143

143144
// DialWsep dials an environments command execution interface
144145
// See https://github.com/cdr/wsep for details.
145-
func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn, error) {
146-
return c.dialWebsocket(ctx, "/proxy/environments/"+env.ID+"/wsep")
146+
func (c Client) DialWsep(ctx context.Context, envID string) (*websocket.Conn, error) {
147+
return c.dialWebsocket(ctx, "/proxy/environments/"+envID+"/wsep")
148+
}
149+
150+
// DialExecutor gives a remote execution interface for performing commands inside an environment.
151+
func (c Client) DialExecutor(ctx context.Context, envID string) (wsep.Execer, error) {
152+
ws, err := c.DialWsep(ctx, envID)
153+
if err != nil {
154+
return nil, err
155+
}
156+
return wsep.RemoteExecer(ws), nil
147157
}
148158

149159
// DialIDEStatus opens a websocket connection for cpu load metrics on the environment.
@@ -234,17 +244,17 @@ type buildLogMsg struct {
234244
}
235245

236246
// WaitForEnvironmentReady will watch the build log and return when done.
237-
func (c Client) WaitForEnvironmentReady(ctx context.Context, env *Environment) error {
238-
conn, err := c.DialEnvironmentBuildLog(ctx, env.ID)
247+
func (c Client) WaitForEnvironmentReady(ctx context.Context, envID string) error {
248+
conn, err := c.DialEnvironmentBuildLog(ctx, envID)
239249
if err != nil {
240-
return xerrors.Errorf("%s: dial build log: %w", env.Name, err)
250+
return xerrors.Errorf("%s: dial build log: %w", envID, err)
241251
}
242252

243253
for {
244254
msg := buildLogMsg{}
245255
err := wsjson.Read(ctx, conn, &msg)
246256
if err != nil {
247-
return xerrors.Errorf("%s: reading build log msg: %w", env.Name, err)
257+
return xerrors.Errorf("%s: reading build log msg: %w", envID, err)
248258
}
249259

250260
if msg.Type == BuildLogTypeDone {

internal/cmd/shell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
140140
ctx, cancel := context.WithCancel(ctx)
141141
defer cancel()
142142

143-
conn, err := client.DialWsep(ctx, env)
143+
conn, err := client.DialWsep(ctx, env.ID)
144144
if err != nil {
145145
return xerrors.Errorf("dial websocket: %w", err)
146146
}

internal/sync/singlefile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
// SingleFile copies the given file into the remote dir or remote path of the given coder.Environment.
1919
func SingleFile(ctx context.Context, local, remoteDir string, env *coder.Environment, client *coder.Client) error {
20-
conn, err := client.DialWsep(ctx, env)
20+
conn, err := client.DialWsep(ctx, env.ID)
2121
if err != nil {
2222
return xerrors.Errorf("dial remote execer: %w", err)
2323
}

internal/sync/sync.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (s Sync) syncPaths(delete bool, local, remote string) error {
8989
}
9090

9191
func (s Sync) remoteCmd(ctx context.Context, prog string, args ...string) error {
92-
conn, err := s.Client.DialWsep(ctx, &s.Env)
92+
conn, err := s.Client.DialWsep(ctx, s.Env.ID)
9393
if err != nil {
9494
return xerrors.Errorf("dial websocket: %w", err)
9595
}
@@ -270,7 +270,7 @@ func (s Sync) Version() (string, error) {
270270
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
271271
defer cancel()
272272

273-
conn, err := s.Client.DialWsep(ctx, &s.Env)
273+
conn, err := s.Client.DialWsep(ctx, s.Env.ID)
274274
if err != nil {
275275
return "", err
276276
}

0 commit comments

Comments
 (0)