|
1 | | -import * as os from "node:os"; |
2 | | - |
3 | 1 | import { execCommand } from "./command/exec"; |
4 | 2 | import { type Logger } from "./logging/logger"; |
5 | | -import { escapeCommandArg } from "./util"; |
6 | | - |
7 | | -import type { WorkspaceConfiguration } from "vscode"; |
8 | | - |
9 | | -export function getHeaderCommand( |
10 | | - config: Pick<WorkspaceConfiguration, "get">, |
11 | | -): string | undefined { |
12 | | - const cmd = |
13 | | - config.get<string>("coder.headerCommand")?.trim() || |
14 | | - process.env.CODER_HEADER_COMMAND?.trim(); |
15 | | - |
16 | | - return cmd || undefined; |
17 | | -} |
18 | | - |
19 | | -export function getHeaderArgs( |
20 | | - config: Pick<WorkspaceConfiguration, "get">, |
21 | | -): string[] { |
22 | | - // Escape a command line to be executed by the Coder binary, so ssh doesn't substitute variables. |
23 | | - const escapeSubcommand: (str: string) => string = |
24 | | - os.platform() === "win32" |
25 | | - ? // On Windows variables are %VAR%, and we need to use double quotes. |
26 | | - (str) => escapeCommandArg(str).replace(/%/g, "%%") |
27 | | - : // On *nix we can use single quotes to escape $VARS. |
28 | | - // Note single quotes cannot be escaped inside single quotes. |
29 | | - (str) => `'${str.replace(/'/g, "'\\''")}'`; |
30 | | - |
31 | | - const command = getHeaderCommand(config); |
32 | | - if (!command) { |
33 | | - return []; |
34 | | - } |
35 | | - return ["--header-command", escapeSubcommand(command)]; |
36 | | -} |
37 | 3 |
|
38 | 4 | /** |
39 | | - * getHeaders executes the header command and parses the headers from stdout. |
40 | | - * Both stdout and stderr are logged on error but stderr is otherwise ignored. |
41 | | - * Throws an error if the process exits with non-zero or the JSON is invalid. |
42 | | - * Returns undefined if there is no header command set. No effort is made to |
43 | | - * validate the JSON other than making sure it can be parsed. |
| 5 | + * Executes the header command and parses headers from stdout. |
| 6 | + * Throws on non-zero exit or malformed output. Returns empty headers if no |
| 7 | + * command is set. |
44 | 8 | */ |
45 | 9 | export async function getHeaders( |
46 | 10 | url: string | undefined, |
|
0 commit comments