move to correct structure

This commit is contained in:
JOLIMAITRE Matthieu 2023-12-09 00:08:48 +01:00
parent 44825c4c31
commit 54c4e91af8
12 changed files with 118 additions and 1 deletions

59
instance/src/lib.ts Normal file
View file

@ -0,0 +1,59 @@
import { toText } from "https://deno.land/std@0.208.0/streams/to_text.ts";
import { lines } from "./lib/utils.ts";
import { ContainerConfig } from "./lib/config.ts";
export type CmdStatus = ReturnType<typeof new_cmd_status>;
export function new_cmd_status() {
return { kind: "status" as const };
}
export type CmdEnable = ReturnType<typeof new_cmd_enable>;
export function new_cmd_enable(name: string) {
return { kind: "enable" as const, name };
}
export type CmdDisable = ReturnType<typeof new_cmd_disable>;
export function new_cmd_disable(name: string) {
return { kind: "disable" as const, name };
}
export type CmdStop = ReturnType<typeof new_cmd_stop>;
export function new_cmd_stop() {
return { kind: "stop" as const };
}
export type Cmd = CmdStatus | CmdEnable | CmdDisable | CmdStop;
export async function* daemon_listen(sock_path: string) {
const server = Deno.listen({ transport: "unix", path: sock_path });
for await (const request of server) {
const respond = async (message: string) => {
try {
await request.write(new TextEncoder().encode(message));
} catch (_) { /* bof mais bon */ }
};
try {
for await (const line of lines(request.readable)) {
const cmd = JSON.parse(line) as Cmd;
yield { cmd, respond };
}
} catch (_) { /* ok tier */ }
}
}
export async function daemon_send(sock_path: string, command: Cmd) {
const request = await Deno.connect({ transport: "unix", path: sock_path });
const text = JSON.stringify(command) + "\r\n\r\n";
await request.write(new TextEncoder().encode(text));
return await toText(request.readable);
}
export type Runner = ReturnType<typeof start_runner>;
export function start_runner(config: ContainerConfig) {
return {
name: config.name,
stop: async () => {
// TODO
},
};
}