UNPKG

@puls-atlas/cli

Version:

The Puls Atlas CLI tool for managing Atlas projects

84 lines 2.97 kB
import * as features from '../../utils/feature.js'; import { logger } from '../../utils/index.js'; import { normalizeOptionalString } from '../../utils/value.js'; import { executeSearchSyncAdminRequest } from './searchApiAdmin.js'; const SUPPORTED_REPAIR_TARGETS = new Set(['sync']); const normalizeRepairTarget = target => normalizeOptionalString(target)?.toLowerCase() ?? null; const normalizeRequiredOption = (value, label) => { const normalizedValue = normalizeOptionalString(value) ?? ''; if (!normalizedValue) { throw new Error(`Atlas search repair requires --${label} <value>.`); } return normalizedValue; }; export const runSearchRepair = async (options = {}, { executeSearchSyncAdminRequestImpl = executeSearchSyncAdminRequest, exitImpl = code => process.exit(code), loadFeatureContextImpl = features.loadFeatureContext, loggerImpl = logger, workingDirectory = process.cwd() } = {}) => { const target = normalizeRepairTarget(options.target); if (!target || !SUPPORTED_REPAIR_TARGETS.has(target)) { loggerImpl.error('Atlas search repair requires <target> to be sync.', false); return exitImpl(1); } try { const context = await loadFeatureContextImpl('search', options, { cwd: workingDirectory }); const collection = normalizeRequiredOption(options.collection, 'collection'); const sourceDocumentId = normalizeRequiredOption(options.documentId, 'document-id'); const spinner = loggerImpl.spinner('Repairing Atlas search sync item...'); try { const result = await executeSearchSyncAdminRequestImpl(context, { method: 'POST', path: '/admin/repair/sync', payload: { collection, sourceDocumentId } }); spinner.succeed('Atlas search sync repair started.'); loggerImpl.summary('Repair summary', [{ label: 'Project', value: context.projectId }, context.environment ? { label: 'Environment', value: context.environment } : null, { label: 'Collection', value: result.collection ?? collection }, { label: 'Source document', value: result.sourceDocumentId ?? sourceDocumentId }, { label: 'Replay storage', value: 'DLQ bucket payload' }, { label: 'Previous failure category', value: result.previousFailureCategory ?? 'unknown' }, { label: 'Sync key', value: result.syncKey ?? 'unknown' }, { label: 'Sync version', value: result.syncVersion ?? 'unknown' }, { label: 'Task name', value: result.taskName ?? 'unknown' }]); return result; } catch (error) { spinner.fail('Atlas search sync repair failed.'); throw error; } } catch (error) { loggerImpl.error(error.message, false); return exitImpl(1); } }; export default async (target, options = {}) => runSearchRepair({ ...(options ?? {}), target });