/** * Service Remove Command * Remove a container service */ import { eq } from 'drizzle-orm'; import { getDb } from '../../db/client'; import { moduleInfrastructure } from '../../db/schema'; import { askConfirm, withInterviewSession } from '../../services/bus-interview'; import { getContainerServiceByServiceId, removeContainerService, } from '../../services/container-service'; import { celiloIntro, celiloOutro } from '../prompts'; import type { CommandResult } from '../types'; /** * Handle service remove command * * @param args - Command arguments [service-id] * @param flags - Command flags (--force) */ export async function handleServiceRemove( args: string[], flags: Record = {}, ): Promise { try { celiloIntro('Remove Container Service'); // Get service ID from args const serviceId = args[0]; if (!serviceId) { return { success: false, error: 'Service ID is required\n\nUsage: celilo service remove ', }; } // Verify service exists const service = await getContainerServiceByServiceId(serviceId); if (!service) { return { success: false, error: `Service not found: ${serviceId}`, }; } // Check for active modules using this service const db = getDb(); const activeModules = await db .select() .from(moduleInfrastructure) .where(eq(moduleInfrastructure.serviceId, service.id)); if (activeModules.length > 0) { console.log(`\nWarning: ${activeModules.length} module(s) currently use this service:`); for (const module of activeModules) { console.log(` - ${module.moduleId}`); } console.log(''); if (!flags.force) { const confirmed = await withInterviewSession(() => askConfirm({ scope: `service:${service.serviceId}`, key: 'remove_unassign_modules', message: 'Remove service and unassign modules?', defaultValue: false, }), ); if (!confirmed) { return { success: false, error: 'Cancelled by user' }; } } } // Confirm deletion if (!flags.force) { const confirmed = await withInterviewSession(() => askConfirm({ scope: `service:${service.serviceId}`, key: 'remove', message: `Remove service '${service.serviceId}' (${service.name})?`, defaultValue: false, }), ); if (!confirmed) { return { success: false, error: 'Cancelled by user' }; } } // Remove the service await removeContainerService(service.id); celiloOutro(`Service '${service.serviceId}' (${service.name}) removed successfully!`); return { success: true, message: `Removed service: ${service.serviceId}`, }; } catch (error) { return { success: false, error: `Failed to remove service: ${error instanceof Error ? error.message : String(error)}`, }; } }