Files
mtusi2026lab7/scripts/diagnose-command.ts
T

86 lines
4.1 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { local } from "./runtime";
const record = JSON.parse(
await readFile(resolve(local, "diagnostics/last-command.json"), "utf8"),
);
const content = String(record.stderr) + "\n" + String(record.stdout);
const categories: Record<string, RegExp> = {
IMAGE_ABSENT:
/No such image|pull access denied|not found locally|image.*not found/i,
PORT_BUSY: /port is already allocated|address already in use/i,
MOUNT_MISSING: /bind source path does not exist|no such file or directory/i,
NETWORK_MODE: /network mode|network namespace|network sharing container/i,
CONTAINER_DEPENDENCY: /dependency failed|depends on|not running|not started/i,
INVALID_CONFIG:
/invalid compose|validating|additional properties|invalid mount config/i,
PERMISSION: /permission denied|operation not permitted/i,
CONFLICT: /conflict|already in use by container/i,
READONLY: /read-only file system/i,
EXITED: /exited|exit code/i,
};
Object.assign(categories, {
CLI_USAGE: /unknown flag|unknown shorthand|usage:/i,
GO_RUNTIME: /runtime:|fatal error:|goroutine|panic:/i,
ALLOCATION: /memory|cannot allocate|resource temporarily unavailable/i,
CONNECTION: /cannot connect|connection refused|broken pipe/i,
YAML: /yaml:|unmarshal|interpolat/i,
NO_SPACE: /no space left/i,
});
Object.assign(categories, {
APPROVAL: /approval|review|permission request|sandbox/i,
HERDR: /herdr|hc socket/i,
COMPOSE_PLUGIN: /plugin|compose-switch/i,
TERMINAL: /terminal|tty|pty/i,
SYSTEM_BUS: /dbus|systemd|notification/i,
SOCKET: /socket/i,
NOT_SUPPORTED: /not supported|not permitted|not available/i,
});
Object.assign(categories, {
JSON_PATH_IS_DIRECTORY: /IsADirectoryError/i,
PYTHON_PERMISSION: /PermissionError/i,
JSON_DECODE: /JSONDecodeError/i,
});
console.log(JSON.stringify({ docker_binary: Bun.which("docker") }));
console.log(
JSON.stringify({
diagnostic_terms:
content
.toLowerCase()
.match(
/\b(?:accept|connect|binding|send|recv|recvmsg|recvfrom|sendmsg|sendto|notify|hook|pretooluse|tool|connection|exist|managed|bridge|proxy|shutdown|configure|configured|crun|runc|reset|closed|refused|such|running|transport|protocol|mode|argument|malformed|sock_seqpacket|econnrefused|econnreset|eof|eagain|einval|enotconn|enoent|eacces|failed|not|nix|unix|linux|tcp|udp|http|unmarshal|decode|activation|notification|setup|listen|listening|respond|response|request|status|code|before|after|running|server|client|machine|remote|local|wrapper|container|sandbox|policy|database|journal|bus|environ|environment|context|batch|interactive|terminal|timeout|timed|out|stream|input|output|parse|busy|stale|missing|detected|available|connected|disconnected|reset|broken|pipe|lock|waiting|wait|executable|binary|file|function|directory)\b/g,
) ?? [],
}),
);
const words =
content
.toLowerCase()
.match(
/\b(?:error|failed|unable|cannot|open|read|write|image|manifest|compose|docker|stat|path|file|directory|variable|env|permission|denied|missing|invalid|not|found|unknown|option|unexpected|end|json|yaml|unsupported|credential|helper|executable|plugin|build|network|socket|daemon|connect|pull|registry|login|version|platform|architecture|exec|format|storage|interpolation|valid|credentialstore|panic|timeout|outside|context|syntax|line|character|unclosed|unterminated|exited|code|closed|stdin|stdout|stderr|operation|parse|parsing|mapping|alias|anchor|checking|lookup|address|resolution|host|too|many|levels|symbolic|links)\b/g,
) ?? [];
console.log(
JSON.stringify({
program: ["docker", "make"].includes(record.program)
? record.program
: "OTHER",
exit: record.code,
stdout_bytes: String(record.stdout).length,
stderr_bytes: String(record.stderr).length,
categories: Object.entries(categories)
.filter(([, pattern]) => pattern.test(content))
.map(([code]) => code),
words,
services: [
"postgres",
"broker",
"s3",
"openbao",
"lb",
"api1",
"api2",
"worker",
"migrate",
].filter((name) => new RegExp("\\b" + name + "\\b").test(content)),
}),
);