UNPKG

@puls-atlas/cli

Version:

The Puls Atlas CLI tool for managing Atlas projects

79 lines 2.74 kB
import * as features from '../../utils/feature.js'; import { logger } from '../../utils/index.js'; import { normalizeOptionalString } from '../../utils/value.js'; import { executeSyncAdminRequest } from './syncApiAdmin.js'; const normalizeRequiredOption = (value, label) => { const normalizedValue = normalizeOptionalString(value) ?? ''; if (!normalizedValue) { throw new Error(`Atlas sync repair requires --${label} <value>.`); } return normalizedValue; }; export const runSyncRepair = async (options = {}, { executeSyncAdminRequestImpl = executeSyncAdminRequest, exitImpl = code => process.exit(code), loadFeatureContextImpl = features.loadFeatureContext, loggerImpl = logger, workingDirectory = process.cwd() } = {}) => { try { const context = await loadFeatureContextImpl('sync', options, { cwd: workingDirectory }); const workloadKey = normalizeRequiredOption(options.workloadKey, 'workload-key'); const sourceDocumentId = normalizeRequiredOption(options.documentId, 'document-id'); const sourceDocumentPath = normalizeOptionalString(options.documentPath) ?? undefined; const spinner = loggerImpl.spinner('Repairing Atlas sync item...'); try { const payload = { workloadKey, sourceDocumentId, ...(sourceDocumentPath !== undefined ? { sourceDocumentPath } : {}) }; const result = await executeSyncAdminRequestImpl(context, { method: 'POST', path: '/admin/repair/sync', payload }); spinner.succeed('Atlas sync repair started.'); loggerImpl.summary('Repair summary', [{ label: 'Project', value: context.projectId }, context.environment ? { label: 'Environment', value: context.environment } : null, { label: 'Workload key', value: result.workloadKey ?? workloadKey }, { label: 'Source document', value: result.sourceDocumentId ?? sourceDocumentId }, { label: 'Sync key', value: result.syncKey ?? 'unknown' }, { label: 'Sync status', value: result.syncStatus ?? 'unknown' }, { label: 'Sync version', value: result.syncVersion ?? 'unknown' }, { label: 'Previous failure category', value: result.previousFailureCategory ?? 'unknown' }, { label: 'Task name', value: result.taskName ?? 'unknown' }]); return result; } catch (error) { spinner.fail('Atlas sync repair failed.'); throw error; } } catch (error) { loggerImpl.error(error.message, false); return exitImpl(1); } }; export default async (options = {}) => runSyncRepair(options ?? {});