Skip to content

Ensure we have a URL #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,42 @@ 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<string | undefined> {
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,
private readonly storage: Storage,
) {}

public async login(...args: string[]): Promise<void> {
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`))
Expand Down
15 changes: 13 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,25 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
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)
}
},
Expand Down