Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refs #49 - Integrated yaml format to Configurator #101

Merged
merged 3 commits into from
Sep 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ node_modules/*
npm-debug.log
purrplingbot.log
config/*.json
config/*.yaml
config/*.yml
config/*.json.bk
!config/*.example.json
!config/*.example.yaml
!config/*.example.yml
92 changes: 81 additions & 11 deletions lib/configurator.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
const LOGGER = require("./logger.js");

var logger = LOGGER.createLogger("Configurator");

var path = [];

function readConfigFile(jsonFileName) {
function readConfigFile(fileName) {
try {
fs = require('fs');
var json = fs.readFileSync(jsonFileName, 'utf8').toString();
logger.log("Loaded config file: %s", jsonFileName);
return JSON.parse(json);
const fs = require('fs');
const path = require('path');
let type = path.extname(fileName);
var configContent = fs.readFileSync(fileName, 'utf8').toString();
logger.log("Loaded config file: %s", fileName);
return parseConfig(configContent, type);
} catch(err) {
logger.log("Failed while reading %s - %s", jsonFileName, err);
logger.log("Failed while reading %s - %s", fileName, err);
logger.log(err);
return null;
}
}

function parseConfig(configContent, type = ".yaml") {
const YAML = require('js-yaml');
let nativeObject;
if (!configContent) {
logger.error("Empty or NULL configuration!");
return;
}
if (type == ".yaml" || type == ".yml") {
try {
logger.log("Parsing YAML configuration");
nativeObject = YAML.safeLoad(configContent);
} catch (err) {
logger.error("Cannot parse YAML config: %s", err);
logger.log(err);
}
} else if (type == ".json") {
try {
logger.log("Parsing JSON configuration");
nativeObject = JSON.parse(configContent);
} catch (err) {
logger.error("Cannot parse JSON config: %s", err);
logger.log(err);
}
} else {
logger.error("Unknown configuration format: %s", type);
}
return nativeObject;
}

function loadImports(root, imports) {
for (nodeToImport in imports) {
path.push(nodeToImport);
Expand Down Expand Up @@ -49,10 +81,48 @@ function loadConfigFile(configFile) {
}

function loadConfiguration(configFile) {
logger.info("Loading configuration from: %s ...", configFile);
path = []; // Explicit reset path
return loadConfigFile(configFile);
const fs = require('fs');
const configFileNameVariants = [
configFile + ".yaml",
configFile + ".yml",
configFile + ".json",
];
let _configFile;
configFileNameVariants.every(cfile => {
if (fs.existsSync(cfile)) {
_configFile = cfile;
logger.log("Config file %s exists - Using it!", cfile);
return false;
}
logger.log("Config file %s not exists - Try next", cfile);
return true;
});
if (!_configFile) {
_configFile = configFile;
logger.log("Using config filename from param: %s", configFile);
}
logger.info("Loading configuration from: %s ...", _configFile);
path = []; // Force reset path
return loadConfigFile(_configFile);
}

function loadConfigurationMerged() {
var conf;
for (var i = 0; i < arguments.length; i++) {
conf = mergeConfigurations(conf, loadConfiguration(arguments[i]));
logger.log("Config file %s loaded and merged!", arguments[i]);
};
return conf;
}

exports.loadConfiguration = loadConfiguration;
exports.readConfigFile = readConfigFile;
function mergeConfigurations(whatObj, withObj) {
const merge = require('merge');
return merge.recursive(false, whatObj, withObj);
}

module.exports = {
loadConfiguration: loadConfiguration,
loadConfigurationMerged: loadConfigurationMerged,
readConfigFile: readConfigFile,
mergeConfigurations: mergeConfigurations,
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"console-stamp": "^0.2.5",
"discord.js": "^11.1.0",
"duration-parser": "^1.0.2",
"js-yaml": "^3.10.0",
"merge": "^1.2.0",
"moment": "^2.18.1",
"request": "^2.81.0",
"stringbuilder": "0.0.11",
Expand Down
2 changes: 1 addition & 1 deletion purrplingbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function init() {
// Load configuration
const Configurator = require("./lib/configurator.js");
try {
config = Configurator.loadConfiguration("config/config.json");
config = Configurator.loadConfiguration("config/config");
} catch (err) {
logger.error("*** Configuration failed to load! Check the config file.");
logger.error(err);
Expand Down