diff --git a/src/cli/plugin/__tests__/setting_parser.js b/src/cli/plugin/__tests__/setting_parser.js index 384d24078b78d..f02d5905e090d 100644 --- a/src/cli/plugin/__tests__/setting_parser.js +++ b/src/cli/plugin/__tests__/setting_parser.js @@ -76,11 +76,11 @@ describe('kibana cli', function () { options = { install: 'dummy/dummy', pluginDir: fromRoot('installedPlugins') }; }); - it('should require the user to specify either install and remove', function () { + it('should require the user to specify either install, remove, or list', function () { options.install = null; parser = settingParser(options); - expect(parser.parse).withArgs().to.throwError(/Please specify either --install or --remove./); + expect(parser.parse).withArgs().to.throwError(/Please specify either --install, --remove, or --list./); }); it('should not allow the user to specify both install and remove', function () { @@ -88,7 +88,32 @@ describe('kibana cli', function () { options.install = 'org/package/version'; parser = settingParser(options); - expect(parser.parse).withArgs().to.throwError(/Please specify either --install or --remove./); + expect(parser.parse).withArgs().to.throwError(/Please specify either --install, --remove, or --list./); + }); + + it('should not allow the user to specify both install and list', function () { + options.list = true; + options.install = 'org/package/version'; + parser = settingParser(options); + + expect(parser.parse).withArgs().to.throwError(/Please specify either --install, --remove, or --list./); + }); + + it('should not allow the user to specify both remove and list', function () { + options.list = true; + options.remove = 'package'; + parser = settingParser(options); + + expect(parser.parse).withArgs().to.throwError(/Please specify either --install, --remove, or --list./); + }); + + it('should not allow the user to specify install, remove, and list', function () { + options.list = true; + options.install = 'org/package/version'; + options.remove = 'package'; + parser = settingParser(options); + + expect(parser.parse).withArgs().to.throwError(/Please specify either --install, --remove, or --list./); }); describe('quiet option', function () { @@ -293,7 +318,7 @@ describe('kibana cli', function () { describe('remove option', function () { it('should set settings.action property to "remove"', function () { - options.install = null; + delete options.install; options.remove = 'package'; parser = settingParser(options); @@ -303,7 +328,7 @@ describe('kibana cli', function () { }); it('should allow one part to the remove parameter', function () { - options.install = null; + delete options.install; options.remove = 'test-plugin'; parser = settingParser(options); @@ -312,8 +337,8 @@ describe('kibana cli', function () { expect(settings).to.have.property('package', 'test-plugin'); }); - it('should not allow more than one part to the install parameter', function () { - options.install = null; + it('should not allow more than one part to the remove parameter', function () { + delete options.install; options.remove = 'kibana/test-plugin'; parser = settingParser(options); @@ -322,7 +347,7 @@ describe('kibana cli', function () { }); it('should populate the pluginPath', function () { - options.install = null; + delete options.install; options.remove = 'test-plugin'; parser = settingParser(options); @@ -334,6 +359,21 @@ describe('kibana cli', function () { }); + describe('list option', function () { + + it('should set settings.action property to "list"', function () { + delete options.install; + delete options.remove; + options.list = true; + parser = settingParser(options); + + var settings = parser.parse(); + + expect(settings).to.have.property('action', 'list'); + }); + + }); + }); }); diff --git a/src/cli/plugin/plugin.js b/src/cli/plugin/plugin.js index e9898f21b4250..7c2bce16a773b 100644 --- a/src/cli/plugin/plugin.js +++ b/src/cli/plugin/plugin.js @@ -3,6 +3,7 @@ const fromRoot = utils('fromRoot'); const settingParser = require('./setting_parser'); const installer = require('./plugin_installer'); const remover = require('./plugin_remover'); +const lister = require('./plugin_lister'); const pluginLogger = require('./plugin_logger'); export default function pluginCli(program) { @@ -18,11 +19,16 @@ export default function pluginCli(program) { const logger = pluginLogger(settings); - if (settings.action === 'install') { - installer.install(settings, logger); - } - if (settings.action === 'remove') { - remover.remove(settings, logger); + switch (settings.action) { + case 'install': + installer.install(settings, logger); + break; + case 'remove': + remover.remove(settings, logger); + break; + case 'list': + lister.list(settings, logger); + break; } } @@ -30,6 +36,7 @@ export default function pluginCli(program) { .command('plugin') .option('-i, --install //', 'The plugin to install') .option('-r, --remove ', 'The plugin to remove') + .option('-l, --list', 'List installed plugins') .option('-q, --quiet', 'Disable all process messaging except errors') .option('-s, --silent', 'Disable all process messaging') .option('-u, --url ', 'Specify download url') diff --git a/src/cli/plugin/plugin_lister.js b/src/cli/plugin/plugin_lister.js new file mode 100644 index 0000000000000..8351c3a96a0b0 --- /dev/null +++ b/src/cli/plugin/plugin_lister.js @@ -0,0 +1,8 @@ +const fs = require('fs'); + +export function list(settings, logger) { + fs.readdirSync(settings.pluginDir) + .forEach(function (pluginFile) { + logger.log(pluginFile); + }); +} diff --git a/src/cli/plugin/setting_parser.js b/src/cli/plugin/setting_parser.js index 783f0364f862d..b18b894b9fc43 100644 --- a/src/cli/plugin/setting_parser.js +++ b/src/cli/plugin/setting_parser.js @@ -1,5 +1,6 @@ const { resolve } = require('path'); const expiry = require('expiry-js'); +import { intersection } from 'lodash'; export default function createSettingParser(options) { function parseMilliseconds(val) { @@ -22,6 +23,10 @@ export default function createSettingParser(options) { return 'https://download.elastic.co/' + settings.organization + '/' + settings.package + '/' + filename; } + function areMultipleOptionsChosen(options, choices) { + return intersection(Object.keys(options), choices).length > 1; + } + function parse() { let parts; let settings = { @@ -84,8 +89,12 @@ export default function createSettingParser(options) { settings.package = parts.shift(); } - if (!settings.action || (options.install && options.remove)) { - throw new Error('Please specify either --install or --remove.'); + if (options.list) { + settings.action = 'list'; + } + + if (!settings.action || areMultipleOptionsChosen(options, [ 'install', 'remove', 'list' ])) { + throw new Error('Please specify either --install, --remove, or --list.'); } settings.pluginDir = options.pluginDir;