diff --git a/coder-sdk/config.go b/coder-sdk/config.go index 7912e78a..f00cd6ff 100644 --- a/coder-sdk/config.go +++ b/coder-sdk/config.go @@ -100,7 +100,7 @@ type configSetupMode struct { func (c Client) SiteSetupModeEnabled(ctx context.Context) (bool, error) { var conf configSetupMode if err := c.requestBody(ctx, http.MethodGet, "/api/config/setup-mode", nil, &conf); err != nil { - return false, nil + return false, err } return conf.SetupMode, nil } diff --git a/coder-sdk/env.go b/coder-sdk/env.go index cf4cfcc9..92505226 100644 --- a/coder-sdk/env.go +++ b/coder-sdk/env.go @@ -5,6 +5,7 @@ import ( "net/http" "time" + "cdr.dev/wsep" "golang.org/x/xerrors" "nhooyr.io/websocket" "nhooyr.io/websocket/wsjson" @@ -142,8 +143,17 @@ func (c Client) EditEnvironment(ctx context.Context, envID string, req UpdateEnv // DialWsep dials an environments command execution interface // See https://github.com/cdr/wsep for details. -func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn, error) { - return c.dialWebsocket(ctx, "/proxy/environments/"+env.ID+"/wsep") +func (c Client) DialWsep(ctx context.Context, envID string) (*websocket.Conn, error) { + return c.dialWebsocket(ctx, "/proxy/environments/"+envID+"/wsep") +} + +// DialExecutor gives a remote execution interface for performing commands inside an environment. +func (c Client) DialExecutor(ctx context.Context, envID string) (wsep.Execer, error) { + ws, err := c.DialWsep(ctx, envID) + if err != nil { + return nil, err + } + return wsep.RemoteExecer(ws), nil } // DialIDEStatus opens a websocket connection for cpu load metrics on the environment. @@ -234,17 +244,17 @@ type buildLogMsg struct { } // WaitForEnvironmentReady will watch the build log and return when done. -func (c Client) WaitForEnvironmentReady(ctx context.Context, env *Environment) error { - conn, err := c.DialEnvironmentBuildLog(ctx, env.ID) +func (c Client) WaitForEnvironmentReady(ctx context.Context, envID string) error { + conn, err := c.DialEnvironmentBuildLog(ctx, envID) if err != nil { - return xerrors.Errorf("%s: dial build log: %w", env.Name, err) + return xerrors.Errorf("%s: dial build log: %w", envID, err) } for { msg := buildLogMsg{} err := wsjson.Read(ctx, conn, &msg) if err != nil { - return xerrors.Errorf("%s: reading build log msg: %w", env.Name, err) + return xerrors.Errorf("%s: reading build log msg: %w", envID, err) } if msg.Type == BuildLogTypeDone { diff --git a/internal/cmd/shell.go b/internal/cmd/shell.go index acf733df..1e448358 100644 --- a/internal/cmd/shell.go +++ b/internal/cmd/shell.go @@ -140,7 +140,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err ctx, cancel := context.WithCancel(ctx) defer cancel() - conn, err := client.DialWsep(ctx, env) + conn, err := client.DialWsep(ctx, env.ID) if err != nil { return xerrors.Errorf("dial websocket: %w", err) } diff --git a/internal/sync/singlefile.go b/internal/sync/singlefile.go index d36a7372..0cc03ef0 100644 --- a/internal/sync/singlefile.go +++ b/internal/sync/singlefile.go @@ -17,7 +17,7 @@ import ( // SingleFile copies the given file into the remote dir or remote path of the given coder.Environment. func SingleFile(ctx context.Context, local, remoteDir string, env *coder.Environment, client *coder.Client) error { - conn, err := client.DialWsep(ctx, env) + conn, err := client.DialWsep(ctx, env.ID) if err != nil { return xerrors.Errorf("dial remote execer: %w", err) } diff --git a/internal/sync/sync.go b/internal/sync/sync.go index ee990c1b..fcca0518 100644 --- a/internal/sync/sync.go +++ b/internal/sync/sync.go @@ -89,7 +89,7 @@ func (s Sync) syncPaths(delete bool, local, remote string) error { } func (s Sync) remoteCmd(ctx context.Context, prog string, args ...string) error { - conn, err := s.Client.DialWsep(ctx, &s.Env) + conn, err := s.Client.DialWsep(ctx, s.Env.ID) if err != nil { return xerrors.Errorf("dial websocket: %w", err) } @@ -270,7 +270,7 @@ func (s Sync) Version() (string, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - conn, err := s.Client.DialWsep(ctx, &s.Env) + conn, err := s.Client.DialWsep(ctx, s.Env.ID) if err != nil { return "", err }