diff --git a/src/commands.ts b/src/commands.ts index c099a34f..3c0792ef 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -8,6 +8,34 @@ import { Remote } from "./remote" import { Storage } from "./storage" import { OpenableTreeItem } from "./workspacesProvider" +// maybeAskUrl asks the user for the URL if it was not provided and normalizes +// the returned URL. +export async function maybeAskUrl( + providedUrl: string | undefined | null, + lastUsedUrl?: string, +): Promise { + let url = + providedUrl || + (await vscode.window.showInputBox({ + title: "Coder URL", + prompt: "Enter the URL of your Coder deployment.", + placeHolder: "https://example.coder.com", + value: lastUsedUrl, + })) + if (!url) { + return undefined + } + if (!url.startsWith("http://") && !url.startsWith("https://")) { + // Default to HTTPS if not provided! + // https://github.com/coder/vscode-coder/issues/44 + url = "https://" + url + } + while (url.endsWith("/")) { + url = url.substring(0, url.length - 1) + } + return url +} + export class Commands { public constructor( private readonly vscodeProposed: typeof vscode, @@ -15,26 +43,7 @@ export class Commands { ) {} public async login(...args: string[]): Promise { - let url: string | undefined = args.length >= 1 ? args[0] : undefined - if (!url) { - url = await vscode.window.showInputBox({ - title: "Coder URL", - prompt: "Enter the URL of your Coder deployment.", - placeHolder: "https://example.coder.com", - value: url, - }) - } - if (!url) { - return - } - if (!url.startsWith("http://") && !url.startsWith("https://")) { - // Default to HTTPS if not provided! - // https://github.com/coder/vscode-coder/issues/44 - url = "https://" + url - } - while (url.endsWith("/")) { - url = url.substring(0, url.length - 1) - } + const url = await maybeAskUrl(args[0]) let token: string | undefined = args.length >= 2 ? args[1] : undefined if (!token) { const opened = await vscode.env.openExternal(vscode.Uri.parse(`${url}/cli-auth`)) diff --git a/src/extension.ts b/src/extension.ts index 1bb82b84..7aa73f94 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -127,14 +127,25 @@ export async function activate(ctx: vscode.ExtensionContext): Promise { throw new Error("workspace must be specified as a query parameter") } - const url = params.get("url") - const token = params.get("token") + // We are not guaranteed that the URL we currently have is for the URL + // this workspace belongs to, or that we even have a URL at all (the + // queries will default to localhost) so ask for it if missing. + // Pre-populate in case we do have the right URL so the user can just + // hit enter and move on. + const url = await maybeAskUrl(params.get("url"), storage.getURL()) if (url) { await storage.setURL(url) + } else { + throw new Error("url must be provided or specified as a query parameter") } + + // If the token is missing we will get a 401 later and the user will be + // prompted to sign in again, so we do not need to ensure it is set. + const token = params.get("token") if (token) { await storage.setSessionToken(token) } + vscode.commands.executeCommand("coder.open", owner, workspace, agent, folder) } },