Skip to content

Commit 3426434

Browse files
committed
Ensure we have a URL
It looks like when we switched to using the VS Code desktop module the URL was no longer included, which caused the extension to query localhost if you had never logged in before, which errors and causes the configuration to never happen. I will fix the module as well, but I figure we can check the URL in the plugin just in case.
1 parent c318bcc commit 3426434

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/commands.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,42 @@ import { Remote } from "./remote"
88
import { Storage } from "./storage"
99
import { OpenableTreeItem } from "./workspacesProvider"
1010

11+
// maybeAskUrl asks the user for the URL if it was not provided and normalizes
12+
// the returned URL.
13+
export async function maybeAskUrl(
14+
providedUrl: string | undefined | null,
15+
lastUsedUrl?: string,
16+
): Promise<string | undefined> {
17+
let url =
18+
providedUrl ||
19+
(await vscode.window.showInputBox({
20+
title: "Coder URL",
21+
prompt: "Enter the URL of your Coder deployment.",
22+
placeHolder: "https://example.coder.com",
23+
value: lastUsedUrl,
24+
}))
25+
if (!url) {
26+
return undefined
27+
}
28+
if (!url.startsWith("http://") && !url.startsWith("https://")) {
29+
// Default to HTTPS if not provided!
30+
// https://github.com/coder/vscode-coder/issues/44
31+
url = "https://" + url
32+
}
33+
while (url.endsWith("/")) {
34+
url = url.substring(0, url.length - 1)
35+
}
36+
return url
37+
}
38+
1139
export class Commands {
1240
public constructor(
1341
private readonly vscodeProposed: typeof vscode,
1442
private readonly storage: Storage,
1543
) {}
1644

1745
public async login(...args: string[]): Promise<void> {
18-
let url: string | undefined = args.length >= 1 ? args[0] : undefined
19-
if (!url) {
20-
url = await vscode.window.showInputBox({
21-
title: "Coder URL",
22-
prompt: "Enter the URL of your Coder deployment.",
23-
placeHolder: "https://example.coder.com",
24-
value: url,
25-
})
26-
}
27-
if (!url) {
28-
return
29-
}
30-
if (!url.startsWith("http://") && !url.startsWith("https://")) {
31-
// Default to HTTPS if not provided!
32-
// https://github.com/coder/vscode-coder/issues/44
33-
url = "https://" + url
34-
}
35-
while (url.endsWith("/")) {
36-
url = url.substring(0, url.length - 1)
37-
}
46+
const url = await maybeAskUrl(args[0])
3847
let token: string | undefined = args.length >= 2 ? args[1] : undefined
3948
if (!token) {
4049
const opened = await vscode.env.openExternal(vscode.Uri.parse(`${url}/cli-auth`))

src/extension.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,25 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
127127
throw new Error("workspace must be specified as a query parameter")
128128
}
129129

130-
const url = params.get("url")
131-
const token = params.get("token")
130+
// We are not guaranteed that the URL we currently have is for the URL
131+
// this workspace belongs to, or that we even have a URL at all (the
132+
// queries will default to localhost) so ask for it if missing.
133+
// Pre-populate in case we do have the right URL so the user can just
134+
// hit enter and move on.
135+
const url = await maybeAskUrl(params.get("url"), storage.getURL())
132136
if (url) {
133137
await storage.setURL(url)
138+
} else {
139+
throw new Error("url must be provided or specified as a query parameter")
134140
}
141+
142+
// If the token is missing we will get a 401 later and the user will be
143+
// prompted to sign in again, so we do not need to ensure it is set.
144+
const token = params.get("token")
135145
if (token) {
136146
await storage.setSessionToken(token)
137147
}
148+
138149
vscode.commands.executeCommand("coder.open", owner, workspace, agent, folder)
139150
}
140151
},

0 commit comments

Comments
 (0)