Skip to content

Commit e45281b

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 e45281b

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

src/commands.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,37 @@ 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(providedUrl: string | undefined | null, lastUsedUrl?: string): Promise<string | undefined> {
14+
let url = providedUrl || await vscode.window.showInputBox({
15+
title: "Coder URL",
16+
prompt: "Enter the URL of your Coder deployment.",
17+
placeHolder: "https://example.coder.com",
18+
value: lastUsedUrl,
19+
})
20+
if (!url) {
21+
return undefined
22+
}
23+
if (!url.startsWith("http://") && !url.startsWith("https://")) {
24+
// Default to HTTPS if not provided!
25+
// https://github.com/coder/vscode-coder/issues/44
26+
url = "https://" + url
27+
}
28+
while (url.endsWith("/")) {
29+
url = url.substring(0, url.length - 1)
30+
}
31+
return url
32+
}
33+
1134
export class Commands {
1235
public constructor(
1336
private readonly vscodeProposed: typeof vscode,
1437
private readonly storage: Storage,
1538
) {}
1639

1740
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-
}
41+
const url = await maybeAskUrl(args[0])
3842
let token: string | undefined = args.length >= 2 ? args[1] : undefined
3943
if (!token) {
4044
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)