UNPKG

@syntropysoft/praetorian

Version:

Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.

139 lines 5.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const chalk_1 = __importDefault(require("chalk")); const ConfigParser_1 = require("../infrastructure/parsers/ConfigParser"); const EqualityRule_1 = require("../domain/rules/EqualityRule"); const FileReaderService_1 = require("../infrastructure/adapters/FileReaderService"); class Validate extends core_1.Command { async run() { const { args, flags } = await this.parse(Validate); try { // Determine files to compare let filesToCompare; if (args.files && args.files.length > 0) { // Use files from command line arguments filesToCompare = Array.isArray(args.files) ? args.files : [args.files]; } else { // Use configuration file const configParser = new ConfigParser_1.ConfigParser(flags.config); if (!configParser.exists()) { this.error(`Configuration file not found: ${flags.config}`); this.log(chalk_1.default.yellow('\nCreate a configuration file with:')); this.log(chalk_1.default.gray('praetorian init')); return; } if (flags.env) { filesToCompare = configParser.getEnvironmentFiles(flags.env); } else { filesToCompare = configParser.getFilesToCompare(); } } // Load and parse files const configFiles = await this.loadFiles(filesToCompare); // Run validation const rule = new EqualityRule_1.EqualityRule(); const result = await rule.execute(configFiles); // Display results this.displayResults(result, flags.output); // Exit with appropriate code if (!result.success) { this.exit(1); } } catch (error) { this.error(error instanceof Error ? error.message : 'Unknown error'); this.exit(1); } } async loadFiles(filePaths) { const fileReaderService = new FileReaderService_1.FileReaderService(); // Validate files before reading const { valid, invalid } = fileReaderService.validateFiles(filePaths); if (invalid.length > 0) { const supportedExtensions = fileReaderService.getSupportedExtensions().join(', '); throw new Error(`Unsupported file formats: ${invalid.join(', ')}. ` + `Supported extensions: ${supportedExtensions}`); } return await fileReaderService.readFiles(valid); } displayResults(result, outputFormat) { if (outputFormat === 'json') { console.log(JSON.stringify(result, null, 2)); return; } // Pretty output console.log(chalk_1.default.blue('\n📊 Validation Results:\n')); if (result.success) { console.log(chalk_1.default.green('✅ All files have consistent keys!')); } else { console.log(chalk_1.default.red('❌ Key inconsistencies found:')); for (const error of result.errors) { console.log(chalk_1.default.red(` • ${error.message}`)); } } if (result.warnings && result.warnings.length > 0) { console.log(chalk_1.default.yellow(`\n⚠️ ${result.warnings.length} warning(s):`)); for (const warning of result.warnings) { console.log(chalk_1.default.yellow(` • ${warning.message}`)); } } // Mostrar claves vacías como información (no afecta el pipeline) if (result.info && result.info.length > 0) { console.log(chalk_1.default.blue(`\nℹ️ ${result.info.length} empty key(s) found (informational):`)); for (const info of result.info) { console.log(chalk_1.default.blue(` • ${info.message}`)); } console.log(chalk_1.default.gray(' Note: Empty keys are informational only and do not affect validation success')); } // Summary if (result.metadata) { console.log(chalk_1.default.blue('\n📈 Summary:')); console.log(` • Files compared: ${result.metadata.filesCompared || 0}`); console.log(` • Total keys: ${result.metadata.totalKeys || 0}`); console.log(` • Empty keys: ${result.metadata.emptyKeys || 0}`); console.log(` • Duration: ${result.metadata.duration || 0}ms`); } } } Validate.description = 'Validate configuration files for key consistency'; Validate.examples = [ '$ praetorian validate', '$ praetorian validate --env dev', '$ praetorian validate config-dev.yaml config-prod.yaml', '$ praetorian validate --output json', ]; Validate.flags = { env: core_1.Flags.string({ char: 'e', description: 'Environment to validate (dev, staging, prod)', required: false, }), output: core_1.Flags.string({ char: 'o', description: 'Output format (pretty, json)', options: ['pretty', 'json'], default: 'pretty', }), config: core_1.Flags.string({ char: 'c', description: 'Path to praetorian.yaml configuration file', default: 'praetorian.yaml', }), help: core_1.Flags.help({ char: 'h' }), }; Validate.args = { files: core_1.Args.string({ description: 'Configuration files to compare', required: false, multiple: true, }), }; exports.default = Validate; //# sourceMappingURL=validate.js.map