@syntropysoft/praetorian
Version:
Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.
79 lines • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IniFileAdapter = void 0;
const AbstractFileAdapter_1 = require("../base/AbstractFileAdapter");
class IniFileAdapter extends AbstractFileAdapter_1.AbstractFileAdapter {
canHandle(filePath) {
return filePath.endsWith('.ini') || filePath.endsWith('.cfg') || filePath.endsWith('.conf');
}
async read(filePath) {
this.validateFileExists(filePath);
try {
const content = await this.readFileContent(filePath);
return this.parseIniContent(content);
}
catch (error) {
throw new Error(`Failed to parse INI file ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
parseIniContent(content) {
const result = {};
const lines = content.split('\n');
let currentSection = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Skip empty lines and comments
if (!line || line.startsWith(';') || line.startsWith('#')) {
continue;
}
// Handle section headers [section]
if (line.startsWith('[') && line.endsWith(']')) {
currentSection = line.slice(1, -1);
if (!result[currentSection]) {
result[currentSection] = {};
}
continue;
}
// Handle key-value pairs
const equalIndex = line.indexOf('=');
if (equalIndex > 0) {
const key = line.substring(0, equalIndex).trim();
const value = line.substring(equalIndex + 1).trim();
if (currentSection) {
result[currentSection][key] = this.parseValue(value);
}
else {
result[key] = this.parseValue(value);
}
}
}
return result;
}
parseValue(value) {
// Remove quotes
if ((value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))) {
return value.slice(1, -1);
}
// Parse booleans
const lowerValue = value.toLowerCase();
if (lowerValue === 'true' || lowerValue === 'yes' || lowerValue === 'on')
return true;
if (lowerValue === 'false' || lowerValue === 'no' || lowerValue === 'off')
return false;
// Parse numbers
if (!isNaN(Number(value))) {
return Number(value);
}
// Return as string
return value;
}
getFormat() {
return 'ini';
}
getSupportedExtensions() {
return ['.ini', '.cfg', '.conf'];
}
}
exports.IniFileAdapter = IniFileAdapter;
//# sourceMappingURL=IniFileAdapter.js.map