3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 20:22:39 +01:00

getConfig now figures out value, appropriate changes for [#189]

This commit is contained in:
reality 2013-01-27 17:53:50 +00:00
parent 4a64928fd6
commit 74d8fd9f94

View File

@ -6,7 +6,7 @@ var fs = require('fs'),
var commands = function(dbot) { var commands = function(dbot) {
var noChangeConfig = [ 'servers', 'name', 'moduleNames' ]; var noChangeConfig = [ 'servers', 'name', 'moduleNames' ];
var getCurrentConfigPath = function(configKey) { var getCurrentConfig = function(configKey) {
var defaultConfigPath = dbot.config; var defaultConfigPath = dbot.config;
var userConfigPath = dbot.db.config; var userConfigPath = dbot.db.config;
@ -25,9 +25,22 @@ var commands = function(dbot) {
} }
} }
var currentOption;
if(configKey.length != 1) {
configKey = _.last(configKey);
if(_.has(userConfigPath, configKey) && !_.isUndefined(userConfigPath[configKey])) {
currentOption = userConfigPath[configKey];
} else if(_.has(defaultConfigPath, configKey)) {
currentOption = defaultConfigPath[configKey];
}
} else {
currentOption = defaultConfigPath[configKey];
}
return { return {
'user': userConfigPath, 'user': userConfigPath,
'default': defaultConfigPath 'default': defaultConfigPath,
'value': currentOption
}; };
}; };
@ -205,16 +218,13 @@ var commands = function(dbot) {
var newOption = event.params[2]; var newOption = event.params[2];
if(!_.include(noChangeConfig, configKey)) { if(!_.include(noChangeConfig, configKey)) {
var configPath = getCurrentConfigPath(configPathString); var configPath = getCurrentConfig(configPathString);
var currentOption;
if(_.has(configPath['user'], configKey)) { if(configPath == false || _.isUndefined(configPath.value)) {
currentOption = configPath['user'][configKey];
} else if(_.has(configPath['default'], configKey)) {
currentOption = configPath['default'][configKey];
} else {
event.reply("Config key doesn't exist bro"); event.reply("Config key doesn't exist bro");
return; return;
} }
var currentOption = configPath.value;
// Convert to boolean type if config item boolean // Convert to boolean type if config item boolean
if(_.isBoolean(currentOption)) { if(_.isBoolean(currentOption)) {
@ -225,8 +235,6 @@ var commands = function(dbot) {
event.reply("Config option is an array. Try 'pushconfig'."); event.reply("Config option is an array. Try 'pushconfig'.");
} }
// TODO: Same for numbers and that I assume
event.reply(configPathString + ": " + currentOption + " -> " + newOption); event.reply(configPathString + ": " + currentOption + " -> " + newOption);
configPath['user'][configKey] = newOption; configPath['user'][configKey] = newOption;
dbot.reloadModules(); dbot.reloadModules();
@ -237,24 +245,19 @@ var commands = function(dbot) {
'showconfig': function(event) { 'showconfig': function(event) {
var configPathString = event.params[1]; var configPathString = event.params[1];
var configPath = getCurrentConfigPath(configPathString); var configPath = getCurrentConfig(configPathString);
if(configPathString) { if(configPathString) {
var configKey = _.last(configPathString.split('.')); var configKey = _.last(configPathString.split('.'));
if(configKey == false) {
if(!_.has(configPath['default'], configKey)) {
event.reply("Config path doesn't exist"); event.reply("Config path doesn't exist");
return; return;
} }
if(_.isObject(configPath['default'][configKey])) { if(_.isObject(configPath.value)) {
event.reply('Config keys in ' + configPathString + ': ' + Object.keys(configPath['default'][configKey])); event.reply('Config keys in ' + configPathString + ': ' + Object.keys(configPath.value));
} else { } else {
var currentOption = configPath['default'][configKey]; event.reply(configKey + ': ' + configPath.value);
if(_.has(configPath['user'], configKey)) {
currentOption = configPath['user'][configKey];
}
event.reply(configKey + ': ' + currentOption);
} }
} else { } else {
event.reply('Config keys in root: ' + Object.keys(configPath['default'])); event.reply('Config keys in root: ' + Object.keys(configPath['default']));