Skip to content

Commit

Permalink
Merge pull request #5920 from ycombinator/gh-5916
Browse files Browse the repository at this point in the history
Adds ability to list installed plugins from CLI
  • Loading branch information
epixa committed Jan 16, 2016
2 parents 83aa037 + 13c0ccc commit a319979
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
56 changes: 48 additions & 8 deletions src/cli/plugin/__tests__/setting_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,44 @@ 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 () {
options.remove = 'package';
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 () {
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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');
});

});

});

});
Expand Down
17 changes: 12 additions & 5 deletions src/cli/plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -18,18 +19,24 @@ 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;
}
}

program
.command('plugin')
.option('-i, --install <org>/<plugin>/<version>', 'The plugin to install')
.option('-r, --remove <plugin>', '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 <url>', 'Specify download url')
Expand Down
8 changes: 8 additions & 0 deletions src/cli/plugin/plugin_lister.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const fs = require('fs');

export function list(settings, logger) {
fs.readdirSync(settings.pluginDir)
.forEach(function (pluginFile) {
logger.log(pluginFile);
});
}
13 changes: 11 additions & 2 deletions src/cli/plugin/setting_parser.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a319979

Please sign in to comment.