import commander from 'commander'; import { errorAndExit } from './errorAndExit'; import { ChangeRequestOptions } from './getIamKey'; /** * Validates the ChangeMinder change request flags added by * `addChangeRequestOptions`. `--chg-number` is mutually exclusive with the * `--ciid`/`--activity-type`/`--description` trio, and if any of the trio is * supplied all three must be present and non-empty once trimmed — otherwise a * whitespace-only value reaches ALKS and fails there as a missing primary CI. * * Exits the process (via `errorAndExit`) on invalid input. */ export function validateChangeRequestOptions( options: commander.OptionValues ): void { const hasCiid = options.ciid !== undefined && options.ciid !== null; const hasActivityType = options.activityType !== undefined && options.activityType !== null; const hasDescription = options.description !== undefined && options.description !== null; const hasChgNumber = !!options.chgNumber; if (hasChgNumber) { if (hasCiid || hasActivityType || hasDescription) { errorAndExit( 'Do not provide --ciid, --activity-type, or --description when using --chg-number.' ); } } else if (hasCiid || hasActivityType || hasDescription) { const ciidVal = typeof options.ciid === 'string' ? options.ciid.trim() : ''; const activityTypeVal = typeof options.activityType === 'string' ? options.activityType.trim() : ''; const descriptionVal = typeof options.description === 'string' ? options.description.trim() : ''; if (!ciidVal || !activityTypeVal || !descriptionVal) { errorAndExit( 'If any of --ciid, --activity-type, or --description is provided, all three must be specified and non-empty.' ); } } } /** * Builds the `ChangeRequestOptions` payload passed to `getIamKey`. Call only * after `validateChangeRequestOptions` has accepted the same options. */ export function buildChangeRequestOptions( options: commander.OptionValues ): ChangeRequestOptions { if (options.chgNumber) { return { changeNumber: options.chgNumber }; } return { ciid: options.ciid, activityType: options.activityType, description: options.description, }; }