diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..75cf8a1 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,10 @@ +engines: + eslint: + enabled: true + channel: "eslint-3" + config: + config: ".eslintrc.json" + +ratings: + paths: + - "**.js" diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index 4427bf4..0000000 --- a/.eslintrc +++ /dev/null @@ -1,20 +0,0 @@ -{ - "env": { - "node": true - }, - "rules": { - "dot-notation": 2, - "indent": [2, 4, {"SwitchCase": 1}], - "one-var": [2, "never"], - "no-trailing-spaces": [2, { "skipBlankLines": false }], - "keyword-spacing": [2, { - "overrides": { }, - before: true, - after: true - }], - "no-delete-var": 2, - "no-label-var": 2, - "no-shadow": 2, - "no-unused-vars": [ 1, { "args": "none" }] - } -} diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..af59f37 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,25 @@ +{ + "env": { + "node": true, + "mocha": true + }, + "extends": "eslint:recommended", + "installedESLint": true, + "root": true, + "rules": { + "comma-dangle": [2, "only-multiline"], + "dot-notation": 2, + "indent": [2, 2, {"SwitchCase": 1}], + "one-var": [2, "never"], + "no-trailing-spaces": [2, { "skipBlankLines": false }], + "keyword-spacing": [2, { + "before": true, + "after": true + }], + "no-delete-var": 2, + "no-label-var": 2, + "no-shadow": 2, + "no-unused-vars": [ 1, { "args": "none" }], + "no-console": 0 + } +} diff --git a/.travis.yml b/.travis.yml index fbf165f..444ac5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ script: after_success: - npm install istanbul codecov - - ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -R spec + - npm run coverage - ./node_modules/.bin/codecov sudo: false diff --git a/index.js b/index.js index 92b941a..23fb67c 100644 --- a/index.js +++ b/index.js @@ -7,100 +7,100 @@ var log = '/var/log/haraka.log'; var plugin; exports.register = function () { - plugin = this; - plugin.get_logreader_ini(); - plugin.load_karma_ini(); + plugin = this; + plugin.get_logreader_ini(); + plugin.load_karma_ini(); }; exports.hook_init_http = function (next, server) { - server.http.app.use('/logs/:uuid', exports.get_logs); - server.http.app.use('/karma/rules', exports.get_rules); - return next(); + server.http.app.use('/logs/:uuid', exports.get_logs); + server.http.app.use('/karma/rules', exports.get_rules); + return next(); }; exports.get_logreader_ini = function () { - plugin = this; - plugin.cfg = plugin.config.get('log.reader.ini', function () { - plugin.get_logreader_ini(); - }); + plugin = this; + plugin.cfg = plugin.config.get('log.reader.ini', function () { + plugin.get_logreader_ini(); + }); - if (plugin.cfg.log && plugin.cfg.log.file) { - log = plugin.cfg.log.file; - } + if (plugin.cfg.log && plugin.cfg.log.file) { + log = plugin.cfg.log.file; + } }; exports.load_karma_ini = function () { - plugin.karma_cfg = plugin.config.get('karma.ini', function () { - plugin.load_karma_ini(); - }); + plugin.karma_cfg = plugin.config.get('karma.ini', function () { + plugin.load_karma_ini(); + }); - if (!plugin.karma_cfg.result_awards) return; - if (!plugin.result_awards) plugin.result_awards = {}; + if (!plugin.karma_cfg.result_awards) return; + if (!plugin.result_awards) plugin.result_awards = {}; - Object.keys(plugin.karma_cfg.result_awards).forEach(function (anum) { - var parts = plugin.karma_cfg.result_awards[anum] + Object.keys(plugin.karma_cfg.result_awards).forEach(function (anum) { + var parts = plugin.karma_cfg.result_awards[anum] .replace(/\s+/, ' ') .split(/(?:\s*\|\s*)/); - plugin.result_awards[anum] = { - pi_name : parts[0], - property : parts[1], - operator : parts[2], - value : parts[3], - award : parts[4], - reason : parts[5], - resolution : parts[6], - }; - }); + plugin.result_awards[anum] = { + pi_name : parts[0], + property : parts[1], + operator : parts[2], + value : parts[3], + award : parts[4], + reason : parts[5], + resolution : parts[6], + }; + }); }; exports.get_rules = function (req, res) { - res.send(JSON.stringify(plugin.result_awards)); + res.send(JSON.stringify(plugin.result_awards)); }; exports.get_logs = function (req, res) { - var uuid = req.params.uuid; - if (!/\-/.test(uuid)) { - return res.send('
Invalid Request'); - } - if (!/^[0-9A-F\-\.]{12,40}$/.test(uuid)) { - return res.send('Invalid Request'); - } + var uuid = req.params.uuid; + if (!/\-/.test(uuid)) { + return res.send('Invalid Request'); + } + if (!/^[0-9A-F\-\.]{12,40}$/.test(uuid)) { + return res.send('Invalid Request'); + } - var matched = ''; + var matched = ''; // spawning a grep process is quite a lot faster than fs.read // (yes, I benchmarked it) - var child = spawn('grep', [ uuid, log ]); - child.stdout.on('data', function (buffer) { - matched += buffer.toString(); + var child = spawn('grep', [ uuid, log ]); + child.stdout.on('data', function (buffer) { + matched += buffer.toString(); + }); + + child.stdout.on('end', function (err) { + if (err) return res.send('' + err + '
'); + + var rawLogs = ''; + var lastKarmaLine; + matched.split('\n').forEach(function (line) { + var trimmed = line.replace(/[A-F0-9\-\.]{12,40}/, '$UUID'); + rawLogs += trimmed + '' + err + '
'); - - var rawLogs = ''; - var lastKarmaLine; - matched.split('\n').forEach(function (line) { - var trimmed = line.replace(/[A-F0-9\-\.]{12,40}/, '$UUID'); - rawLogs += trimmed + '