2023-05-23 18:53:53 +00:00
|
|
|
import { SysCallMapping } from "../../plugos/system.ts";
|
2023-07-24 07:36:33 +00:00
|
|
|
import type { Client } from "../client.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
export function shellSyscalls(
|
2023-07-24 07:36:33 +00:00
|
|
|
client: Client,
|
2023-05-23 18:53:53 +00:00
|
|
|
): SysCallMapping {
|
|
|
|
return {
|
|
|
|
"shell.run": async (
|
|
|
|
_ctx,
|
|
|
|
cmd: string,
|
|
|
|
args: string[],
|
|
|
|
): Promise<{ stdout: string; stderr: string; code: number }> => {
|
2023-07-24 07:36:33 +00:00
|
|
|
if (!client.remoteSpacePrimitives) {
|
2023-05-23 18:53:53 +00:00
|
|
|
throw new Error("Not supported in fully local mode");
|
|
|
|
}
|
2023-07-24 07:36:33 +00:00
|
|
|
const resp = client.remoteSpacePrimitives.authenticatedFetch(
|
|
|
|
`${client.remoteSpacePrimitives.url}/.rpc`,
|
2023-05-23 18:53:53 +00:00
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({
|
|
|
|
operation: "shell",
|
|
|
|
cmd,
|
|
|
|
args,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const { code, stderr, stdout } = await (await resp).json();
|
|
|
|
if (code !== 0) {
|
|
|
|
throw new Error(stderr);
|
|
|
|
}
|
|
|
|
return { code, stderr, stdout };
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|