|
| 1 | +/** |
| 2 | + * T-CMP-01 & T-CMP-07: Compare command CLI registration and output formatting. |
| 3 | + * |
| 4 | + * Commander subcommand with: |
| 5 | + * --source-resource-group, --source-service-name (required) |
| 6 | + * --target-resource-group, --target-service-name (required) |
| 7 | + * --source-subscription-id, --target-subscription-id (optional overrides) |
| 8 | + * |
| 9 | + * Inherits global options: --subscription-id, --cloud, --format, --log-level, auth flags. |
| 10 | + * |
| 11 | + * Exit codes: |
| 12 | + * 0 = identical |
| 13 | + * 1 = differences found |
| 14 | + * 2 = fatal error |
| 15 | + */ |
| 16 | + |
| 17 | +import { Command } from 'commander'; |
| 18 | +import { CompareConfig } from '../models/config.js'; |
| 19 | +import { ApimServiceContext } from '../models/types.js'; |
| 20 | +import { runCompare, CompareResult } from '../services/compare-service.js'; |
| 21 | +import { logger, parseLogLevel } from '../lib/logger.js'; |
| 22 | +import { ApimClient } from '../clients/apim-client.js'; |
| 23 | +import { getCloudConfig, buildArmBaseUrl } from '../lib/cloud-config.js'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Interface for compare command options (from CLI flags). |
| 27 | + */ |
| 28 | +interface CompareOptions { |
| 29 | + sourceResourceGroup: string; |
| 30 | + sourceServiceName: string; |
| 31 | + targetResourceGroup: string; |
| 32 | + targetServiceName: string; |
| 33 | + sourceSubscriptionId?: string; |
| 34 | + targetSubscriptionId?: string; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Create and return the compare command for Commander. |
| 39 | + */ |
| 40 | +export function createCompareCommand(): Command { |
| 41 | + const compare = new Command('compare') |
| 42 | + .description('Compare two Azure APIM instances and report differences') |
| 43 | + .requiredOption('--source-resource-group <rg>', 'Source APIM resource group') |
| 44 | + .requiredOption('--source-service-name <name>', 'Source APIM service instance name') |
| 45 | + .requiredOption('--target-resource-group <rg>', 'Target APIM resource group') |
| 46 | + .requiredOption('--target-service-name <name>', 'Target APIM service instance name') |
| 47 | + .option('--source-subscription-id <id>', 'Source subscription ID (overrides --subscription-id for source)') |
| 48 | + .option('--target-subscription-id <id>', 'Target subscription ID (overrides --subscription-id for target)') |
| 49 | + .action(async (options: CompareOptions, command: Command) => { |
| 50 | + const globalOpts = command.optsWithGlobals<{ |
| 51 | + logLevel?: string; |
| 52 | + subscriptionId?: string; |
| 53 | + cloud?: string; |
| 54 | + format?: string; |
| 55 | + apiVersion?: string; |
| 56 | + }>(); |
| 57 | + |
| 58 | + await executeCompare(options, globalOpts); |
| 59 | + }); |
| 60 | + |
| 61 | + return compare; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Execute the compare command. |
| 66 | + */ |
| 67 | +async function executeCompare( |
| 68 | + options: CompareOptions, |
| 69 | + globalOpts: { |
| 70 | + logLevel?: string; |
| 71 | + subscriptionId?: string; |
| 72 | + cloud?: string; |
| 73 | + format?: string; |
| 74 | + apiVersion?: string; |
| 75 | + }, |
| 76 | +): Promise<void> { |
| 77 | + const defaultSubscriptionId = |
| 78 | + globalOpts.subscriptionId ?? process.env.AZURE_SUBSCRIPTION_ID; |
| 79 | + |
| 80 | + const sourceSubscriptionId = options.sourceSubscriptionId ?? defaultSubscriptionId; |
| 81 | + const targetSubscriptionId = options.targetSubscriptionId ?? defaultSubscriptionId; |
| 82 | + |
| 83 | + if (!sourceSubscriptionId) { |
| 84 | + logger.error( |
| 85 | + 'Source subscription ID required: use --source-subscription-id, --subscription-id, or set AZURE_SUBSCRIPTION_ID', |
| 86 | + ); |
| 87 | + process.exit(2); |
| 88 | + } |
| 89 | + |
| 90 | + if (!targetSubscriptionId) { |
| 91 | + logger.error( |
| 92 | + 'Target subscription ID required: use --target-subscription-id, --subscription-id, or set AZURE_SUBSCRIPTION_ID', |
| 93 | + ); |
| 94 | + process.exit(2); |
| 95 | + } |
| 96 | + |
| 97 | + const apiVersion = |
| 98 | + globalOpts.apiVersion ?? process.env.AZURE_API_VERSION ?? '2024-05-01'; |
| 99 | + const cloudName = globalOpts.cloud ?? 'public'; |
| 100 | + const cloudConfig = getCloudConfig(cloudName); |
| 101 | + |
| 102 | + const sourceContext: ApimServiceContext = { |
| 103 | + subscriptionId: sourceSubscriptionId, |
| 104 | + resourceGroup: options.sourceResourceGroup, |
| 105 | + serviceName: options.sourceServiceName, |
| 106 | + apiVersion, |
| 107 | + baseUrl: buildArmBaseUrl( |
| 108 | + cloudName, |
| 109 | + sourceSubscriptionId, |
| 110 | + options.sourceResourceGroup, |
| 111 | + options.sourceServiceName, |
| 112 | + ), |
| 113 | + }; |
| 114 | + |
| 115 | + const targetContext: ApimServiceContext = { |
| 116 | + subscriptionId: targetSubscriptionId, |
| 117 | + resourceGroup: options.targetResourceGroup, |
| 118 | + serviceName: options.targetServiceName, |
| 119 | + apiVersion, |
| 120 | + baseUrl: buildArmBaseUrl( |
| 121 | + cloudName, |
| 122 | + targetSubscriptionId, |
| 123 | + options.targetResourceGroup, |
| 124 | + options.targetServiceName, |
| 125 | + ), |
| 126 | + }; |
| 127 | + |
| 128 | + const compareConfig: CompareConfig = { |
| 129 | + source: sourceContext, |
| 130 | + target: targetContext, |
| 131 | + logLevel: parseLogLevel(globalOpts.logLevel ?? 'info'), |
| 132 | + }; |
| 133 | + |
| 134 | + const client = new ApimClient(cloudConfig.authScope); |
| 135 | + const result = await runCompare(client, compareConfig); |
| 136 | + |
| 137 | + if (globalOpts.format === 'json') { |
| 138 | + outputJson(result); |
| 139 | + } else { |
| 140 | + outputText(result); |
| 141 | + } |
| 142 | + |
| 143 | + process.exit(result.exitCode); |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * T-CMP-07: JSON output mode for compare. |
| 148 | + * Machine-readable JSON to stdout with per-type results and summary. |
| 149 | + */ |
| 150 | +function outputJson(result: CompareResult): void { |
| 151 | + const output = { |
| 152 | + status: |
| 153 | + result.exitCode === 0 |
| 154 | + ? 'identical' |
| 155 | + : result.exitCode === 1 |
| 156 | + ? 'differences' |
| 157 | + : 'error', |
| 158 | + exitCode: result.exitCode, |
| 159 | + summary: { |
| 160 | + totalDiffs: result.totalDiffs, |
| 161 | + totalCompared: result.totalCompared, |
| 162 | + skippedTypes: result.skippedTypes, |
| 163 | + }, |
| 164 | + resourceTypes: result.typeResults.map((r) => ({ |
| 165 | + label: r.label, |
| 166 | + compared: r.compared, |
| 167 | + skipped: r.skipped, |
| 168 | + skipReason: r.skipReason, |
| 169 | + differences: r.differences.map((d) => ({ |
| 170 | + name: d.name, |
| 171 | + diffs: d.diffs, |
| 172 | + })), |
| 173 | + })), |
| 174 | + }; |
| 175 | + |
| 176 | + process.stdout.write(JSON.stringify(output, null, 2) + '\n'); |
| 177 | +} |
| 178 | + |
| 179 | +/** |
| 180 | + * Text output mode (default) — per-resource-type summary with difference details. |
| 181 | + */ |
| 182 | +function outputText(result: CompareResult): void { |
| 183 | + process.stdout.write('\n'); |
| 184 | + process.stdout.write('╔══════════════════════════════════════════════════════════════╗\n'); |
| 185 | + process.stdout.write('║ APIM Instance Comparison ║\n'); |
| 186 | + process.stdout.write('╚══════════════════════════════════════════════════════════════╝\n'); |
| 187 | + |
| 188 | + for (const r of result.typeResults) { |
| 189 | + if (r.skipped) { |
| 190 | + process.stdout.write(` ⚠️ ${r.label}: SKIPPED (${r.skipReason ?? 'unknown'})\n`); |
| 191 | + continue; |
| 192 | + } |
| 193 | + |
| 194 | + if (r.differences.length === 0) { |
| 195 | + process.stdout.write(` ✅ ${r.label}: ${r.compared} resource(s) matched\n`); |
| 196 | + } else { |
| 197 | + process.stdout.write(` ❌ ${r.label}: ${r.differences.length} difference(s)\n`); |
| 198 | + for (const diff of r.differences) { |
| 199 | + process.stdout.write(` ${diff.name}\n`); |
| 200 | + for (const line of diff.diffs) { |
| 201 | + process.stdout.write(` ${line}\n`); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + process.stdout.write('\n══════════════════════════════════════════════════════════════\n'); |
| 208 | + |
| 209 | + if (result.exitCode === 2) { |
| 210 | + process.stdout.write('💥 ERROR — fatal error during comparison\n'); |
| 211 | + } else if (result.totalDiffs === 0) { |
| 212 | + process.stdout.write( |
| 213 | + `✅ PASS — ${result.typeResults.length} resource type(s) compared, ${result.totalCompared} resource(s) matched\n`, |
| 214 | + ); |
| 215 | + } else { |
| 216 | + process.stdout.write( |
| 217 | + `❌ FAIL — ${result.totalDiffs} difference(s) found across ${result.typeResults.length} resource type(s) (${result.totalCompared} compared)\n`, |
| 218 | + ); |
| 219 | + } |
| 220 | + |
| 221 | + if (result.skippedTypes > 0) { |
| 222 | + process.stdout.write( |
| 223 | + ` (${result.skippedTypes} type(s) skipped due to query failures)\n`, |
| 224 | + ); |
| 225 | + } |
| 226 | +} |
0 commit comments