-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathheaders.ts
More file actions
36 lines (30 loc) · 1.22 KB
/
headers.ts
File metadata and controls
36 lines (30 loc) · 1.22 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
import * as os from "node:os";
import { escapeCommandArg } from "../util";
import type { WorkspaceConfiguration } from "vscode";
/** Returns the header command from settings or the CODER_HEADER_COMMAND env var. */
export function getHeaderCommand(
config: Pick<WorkspaceConfiguration, "get">,
): string | undefined {
const cmd =
config.get<string>("coder.headerCommand")?.trim() ||
process.env.CODER_HEADER_COMMAND?.trim();
return cmd || undefined;
}
/** Returns `--header-command` CLI args, escaped for the current platform. */
export function getHeaderArgs(
config: Pick<WorkspaceConfiguration, "get">,
): string[] {
// Escape a command line to be executed by the Coder binary, so ssh doesn't substitute variables.
const escapeSubcommand: (str: string) => string =
os.platform() === "win32"
? // On Windows variables are %VAR%, and we need to use double quotes.
(str) => escapeCommandArg(str).replace(/%/g, "%%")
: // On *nix we can use single quotes to escape $VARS.
// Note single quotes cannot be escaped inside single quotes.
(str) => `'${str.replace(/'/g, "'\\''")}'`;
const command = getHeaderCommand(config);
if (!command) {
return [];
}
return ["--header-command", escapeSubcommand(command)];
}