|
1 | | -import { type WorkspaceConfiguration } from "vscode"; |
| 1 | +import * as vscode from "vscode"; |
2 | 2 |
|
| 3 | +import { type FeatureSet } from "./featureSet"; |
3 | 4 | import { getHeaderArgs } from "./headers"; |
| 5 | +import { isKeyringSupported } from "./keyringStore"; |
4 | 6 | import { escapeCommandArg } from "./util"; |
5 | 7 |
|
| 8 | +export type CliAuth = |
| 9 | + | { mode: "global-config"; configDir: string } |
| 10 | + | { mode: "url"; url: string }; |
| 11 | + |
6 | 12 | /** |
7 | 13 | * Returns the raw global flags from user configuration. |
8 | 14 | */ |
9 | 15 | export function getGlobalFlagsRaw( |
10 | | - configs: Pick<WorkspaceConfiguration, "get">, |
| 16 | + configs: Pick<vscode.WorkspaceConfiguration, "get">, |
11 | 17 | ): string[] { |
12 | 18 | return configs.get<string[]>("coder.globalFlags", []); |
13 | 19 | } |
14 | 20 |
|
15 | 21 | /** |
16 | 22 | * Returns global configuration flags for Coder CLI commands. |
17 | | - * Always includes the `--global-config` argument with the specified config directory. |
| 23 | + * Includes either `--global-config` or `--url` depending on the auth mode. |
18 | 24 | */ |
19 | 25 | export function getGlobalFlags( |
20 | | - configs: Pick<WorkspaceConfiguration, "get">, |
21 | | - configDir: string, |
| 26 | + configs: Pick<vscode.WorkspaceConfiguration, "get">, |
| 27 | + auth: CliAuth, |
22 | 28 | ): string[] { |
| 29 | + const authFlags = |
| 30 | + auth.mode === "url" |
| 31 | + ? ["--url", escapeCommandArg(auth.url)] |
| 32 | + : ["--global-config", escapeCommandArg(auth.configDir)]; |
| 33 | + |
23 | 34 | // Last takes precedence/overrides previous ones |
24 | 35 | return [ |
25 | 36 | ...getGlobalFlagsRaw(configs), |
26 | | - "--global-config", |
27 | | - escapeCommandArg(configDir), |
| 37 | + ...authFlags, |
28 | 38 | ...getHeaderArgs(configs), |
29 | 39 | ]; |
30 | 40 | } |
31 | 41 |
|
| 42 | +/** |
| 43 | + * Single source of truth: should the extension use the OS keyring for this session? |
| 44 | + * Requires CLI >= 2.29.0, macOS or Windows, and the coder.useKeyring setting enabled. |
| 45 | + */ |
| 46 | +export function shouldUseKeyring(featureSet: FeatureSet): boolean { |
| 47 | + return ( |
| 48 | + featureSet.keyringAuth && |
| 49 | + isKeyringSupported() && |
| 50 | + vscode.workspace.getConfiguration().get<boolean>("coder.useKeyring", true) |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Resolves how the CLI should authenticate: via the keyring (`--url`) or via |
| 56 | + * the global config directory (`--global-config`). |
| 57 | + */ |
| 58 | +export function resolveCliAuth( |
| 59 | + featureSet: FeatureSet, |
| 60 | + deploymentUrl: string | undefined, |
| 61 | + configDir: string, |
| 62 | +): CliAuth { |
| 63 | + if (shouldUseKeyring(featureSet) && deploymentUrl) { |
| 64 | + return { mode: "url", url: deploymentUrl }; |
| 65 | + } |
| 66 | + return { mode: "global-config", configDir }; |
| 67 | +} |
| 68 | + |
32 | 69 | /** |
33 | 70 | * Returns SSH flags for the `coder ssh` command from user configuration. |
34 | 71 | */ |
35 | 72 | export function getSshFlags( |
36 | | - configs: Pick<WorkspaceConfiguration, "get">, |
| 73 | + configs: Pick<vscode.WorkspaceConfiguration, "get">, |
37 | 74 | ): string[] { |
38 | 75 | // Make sure to match this default with the one in the package.json |
39 | 76 | return configs.get<string[]>("coder.sshFlags", ["--disable-autostart"]); |
|
0 commit comments