/** * Backup Delete Command * Delete a specific backup entry and its storage file. */ import { deleteBackupRecord, formatSize, getBackup } from '../../services/backup-metadata'; import { createStorageProvider, getBackupStorage } from '../../services/backup-storage'; import { askConfirm, withInterviewSession } from '../../services/bus-interview'; import { celiloIntro, celiloOutro } from '../prompts'; import type { CommandResult } from '../types'; export async function handleBackupDelete( args: string[], flags: Record = {}, ): Promise { try { celiloIntro('Delete Backup'); const backupId = args[0]; if (!backupId) { return { success: false, error: 'Backup ID is required\n\nUsage: celilo backup delete ', }; } const backup = getBackup(backupId); if (!backup) { return { success: false, error: `Backup not found: ${backupId}` }; } const storage = getBackupStorage(backup.storageId); const isSystem = backup.backupType === 'system_state'; const module = isSystem ? '[system]' : (backup.moduleId ?? 'unknown'); const date = new Date(backup.startedAt).toISOString().replace('T', ' ').substring(0, 19); console.log( `\n ${backup.id.substring(0, 8)} ${module} ${date} ${formatSize(backup.sizeBytes)} ${backup.status}`, ); console.log(` → ${storage?.storageId ?? '?'}: ${backup.storagePath}\n`); if (!flags.force) { const confirmed = await withInterviewSession(() => askConfirm({ scope: `backup:${backupId}`, key: 'delete', message: 'Delete this backup?', defaultValue: false, }), ); if (!confirmed) { return { success: false, error: 'Cancelled by user' }; } } // Delete from storage try { const provider = await createStorageProvider(backup.storageId); await provider.delete(backup.storagePath); } catch { // Storage file may already be gone — continue with DB cleanup } // Delete database record deleteBackupRecord(backup.id); celiloOutro(`Backup ${backup.id.substring(0, 8)} deleted.`); return { success: true, message: `Deleted backup: ${backupId}` }; } catch (error) { return { success: false, error: `Failed to delete backup: ${error instanceof Error ? error.message : String(error)}`, }; } }