diff --git a/cli.js b/cli.js index 149a302..6c668a6 100755 --- a/cli.js +++ b/cli.js @@ -33,6 +33,10 @@ function start (opts) { opts.cloud = { id: opts.cloud } } + if (opts.rejectUnauthorized) { + opts.rejectUnauthorized = opts.rejectUnauthorized !== 'false' + } + const stream = pinoElasticSearch(opts) stream.on('unknown', (line, error) => { @@ -45,47 +49,66 @@ function start (opts) { console.error('Elasticsearch server error:', error) }) - if (opts.rejectUnauthorized) { - opts.rejectUnauthorized = opts.rejectUnauthorized !== 'false' - } pump(process.stdin, stream) } -const flags = minimist(process.argv.slice(2), { - alias: { - version: 'v', - help: 'h', - node: 'n', - index: 'i', - 'flush-bytes': 'f', - 'flush-interval': 't', - 'trace-level': 'l', - username: 'u', - password: 'p', - 'api-key': 'k', - cloud: 'c', - 'read-config': 'r' - }, - default: { - node: 'http://localhost:9200' - } -}) - -const allowedProps = ['node', 'index', 'flush-bytes', 'flush-interval', 'trace-level', 'username', 'password', 'api-key', 'cloud', 'es-version', 'rejectUnauthorized'] +function startCli (flags) { + const allowedProps = [ + 'node', + 'index', + 'flush-bytes', + 'flush-interval', + 'trace-level', + 'username', + 'password', + 'api-key', + 'cloud', + 'es-version', + 'rejectUnauthorized' + ] -if (flags['read-config']) { - if (flags['read-config'].match(/.*\.json$/) !== null) { - const config = JSON.parse(fs.readFileSync(path.join(process.cwd(), flags['read-config']), 'utf-8')) - allowedProps.forEach(key => { - if (config[key]) { flags[key] = config[key] } - }) + if (flags['read-config']) { + if (flags['read-config'].match(/.*\.json$/) !== null) { + const config = JSON.parse(fs.readFileSync(path.join(process.cwd(), flags['read-config']), 'utf-8')) + allowedProps.forEach(key => { + if (config[key] !== undefined) { + flags[key] = config[key] + } + }) + } + if (flags['read-config'].match(/.*\.js$/) !== null) { + const config = require(path.join(process.cwd(), flags['read-config'])) + allowedProps.forEach(key => { + if (config[key] !== undefined) { + flags[key] = config[key] + } + }) + } } - if (flags['read-config'].match(/.*\.js$/) !== null) { - const config = require(path.join(process.cwd(), flags['read-config'])) - allowedProps.forEach(key => { - if (config[key]) { flags[key] = config[key] } - }) - } + start(flags) } -start(flags) + +if (require.main === module) { + startCli(minimist(process.argv.slice(2), { + alias: { + version: 'v', + help: 'h', + node: 'n', + index: 'i', + 'flush-bytes': 'f', + 'flush-interval': 't', + 'trace-level': 'l', + username: 'u', + password: 'p', + 'api-key': 'k', + cloud: 'c', + 'read-config': 'r' + }, + default: { + node: 'http://localhost:9200' + } + })) +} + +module.exports = startCli diff --git a/test/cli.test.js b/test/cli.test.js new file mode 100644 index 0000000..e0b182e --- /dev/null +++ b/test/cli.test.js @@ -0,0 +1,107 @@ +'use strict' +const test = require('tap').test +const proxyquire = require('proxyquire') + +test('CLI: arg node should passed to client constructor', async (t) => { + const cli = proxyquire('../cli.js', { + pump: () => { }, + './lib.js': (opts) => { + t.same(opts, { node: 'https://custom-node-url:9999' }) + return { + on: () => { } + } + } + }) + + cli({ node: 'https://custom-node-url:9999' }) +}) + +test('CLI: arg rejectUnauthorized, if set to \'true\', should passed as true (bool) to client constructor', async (t) => { + const cli = proxyquire('../cli.js', { + pump: () => { }, + './lib.js': (opts) => { + t.same(opts, { + node: 'https://custom-node-url:9999', + rejectUnauthorized: true + }) + return { + on: () => { } + } + } + }) + + cli({ + node: 'https://custom-node-url:9999', + rejectUnauthorized: 'true' + }) +}) + +test('CLI: arg rejectUnauthorized, if set to \'false\', should passed as false (bool) to client constructor', async (t) => { + const cli = proxyquire('../cli.js', { + pump: () => { }, + './lib.js': (opts) => { + t.same(opts, { + node: 'https://custom-node-url:9999', + rejectUnauthorized: false + }) + return { + on: () => { } + } + } + }) + + cli({ + node: 'https://custom-node-url:9999', + rejectUnauthorized: 'false' + }) +}) + +test('CLI: arg rejectUnauthorized, if set to anything instead of true or false, should passed as true (bool) to client constructor', async (t) => { + const cli = proxyquire('../cli.js', { + pump: () => { }, + './lib.js': (opts) => { + t.same(opts, { + node: 'https://custom-node-url:9999', + rejectUnauthorized: true + }) + return { + on: () => { } + } + } + }) + + cli({ + node: 'https://custom-node-url:9999', + rejectUnauthorized: 'anything' + }) +}) + +test('CLI: if arg.read-config is set, should read the config file and passed the value (only allowed values)', async (t) => { + const cli = proxyquire('../cli.js', { + pump: () => { }, + './lib.js': (opts) => { + t.same(opts, { + index: 'custom-index', + node: 'https://localhost:9200', + rejectUnauthorized: false, + auth: { + username: 'elastic', + password: 'pass' + }, + // some keys are redundant, it is intended as it is. + // (see function start() in cli.js) + 'read-config': 'test/exampleConfig.js', + username: 'elastic', + password: 'pass' + }) + return { + on: () => { } + } + } + }) + + cli({ + node: 'https://custom-node-url:9999', + 'read-config': 'test/exampleConfig.js' + }) +}) diff --git a/test/exampleConfig.js b/test/exampleConfig.js new file mode 100644 index 0000000..93a77d8 --- /dev/null +++ b/test/exampleConfig.js @@ -0,0 +1,7 @@ +module.exports = { + index: 'custom-index', + node: 'https://localhost:9200', + rejectUnauthorized: false, + username: 'elastic', + password: 'pass' +}