|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +import { EXIT_CODES, parseCommandArgs, usageError } from '../../infrastructure.js'; |
| 4 | +import { openGraph } from '../../shared.js'; |
| 5 | + |
| 6 | +/** @typedef {import('../../types.js').CliOptions} CliOptions */ |
| 7 | + |
| 8 | +export const WORKING_SET_SUBCOMMAND = Object.freeze({ |
| 9 | + name: 'transfer-plan', |
| 10 | + summary: 'Plan a deterministic transfer from one working set into live, base, or another working set', |
| 11 | +}); |
| 12 | + |
| 13 | +const TRANSFER_PLAN_OPTIONS = { |
| 14 | + into: { type: 'string', default: 'live' }, |
| 15 | + 'lamport-ceiling': { type: 'string' }, |
| 16 | + 'into-lamport-ceiling': { type: 'string' }, |
| 17 | +}; |
| 18 | + |
| 19 | +const transferPlanSchema = z.object({ |
| 20 | + into: z.string().default('live'), |
| 21 | + 'lamport-ceiling': z.coerce.number().int().nonnegative().optional(), |
| 22 | + 'into-lamport-ceiling': z.coerce.number().int().nonnegative().optional(), |
| 23 | +}).strict().transform((val, ctx) => { |
| 24 | + const rawInto = val.into.trim(); |
| 25 | + /** @type {'base'|'live'|{ kind: 'working_set', workingSetId: string }} */ |
| 26 | + let into; |
| 27 | + if (rawInto === 'base' || rawInto === 'live') { |
| 28 | + into = rawInto; |
| 29 | + } else if (rawInto.startsWith('working-set:') && rawInto.length > 'working-set:'.length) { |
| 30 | + into = { |
| 31 | + kind: /** @type {const} */ ('working_set'), |
| 32 | + workingSetId: rawInto.slice('working-set:'.length), |
| 33 | + }; |
| 34 | + } else { |
| 35 | + ctx.addIssue({ |
| 36 | + code: z.ZodIssueCode.custom, |
| 37 | + path: ['into'], |
| 38 | + message: 'into must be base, live, or working-set:<id>', |
| 39 | + }); |
| 40 | + return z.NEVER; |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + intoRaw: rawInto, |
| 45 | + transferOptions: /** @type {{ |
| 46 | + into?: 'base'|'live'|{ kind: 'working_set', workingSetId: string }, |
| 47 | + ceiling?: number|null, |
| 48 | + intoCeiling?: number|null |
| 49 | + }} */ ({ |
| 50 | + into, |
| 51 | + ceiling: val['lamport-ceiling'] ?? null, |
| 52 | + intoCeiling: val['into-lamport-ceiling'] ?? null, |
| 53 | + }), |
| 54 | + }; |
| 55 | +}); |
| 56 | + |
| 57 | +/** |
| 58 | + * @param {{options: CliOptions, args: string[]}} params |
| 59 | + * @returns {Promise<{payload: unknown, exitCode: number}>} |
| 60 | + */ |
| 61 | +export async function handleWorkingSetSubcommand({ options, args }) { |
| 62 | + const { values, positionals } = parseCommandArgs(args, TRANSFER_PLAN_OPTIONS, transferPlanSchema, { |
| 63 | + allowPositionals: true, |
| 64 | + }); |
| 65 | + if (positionals.length !== 1) { |
| 66 | + throw usageError( |
| 67 | + 'Usage: warp-graph working-set transfer-plan <id> [--into live|base|working-set:<id>] [--lamport-ceiling <n>] [--into-lamport-ceiling <n>]', |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + const workingSetId = positionals[0]; |
| 72 | + const { graph, graphName } = await openGraph(options); |
| 73 | + const transferPlan = await graph.planWorkingSetTransfer(workingSetId, values.transferOptions); |
| 74 | + |
| 75 | + return { |
| 76 | + payload: { |
| 77 | + graph: graphName, |
| 78 | + workingSetAction: 'transfer-plan', |
| 79 | + workingSetId, |
| 80 | + into: values.intoRaw, |
| 81 | + transferPlan, |
| 82 | + }, |
| 83 | + exitCode: EXIT_CODES.OK, |
| 84 | + }; |
| 85 | +} |
0 commit comments