import { webapp_client } from "@cocalc/frontend/webapp-client";
export async function writeTextFileToComputeServer({
value,
project_id,
compute_server_id,
path,
sudo,
}: {
value: string;
project_id: string;
compute_server_id: number;
path: string;
sudo?: boolean;
}) {
const base64value = Buffer.from(value).toString("base64");
const random = `.tmp${Math.random()}`;
if (sudo) {
const args = [
"sh",
"-c",
`echo "${base64value}" | base64 --decode > "${path}${random}" && mv "${path}${random}" "${path}"`,
];
await webapp_client.exec({
filesystem: true,
compute_server_id,
project_id,
command: "sudo",
args,
});
} else {
await webapp_client.exec({
filesystem: true,
compute_server_id,
project_id,
command: `echo "${base64value}" | base64 --decode > "${path}${random}" && mv "${path}${random}" "${path}"`,
});
}
}