/** * Machine Earmark Command * Earmark a machine for a specific module, or clear an earmark */ import { getMachineByHostname, getMachineByIp, updateMachineEarmark, } from '../../services/machine-pool'; import { celiloIntro, celiloOutro } from '../prompts'; import type { CommandResult } from '../types'; /** * Handle machine earmark command * * @param args - [hostname|ip, module-id] or [hostname|ip, --clear] * @param flags - --clear to remove earmark */ export async function handleMachineEarmark( args: string[], flags: Record = {}, ): Promise { try { celiloIntro('Earmark Machine'); const identifier = args[0]; if (!identifier) { return { success: false, error: 'Machine hostname or IP is required\n\nUsage:\n celilo machine earmark \n celilo machine earmark --clear', }; } // Look up by IP first, then by hostname const isIp = /^\d+\.\d+\.\d+\.\d+$/.test(identifier); const machine = isIp ? await getMachineByIp(identifier) : await getMachineByHostname(identifier); if (!machine) { return { success: false, error: `Machine not found: ${identifier}`, }; } // Clear earmark if (flags.clear) { await updateMachineEarmark(machine.id, null); celiloOutro(`Cleared earmark on '${machine.hostname}' (${machine.ipAddress})`); return { success: true, message: `Cleared earmark on ${machine.hostname}`, }; } // Set earmark const moduleId = args[1]; if (!moduleId) { return { success: false, error: 'Module ID is required\n\nUsage:\n celilo machine earmark \n celilo machine earmark --clear', }; } await updateMachineEarmark(machine.id, moduleId); celiloOutro(`Earmarked '${machine.hostname}' (${machine.ipAddress}) for module '${moduleId}'`); return { success: true, message: `Earmarked ${machine.hostname} for ${moduleId}`, }; } catch (error) { return { success: false, error: `Failed to earmark machine: ${error instanceof Error ? error.message : String(error)}`, }; } }