A simple command-line tool to start NetBSD virtual machines using QEMU with sensible defaults.
at main 50 lines 1.3 kB view raw
1import { Data, Effect, pipe } from "effect"; 2import type { VirtualMachine } from "../db.ts"; 3import { getInstanceState, removeInstanceState } from "../state.ts"; 4 5class VmNotFoundError extends Data.TaggedError("VmNotFoundError")<{ 6 name: string; 7}> {} 8 9const findVm = (name: string) => 10 pipe( 11 getInstanceState(name), 12 Effect.flatMap((vm) => 13 vm ? Effect.succeed(vm) : Effect.fail(new VmNotFoundError({ name })) 14 ), 15 ); 16 17const logRemoving = (vm: VirtualMachine) => 18 Effect.sync(() => { 19 console.log(`Removing virtual machine ${vm.name} (ID: ${vm.id})...`); 20 }); 21 22const removeVm = (name: string, vm: VirtualMachine) => 23 pipe( 24 removeInstanceState(name), 25 Effect.map(() => vm), 26 ); 27 28const handleError = (error: VmNotFoundError | Error) => 29 Effect.sync(() => { 30 if (error instanceof VmNotFoundError) { 31 console.error( 32 `Virtual machine with name or ID ${error.name} not found.`, 33 ); 34 } else { 35 console.error(`An error occurred: ${error}`); 36 } 37 Deno.exit(1); 38 }); 39 40const removeEffect = (name: string) => 41 pipe( 42 findVm(name), 43 Effect.tap(logRemoving), 44 Effect.flatMap((vm) => removeVm(name, vm)), 45 Effect.catchAll(handleError), 46 ); 47 48export default async function (name: string) { 49 await Effect.runPromise(removeEffect(name)); 50}