From 663f653ced4cd723f610d2c0f33d7e129ef11e92 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Wed, 29 May 2013 21:50:41 +0200 Subject: [PATCH] Add more information and options in PluginManager New option -l, --list displays list of existing plugins New option -h, --help displays help Deprecate options: -install is now -i, --install -remove is now -r, --remove -url is now -u, --url Catch ArraysOutOfBoundException when no arg given to install, remove or url option Add description on plugin name structure: - elasticsearch/plugin/version for official elasticsearch plugins (download from download.elasticsearch.org) - groupId/artifactId/version for community plugins (download from maven central or oss sonatype) - username/repository for site plugins (download from github master) Closes #3112. --- .../elasticsearch/plugins/PluginManager.java | 89 +++++++++++++++---- 1 file changed, 73 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugins/PluginManager.java b/src/main/java/org/elasticsearch/plugins/PluginManager.java index 8492760cc7a..b7eae87fc4b 100644 --- a/src/main/java/org/elasticsearch/plugins/PluginManager.java +++ b/src/main/java/org/elasticsearch/plugins/PluginManager.java @@ -288,6 +288,18 @@ public class PluginManager { } } + public void listInstalledPlugins() { + File[] plugins = environment.pluginsFile().listFiles(); + System.out.println("Installed plugins:"); + if (plugins.length == 0) { + System.out.println(" - No plugin detected in " + environment.pluginsFile().getAbsolutePath()); + } + + for (int i = 0; i < plugins.length; i++) { + System.out.println(" - " + plugins[i].getName()); + } + } + public static void main(String[] args) { Tuple initialSettings = InternalSettingsPerparer.prepareSettings(EMPTY_SETTINGS, true); @@ -298,9 +310,19 @@ public class PluginManager { String url = null; boolean verbose = false; for (int i = 0; i < args.length; i++) { - if ("url".equals(args[i]) || "-url".equals(args[i])) { - url = args[i + 1]; - } else if ("verbose".equals(args[i]) || "-verbose".equals(args[i])) { + String command = args[i]; + if ("-u".equals(command) || "--url".equals(command) + // Deprecated commands + || "url".equals(command) || "-url".equals(command)) { + try { + url = args[i + 1]; + } catch (Exception e) { + displayHelp("Error while installing plugin, reason: " + e.getClass().getSimpleName() + + ": " + e.getMessage()); + System.exit(1); + } + } else if ("-v".equals(command) || "--v".equals(command) + || "verbose".equals(command) || "-verbose".equals(command)) { verbose = true; } } @@ -309,34 +331,69 @@ public class PluginManager { PluginManager pluginManager = new PluginManager(initialSettings.v2(), url); if (args.length < 1) { - System.out.println("Usage:"); - System.out.println(" -url [plugin location] : Set exact URL to download the plugin from"); - System.out.println(" -install [plugin name] : Downloads and installs listed plugins"); - System.out.println(" -remove [plugin name] : Removes listed plugins"); - System.out.println(" -verbose : Prints verbose messages"); + displayHelp(null); } for (int c = 0; c < args.length; c++) { String command = args[c]; - if (command.equals("install") || command.equals("-install")) { - String pluginName = args[++c]; - System.out.println("-> Installing " + pluginName + "..."); + if (command.equals("-i") || command.equals("--install") + // Deprecated commands + || command.equals("install") || command.equals("-install")) { + String pluginName = null; try { + pluginName = args[++c]; + System.out.println("-> Installing " + pluginName + "..."); pluginManager.downloadAndExtract(pluginName, verbose); } catch (IOException e) { System.out.println("Failed to install " + pluginName + ", reason: " + e.getMessage()); + } catch (Exception e) { + displayHelp("Error while installing plugin, reason: " + e.getClass().getSimpleName() + + ": " + e.getMessage()); + System.exit(1); } - } else if (command.equals("remove") || command.equals("-remove")) { - String pluginName = args[++c]; - System.out.println("-> Removing " + pluginName + " "); + } else if (command.equals("-r") || command.equals("--remove") + // Deprecated commands + || command.equals("remove") || command.equals("-remove")) { + String pluginName = null; try { + pluginName = args[++c]; + System.out.println("-> Removing " + pluginName + " "); pluginManager.removePlugin(pluginName); } catch (IOException e) { System.out.println("Failed to remove " + pluginName + ", reason: " + e.getMessage()); + } catch (Exception e) { + displayHelp("Error while removing plugin, reason: " + e.getClass().getSimpleName() + + ": " + e.getMessage()); } + } else if (command.equals("-l") || command.equals("--list")) { + pluginManager.listInstalledPlugins(); + } else if (command.equals("-h") || command.equals("--help")) { + displayHelp(null); } else { - // not install or remove, continue - c++; + displayHelp("Command [" + args[c] + "] unknown."); + // Unknown command. We break... + System.exit(1); } } } + + private static void displayHelp(String message) { + System.out.println("Usage:"); + System.out.println(" -u, --url [plugin location] : Set exact URL to download the plugin from"); + System.out.println(" -i, --install [plugin name] : Downloads and installs listed plugins [*]"); + System.out.println(" -r, --remove [plugin name] : Removes listed plugins"); + System.out.println(" -l, --list : List installed plugins"); + System.out.println(" -v, --verbose : Prints verbose messages"); + System.out.println(" -h, --help : Prints this help message"); + System.out.println(); + System.out.println(" [*] Plugin name could be:"); + System.out.println(" elasticsearch/plugin/version for official elasticsearch plugins (download from download.elasticsearch.org)"); + System.out.println(" groupId/artifactId/version for community plugins (download from maven central or oss sonatype)"); + System.out.println(" username/repository for site plugins (download from github master)"); + + if (message != null) { + System.out.println(); + System.out.println("Message:"); + System.out.println(" " + message); + } + } }