Skip to content

Commit dc6b4d3

Browse files
committed
Pass version and auth options through execCoder
This will make it so that we can try authentication when the command fails (as opposed to during preflight) since the Coder CLI does not have a command that lets you check authentication status.
1 parent 0dc0c10 commit dc6b4d3

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/download.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import * as nodeWhich from "which"
77
import { requestResponse } from "./request"
88
import { context, debug, extractTar, extractZip, getAssetUrl, onLine, outputChannel, wrapExit } from "./utils"
99

10+
/**
11+
* Options for installing and authenticating the Coder CLI.
12+
*/
13+
export interface CoderOptions {
14+
accessUri?: string
15+
token?: string
16+
version?: string
17+
}
18+
1019
/**
1120
* Return "true" if the binary is found in $PATH.
1221
*/
@@ -17,13 +26,13 @@ export const binaryExists = async (bin: string): Promise<boolean> => {
1726
}
1827

1928
/**
20-
* Run a command with the Coder CLI after making sure it is installed. On
21-
* success stdout is returned. On failure the error will include stderr in the
22-
* message.
29+
* Run a command with the Coder CLI after making sure it is installed and
30+
* authenticated. On success stdout is returned. On failure the error will
31+
* include stderr in the message.
2332
*/
24-
export const execCoder = async (command: string): Promise<string> => {
33+
export const execCoder = async (command: string, opts?: CoderOptions): Promise<string> => {
2534
debug(`Run command: ${command}`)
26-
const coderBinary = await preflight()
35+
const coderBinary = await preflight(opts?.version)
2736
const output = await promisify(cp.exec)(coderBinary + " " + command)
2837
return output.stdout
2938
}
@@ -40,9 +49,6 @@ const coderInvocation = (): { cmd: string; args: string[] } => {
4049
return { cmd: process.platform === "win32" ? "coder.exe" : "coder", args: [] }
4150
}
4251

43-
/** Only one preflight request at a time. */
44-
let _preflight: Promise<string> | undefined
45-
4652
/**
4753
* Download the Coder CLI to the provided location and return that location.
4854
*/
@@ -178,13 +184,13 @@ export const maybeInstall = async (version: string): Promise<string> => {
178184
}
179185
}
180186

187+
/** Only one preflight request at a time. */
188+
let _preflight: Promise<string> | undefined
189+
181190
/**
182-
* Check that Coder is installed and authenticated. If not try installing and
183-
* authenticating.
191+
* Check that Coder is installed. If not try installing.
184192
*
185193
* Return the appropriate invocation for the binary.
186-
*
187-
* @TODO Implement authentication portion.
188194
*/
189195
export const preflight = async (version = "latest"): Promise<string> => {
190196
if (!_preflight) {

src/extension.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ export const uriHandler: vscode.UriHandler = {
4343
debug(`Handling URI: ${uri}`)
4444
switch (action) {
4545
case "open-workspace": {
46-
return preflight(getQueryValue(query.version)).then(() => openWorkspace(resource))
46+
return openWorkspace(resource, {
47+
accessUri: getQueryValue(query.accessUri),
48+
token: getQueryValue(query.token),
49+
version: getQueryValue(query.version),
50+
})
4751
}
4852
default:
4953
vscode.window.showErrorMessage(`Unknown action "${action}"`)

src/workspaces.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path"
22
import * as vscode from "vscode"
3-
import { execCoder } from "./download"
3+
import { CoderOptions, execCoder } from "./download"
44
import { mediaDir } from "./utils"
55

66
export class CoderWorkspacesProvider implements vscode.TreeDataProvider<CoderWorkspaceListItem> {
@@ -48,15 +48,22 @@ export const shutdownWorkspace = async (name: string): Promise<void> => {
4848
* Inject Coder hosts into the SSH config file.
4949
*
5050
* If `remote.SSH.configFile` is set use that otherwise use the default.
51+
*
52+
* If version and authentication details are provided use those with the binary.
5153
*/
52-
const setupSSH = async (): Promise<void> => {
54+
const setupSSH = async (opts?: CoderOptions): Promise<void> => {
5355
const configFile = vscode.workspace.getConfiguration("remote.SSH").get("configFile")
54-
await execCoder(`config-ssh ${configFile ? `--filepath ${configFile}` : ""}`)
56+
await execCoder(`config-ssh ${configFile ? `--filepath ${configFile}` : ""}`, opts)
5557
}
5658

57-
export const openWorkspace = async (name: string): Promise<void> => {
59+
/**
60+
* Open the specified workspace.
61+
*
62+
* If version and authentication details are provided use those with the binary.
63+
*/
64+
export const openWorkspace = async (name: string, opts?: CoderOptions): Promise<void> => {
5865
try {
59-
await setupSSH()
66+
await setupSSH(opts)
6067
// If the provided workspace does not exist this is the point at which we
6168
// will find out because `coder sh` will exit with 1 causing the exec to
6269
// reject (piping should be avoided since the exit code is swallowed).

0 commit comments

Comments
 (0)