UNPKG

@syntropysoft/praetorian

Version:

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

207 lines • 8.04 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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 fs = __importStar(require("fs")); const yaml = __importStar(require("yaml")); const ConfigParser_1 = require("../../infrastructure/parsers/ConfigParser"); const EqualityRule_1 = require("../../domain/rules/EqualityRule"); 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 configFiles = []; for (const filePath of filePaths) { if (!fs.existsSync(filePath)) { throw new Error(`File not found: ${filePath}`); } const content = fs.readFileSync(filePath, 'utf8'); let parsedContent; if (filePath.endsWith('.yaml') || filePath.endsWith('.yml')) { parsedContent = yaml.parse(content); } else if (filePath.endsWith('.json')) { parsedContent = JSON.parse(content); } else if (filePath.endsWith('.env') || filePath.startsWith('env.')) { parsedContent = this.parseEnvFile(content); } else { throw new Error(`Unsupported file format: ${filePath}`); } configFiles.push({ path: filePath, content: parsedContent, format: this.getFileFormat(filePath), }); } return configFiles; } parseEnvFile(content) { const result = {}; const lines = content.split('\n'); for (const line of lines) { const trimmed = line.trim(); if (trimmed && !trimmed.startsWith('#')) { const [key, ...valueParts] = trimmed.split('='); if (key && valueParts.length > 0) { result[key.trim()] = valueParts.join('=').trim(); } } } return result; } getFileFormat(filePath) { if (filePath.endsWith('.yaml') || filePath.endsWith('.yml')) { return 'yaml'; } else if (filePath.endsWith('.json')) { return 'json'; } else if (filePath.endsWith('.env') || filePath.startsWith('env.')) { return 'env'; } throw new Error(`Unsupported file format: ${filePath}`); } 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}`)); } } // 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(` • 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