@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 35 lines 1.38 KB
Raw
1 /**
2 * Renders a healthcheck `cmd` as the single string Docker's `--health-cmd`
3 * flag takes. The flag always runs its value through the container's shell, so
4 * the Docker API's exec form has to be flattened:
5 *
6 * - `"cmd"`: used as is.
7 * - `["CMD-SHELL", "cmd"]`: the shell command, without the marker.
8 * - `["CMD", "pg_isready", "-U", "app"]`: the arguments, each quoted so it
9 * reaches the program as one argument.
10 * - `["NONE"]`: no command (the caller disables the healthcheck).
11 * - any other array: joined with spaces and run in the shell, as before, so
12 * `["curl -f localhost || exit 1"]` keeps working.
13 */
14 export const healthcheckCommand = (cmd: string[] | string): string | undefined => {
15 if (typeof cmd === "string") return cmd;
16 const [head, ...rest] = cmd;
17 switch (head) {
18 case "NONE":
19 return undefined;
20 case "CMD-SHELL":
21 return rest.join(" ");
22 case "CMD":
23 return rest.map(shellQuote).join(" ");
24 default:
25 return cmd.join(" ");
26 }
27 };
28
29 /** True when `cmd` is Docker's `["NONE"]` form, which disables the healthcheck. */
30 export const isHealthcheckDisabled = (cmd: string[] | string): boolean =>
31 Array.isArray(cmd) && cmd[0] === "NONE";
32
33 /** Single-quotes an argument unless it is made only of shell-safe characters. */
34 const shellQuote = (arg: string): string =>
35 /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replaceAll("'", `'\\''`)}'`;