Skip to content

Commit

Permalink
replace the rest of ./config with haraka-config (haraka#2119)
Browse files Browse the repository at this point in the history
* replace ./config with haraka-config
* replace ./config.js with deprecated shim
* remove test/config/test.*
* add haraka-config as dependency
* remove js-yaml dep (its in haraka-config)
  • Loading branch information
msimerson authored Sep 30, 2017
1 parent adadf83 commit ace74b7
Show file tree
Hide file tree
Showing 38 changed files with 44 additions and 1,718 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* New Features
* Fixes
* Changes
* config: replace ./config.js with haraka-config #2119

## 2.8.16 - Sep 30, 2017

Expand Down
6 changes: 2 additions & 4 deletions bin/haraka
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ else if (parsed.graceful) {
logger = require(path.join(base, "logger"));
if (!parsed.verbose)
logger.log = function () {} // disable logging for this
config = require(path.join(base, "config"));
config = require('haraka-config');
if (!config.get("smtp.ini").main.nodes) {
console.log("Graceful restart not possible without `nodes` value in smtp.ini");
process.exit();
Expand Down Expand Up @@ -461,7 +461,6 @@ else if (parsed.order) {
(path.join(process.env.HARAKA, 'node_modules'));
require('module')._initPaths(); // Horrible hack
}
require(path.join(base, 'configfile')).watch_files = false;
logger = require(path.join(base, "logger"));
if (!parsed.verbose)
logger.log = function () {} // disable logging for this
Expand Down Expand Up @@ -497,7 +496,6 @@ else if (parsed.test) {
(path.join(process.env.HARAKA, 'node_modules'));
require('module')._initPaths(); // Horrible hack
}
require(path.join(base, 'configfile')).watch_files = false;
logger = require(path.join(base, "logger"));
logger.loglevel = logger.levels.PROTOCOL;

Expand Down Expand Up @@ -674,7 +672,7 @@ else {
}

function send_internal_command (cmd, done) {
config = require(path.join(base, "config"));
config = require('haraka-config');
const key = config.get("internalcmd_key");
const smtp_ini = config.get("smtp.ini");
const listen_addrs = require(path.join(base, "server")).get_listen_addrs(smtp_ini.main);
Expand Down
134 changes: 4 additions & 130 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,132 +1,6 @@
'use strict';
'use strict'

const path = require('path');
module.exports = require('haraka-config')

const cfreader = require('./configfile');

module.exports = new Config();

function Config (root_path, no_overrides) {
this.root_path = root_path || cfreader.config_path;
this.module_config = function (defaults_path, overrides_path) {
const cfg = new Config(path.join(defaults_path, 'config'), true);
if (overrides_path) {
cfg.overrides_path = path.join(overrides_path, 'config');
}
return cfg;
};
if (process.env.HARAKA_TEST_DIR) {
this.root_path = path.join(process.env.HARAKA_TEST_DIR, 'config');
return;
}
if (process.env.HARAKA && !no_overrides) {
this.overrides_path = root_path || cfreader.config_path;
this.root_path = path.join(process.env.HARAKA, 'config');
}
}

Config.prototype.get = function (name, type, cb, options) {
const a = this.arrange_args([name, type, cb, options]);
if (!a[1]) a[1] = 'value';

const full_path = path.resolve(this.root_path, a[0]);

let results = cfreader.read_config(full_path, a[1], a[2], a[3]);

if (this.overrides_path) {
const overrides_path = path.resolve(this.overrides_path, a[0]);

const overrides = cfreader.read_config(overrides_path, a[1], a[2], a[3]);

results = merge_config(results, overrides, a[1]);
}

// Pass arrays by value to prevent config being modified accidentally.
if (Array.isArray(results)) {
return results.slice();
}

return results;
};

function merge_config (defaults, overrides, type) {
if (type === 'ini' || type === 'json' || type === 'yaml') {
return merge_struct(JSON.parse(JSON.stringify(defaults)), overrides);
}
else if (Array.isArray(overrides) && Array.isArray(defaults) &&
overrides.length > 0) {
return overrides;
}
else if (overrides != null) {
return overrides;
}
else {
return defaults;
}
}

function merge_struct (defaults, overrides) {
for (const k in overrides) {
if (k in defaults) {
if (typeof overrides[k] === 'object' &&
typeof defaults[k] === 'object') {
defaults[k] = merge_struct(defaults[k], overrides[k]);
}
else {
defaults[k] = overrides[k];
}
}
else {
defaults[k] = overrides[k];
}
}
return defaults;
}

/* ways get() can be called:
config.get('thing');
config.get('thing', type);
config.get('thing', cb);
config.get('thing', cb, options);
config.get('thing', options);
config.get('thing', type, cb);
config.get('thing', type, options);
config.get('thing', type, cb, options);
*/

Config.prototype.arrange_args = function (args) {
const fs_name = args.shift();
let fs_type = null;
let cb;
let options;

for (let i=0; i < args.length; i++) {
if (args[i] === undefined) continue;
switch (typeof args[i]) { // what is it?
case 'function':
cb = args[i];
break;
case 'object':
options = args[i];
break;
case 'string':
if (/^(ini|value|list|data|json|yaml|binary)$/.test(args[i])) {
fs_type = args[i];
break;
}
console.log('unknown string:' + args[i]);
break;
}
// console.log('unknown arg:' + args[i] + ', typeof: ' +
// typeof args[i]);
}

if (!fs_type) {
if (/\.json$/.test(fs_name)) fs_type = 'json';
else if (/\.yaml$/.test(fs_name)) fs_type = 'yaml';
else if (/\.ini$/.test(fs_name)) fs_type = 'ini';
else fs_type = 'value';
}

return [fs_name, fs_type, cb, options];
};
// use emit is the same way util.deprecate does it, so follow that style
process.emit('warning', 'Loading config via require("./config") is deprecated, please use: require("haraka-config") instead.')
Loading

0 comments on commit ace74b7

Please sign in to comment.