MCPcopy Index your code
hub / github.com/coder/mux / runCommand

Function runCommand

src/node/runtime/DockerRuntime.ts:79–131  ·  view source on GitHub ↗
(
  command: string,
  args: string[],
  options: { timeoutMs?: number; shell?: boolean; abortSignal?: AbortSignal } = {}
)

Source from the content-addressed store, hash-verified

77 | { action: "create" }; // Doesn't exist, proceed to create
78
79function runCommand(
80 command: string,
81 args: string[],
82 options: { timeoutMs?: number; shell?: boolean; abortSignal?: AbortSignal } = {}
83): Promise<DockerCommandResult> {
84 return new Promise((resolve) => {
85 let stdout = "";
86 let stderr = "";
87 let settled = false;
88
89 if (options.abortSignal?.aborted) {
90 resolve({ exitCode: -1, stdout, stderr: "Command aborted" });
91 return;
92 }
93
94 const child = spawn(command, args, { shell: options.shell === true });
95
96 const finish = (result: DockerCommandResult) => {
97 if (settled) return;
98 settled = true;
99 clearTimeout(timer);
100 options.abortSignal?.removeEventListener("abort", onAbort);
101 resolve(result);
102 };
103
104 const timer = setTimeout(() => {
105 child.kill();
106 finish({ exitCode: -1, stdout, stderr: "Command timed out" });
107 }, options.timeoutMs ?? 30000);
108
109 const onAbort = () => {
110 child.kill();
111 finish({ exitCode: -1, stdout, stderr: "Command aborted" });
112 };
113 options.abortSignal?.addEventListener("abort", onAbort, { once: true });
114
115 child.stdout?.on("data", (data: Buffer) => {
116 stdout += data.toString();
117 });
118
119 child.stderr?.on("data", (data: Buffer) => {
120 stderr += data.toString();
121 });
122
123 child.on("close", (code) => {
124 finish({ exitCode: code ?? -1, stdout, stderr });
125 });
126
127 child.on("error", (err) => {
128 finish({ exitCode: -1, stdout, stderr: err.message });
129 });
130 });
131}
132
133/**
134 * Run a Docker CLI command and return result.

Callers 2

runDockerCommandFunction · 0.70
runSpawnCommandFunction · 0.70

Calls 5

onMethod · 0.80
finishFunction · 0.70
killMethod · 0.65
resolveFunction · 0.50
addEventListenerMethod · 0.45

Tested by

no test coverage detected