-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcliConfig.ts
More file actions
104 lines (92 loc) · 2.89 KB
/
cliConfig.ts
File metadata and controls
104 lines (92 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { isKeyringSupported } from "./core/cliCredentialManager";
import { getHeaderArgs } from "./headers";
import { escapeCommandArg } from "./util";
import type { WorkspaceConfiguration } from "vscode";
import type { FeatureSet } from "./featureSet";
export type CliAuth =
| { mode: "global-config"; configDir: string }
| { mode: "url"; url: string };
/**
* Returns the raw global flags from user configuration.
*/
export function getGlobalFlagsRaw(
configs: Pick<WorkspaceConfiguration, "get">,
): string[] {
return configs.get<string[]>("coder.globalFlags", []);
}
/**
* Returns global configuration flags for Coder CLI commands.
* Includes either `--global-config` or `--url` depending on the auth mode.
*/
export function getGlobalFlags(
configs: Pick<WorkspaceConfiguration, "get">,
auth: CliAuth,
): string[] {
const authFlags =
auth.mode === "url"
? ["--url", escapeCommandArg(auth.url)]
: ["--global-config", escapeCommandArg(auth.configDir)];
const raw = getGlobalFlagsRaw(configs);
const filtered: string[] = [];
for (let i = 0; i < raw.length; i++) {
if (isFlag(raw[i], "--use-keyring")) {
continue;
}
if (isFlag(raw[i], "--global-config")) {
// Skip the next item too when the value is a separate entry.
if (raw[i] === "--global-config") {
i++;
}
continue;
}
filtered.push(raw[i]);
}
return [...filtered, ...authFlags, ...getHeaderArgs(configs)];
}
function isFlag(item: string, name: string): boolean {
return (
item === name || item.startsWith(`${name}=`) || item.startsWith(`${name} `)
);
}
/**
* Returns true when the user has keyring enabled and the platform supports it.
*/
export function isKeyringEnabled(
configs: Pick<WorkspaceConfiguration, "get">,
): boolean {
return isKeyringSupported() && configs.get<boolean>("coder.useKeyring", true);
}
/**
* Single source of truth: should the extension use the OS keyring for this session?
* Requires CLI >= 2.29.0, macOS or Windows, and the coder.useKeyring setting enabled.
*/
export function shouldUseKeyring(
configs: Pick<WorkspaceConfiguration, "get">,
featureSet: FeatureSet,
): boolean {
return isKeyringEnabled(configs) && featureSet.keyringAuth;
}
/**
* Resolves how the CLI should authenticate: via the keyring (`--url`) or via
* the global config directory (`--global-config`).
*/
export function resolveCliAuth(
configs: Pick<WorkspaceConfiguration, "get">,
featureSet: FeatureSet,
deploymentUrl: string,
configDir: string,
): CliAuth {
if (shouldUseKeyring(configs, featureSet)) {
return { mode: "url", url: deploymentUrl };
}
return { mode: "global-config", configDir };
}
/**
* Returns SSH flags for the `coder ssh` command from user configuration.
*/
export function getSshFlags(
configs: Pick<WorkspaceConfiguration, "get">,
): string[] {
// Make sure to match this default with the one in the package.json
return configs.get<string[]>("coder.sshFlags", ["--disable-autostart"]);
}