James Moger
2014-04-14 d3b1e34c7428fbe4ef3abeee9eaef0f885c3f19b
src/main/java/com/gitblit/transport/ssh/commands/PluginDispatcher.java
@@ -15,6 +15,7 @@
 */
package com.gitblit.transport.ssh.commands;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
@@ -56,6 +57,7 @@
      register(user, RefreshPlugins.class);
      register(user, AvailablePlugins.class);
      register(user, InstallPlugin.class);
      register(user, UpgradePlugin.class);
      register(user, UninstallPlugin.class);
   }
@@ -73,7 +75,7 @@
      protected void asTable(List<PluginWrapper> list) {
         String[] headers;
         if (verbose) {
            String [] h = { "#", "Id", "Version", "State", "Path", "Provider"};
            String [] h = { "#", "Id", "Description", "Version", "Requires", "State", "Path", "Provider"};
            headers = h;
         } else {
            String [] h = { "#", "Id", "Version", "State", "Path"};
@@ -84,7 +86,7 @@
            PluginWrapper p = list.get(i);
            PluginDescriptor d = p.getDescriptor();
            if (verbose) {
               data[i] = new Object[] { "" + (i + 1), d.getPluginId(), d.getVersion(), p.getPluginState(), p.getPluginPath(), d.getProvider() };
               data[i] = new Object[] { "" + (i + 1), d.getPluginId(), null/*d.getDescription()*/, d.getVersion(), null/*d.getRequires()*/, p.getPluginState(), p.getPluginPath(), d.getProvider() };
            } else {
               data[i] = new Object[] { "" + (i + 1), d.getPluginId(), d.getVersion(), p.getPluginState(), p.getPluginPath() };
            }
@@ -98,7 +100,7 @@
         for (PluginWrapper pw : list) {
            PluginDescriptor d = pw.getDescriptor();
            if (verbose) {
               outTabbed(d.getPluginId(), d.getVersion(), pw.getPluginState(), pw.getPluginPath(), d.getProvider());
               outTabbed(d.getPluginId(), null/*d.getDescription()*/, d.getVersion(), null/*d.getRequires()*/, pw.getPluginState(), pw.getPluginPath(), d.getProvider());
            } else {
               outTabbed(d.getPluginId(), d.getVersion(), pw.getPluginState(), pw.getPluginPath());
            }
@@ -136,7 +138,7 @@
   @CommandMetaData(name = "start", description = "Start a plugin")
   public static class StartPlugin extends PluginCommand {
      @Argument(index = 0, required = true, metaVar = "ALL|<id>", usage = "the plugin to start")
      @Argument(index = 0, required = true, metaVar = "ALL|<ID>|<INDEX>", usage = "the plugin to start")
      protected String id;
      @Override
@@ -164,7 +166,7 @@
   @CommandMetaData(name = "stop", description = "Stop a plugin")
   public static class StopPlugin extends PluginCommand {
      @Argument(index = 0, required = true, metaVar = "ALL|<id>", usage = "the plugin to stop")
      @Argument(index = 0, required = true, metaVar = "ALL|<ID>|<INDEX>", usage = "the plugin to stop")
      protected String id;
      @Override
@@ -192,7 +194,7 @@
   @CommandMetaData(name = "enable", description = "Enable a plugin")
   public static class EnablePlugin extends PluginCommand {
      @Argument(index = 0, required = true, metaVar = "<id>", usage = "the plugin id to enable")
      @Argument(index = 0, required = true, metaVar = "<ID>|<INDEX>", usage = "the plugin to enable")
      protected String id;
      @Override
@@ -214,7 +216,7 @@
   @CommandMetaData(name = "disable", description = "Disable a plugin")
   public static class DisablePlugin extends PluginCommand {
      @Argument(index = 0, required = true, metaVar = "<id>", usage = "the plugin to disable")
      @Argument(index = 0, required = true, metaVar = "<ID>|<INDEX>", usage = "the plugin to disable")
      protected String id;
      @Override
@@ -236,7 +238,7 @@
   @CommandMetaData(name = "show", description = "Show the details of a plugin")
   public static class ShowPlugin extends PluginCommand {
      @Argument(index = 0, required = true, metaVar = "<id>", usage = "the plugin to show")
      @Argument(index = 0, required = true, metaVar = "<ID>|<INDEX>", usage = "the plugin to show")
      protected String id;
      @Override
@@ -256,8 +258,9 @@
      protected String buildFieldTable(PluginWrapper pw, PluginRegistration reg) {
         final String id = pw == null ? reg.id : pw.getPluginId();
         final String name = reg == null ? "" : reg.name;
         final String version = pw == null ? "" : pw.getDescriptor().getVersion().toString();
         final String description = reg == null ? ""/*pw.getDescriptor().getDescription()*/ : reg.description;
         final String version = pw == null ? reg.getCurrentRelease().version : pw.getDescriptor().getVersion().toString();
         final String requires = pw == null ? reg.getCurrentRelease().requires : ""/*pw.getDescriptor().getRequires().toString()*/;
         final String provider = pw == null ? reg.provider : pw.getDescriptor().getProvider();
         final String registry = reg == null ? "" : reg.registry;
         final String path = pw == null ? "" : pw.getPluginPath();
@@ -277,10 +280,11 @@
         StringBuilder sb = new StringBuilder();
         sb.append("ID          : ").append(id).append('\n');
         sb.append("Version     : ").append(version).append('\n');
         sb.append("Requires    : ").append(requires).append('\n');
         sb.append("State       : ").append(state).append('\n');
         sb.append("Path        : ").append(path).append('\n');
         sb.append('\n');
         sb.append("Name        : ").append(name).append('\n');
         sb.append("Description : ").append(description).append('\n');
         sb.append("Provider    : ").append(provider).append('\n');
         sb.append("Project URL : ").append(projectUrl).append('\n');
         sb.append("Registry    : ").append(registry).append('\n');
@@ -410,10 +414,14 @@
   @CommandMetaData(name = "refresh", description = "Refresh the plugin registry data")
   public static class RefreshPlugins extends SshCommand {
      @Option(name = "--noverify", usage = "Disable checksum verification")
      private boolean disableChecksum;
      @Override
      public void run() throws Failure {
         IGitblit gitblit = getContext().getGitblit();
         gitblit.refreshRegistry();
         gitblit.refreshRegistry(!disableChecksum);
      }
   }
@@ -426,16 +434,19 @@
      @Option(name = "--updates", aliases = { "-u" }, usage = "show available updates")
      protected boolean updates;
      @Option(name = "--noverify", usage = "Disable checksum verification")
      private boolean disableChecksum;
      @Override
      protected List<PluginRegistration> getItems() throws UnloggedFailure {
         IGitblit gitblit = getContext().getGitblit();
         if (refresh) {
            gitblit.refreshRegistry();
            gitblit.refreshRegistry(!disableChecksum);
         }
         List<PluginRegistration> list;
         if (updates) {
            list = gitblit.getRegisteredPlugins(InstallState.CAN_UPDATE);
            list = gitblit.getRegisteredPlugins(InstallState.UPDATE_AVAILABLE);
         } else {
            list = gitblit.getRegisteredPlugins();
         }
@@ -444,17 +455,17 @@
      @Override
      protected boolean matches(String filter, PluginRegistration t) {
         return t.id.matches(filter) || t.name.matches(filter);
         return t.id.matches(filter) || (t.description != null && t.description.matches(filter));
      }
      @Override
      protected void asTable(List<PluginRegistration> list) {
         String[] headers;
         if (verbose) {
            String [] h = { "Id", "Name", "Description", "Installed", "Current", "Requires", "State", "Registry" };
            String [] h = { "Id", "Description", "Installed", "Current", "Requires", "State", "Registry" };
            headers = h;
         } else {
            String [] h = { "Id", "Name", "Installed", "Current", "Requires", "State" };
            String [] h = { "Id", "Installed", "Current", "Requires", "State" };
            headers = h;
         }
         Object[][] data = new Object[list.size()][];
@@ -462,9 +473,9 @@
            PluginRegistration p = list.get(i);
            PluginRelease curr = p.getCurrentRelease();
            if (verbose) {
               data[i] = new Object[] {p.id, p.name, p.description, p.installedRelease, curr.version, curr.requires, p.getInstallState(), p.registry};
               data[i] = new Object[] {p.id, p.description, p.installedRelease, curr.version, curr.requires, p.getInstallState(), p.registry};
            } else {
               data[i] = new Object[] {p.id, p.name, p.installedRelease, curr.version, curr.requires, p.getInstallState()};
               data[i] = new Object[] {p.id, p.installedRelease, curr.version, curr.requires, p.getInstallState()};
            }
         }
@@ -476,9 +487,9 @@
         for (PluginRegistration p : list) {
            PluginRelease curr = p.getCurrentRelease();
            if (verbose) {
               outTabbed(p.id, p.name, p.description, p.installedRelease, curr.version, curr.requires, p.getInstallState(), p.provider, p.registry);
               outTabbed(p.id, p.description, p.installedRelease, curr.version, curr.requires, p.getInstallState(), p.provider, p.registry);
            } else {
               outTabbed(p.id, p.name, p.installedRelease, curr.version, curr.requires, p.getInstallState());
               outTabbed(p.id, p.installedRelease, curr.version, curr.requires, p.getInstallState());
            }
         }
      }
@@ -487,8 +498,8 @@
   @CommandMetaData(name = "install", description = "Download and installs a plugin")
   public static class InstallPlugin extends SshCommand {
      @Argument(index = 0, required = true, metaVar = "<URL>|<ID>|<NAME>", usage = "the id, name, or the url of the plugin to download and install")
      protected String urlOrIdOrName;
      @Argument(index = 0, required = true, metaVar = "<URL>|<ID>", usage = "the id or the url of the plugin to download and install")
      protected String urlOrId;
      @Option(name = "--version", usage = "The specific version to install")
      private String version;
@@ -500,27 +511,65 @@
      public void run() throws Failure {
         IGitblit gitblit = getContext().getGitblit();
         try {
            String ulc = urlOrIdOrName.toLowerCase();
            String ulc = urlOrId.toLowerCase();
            if (ulc.startsWith("http://") || ulc.startsWith("https://")) {
               if (gitblit.installPlugin(urlOrIdOrName, !disableChecksum)) {
                  stdout.println(String.format("Installed %s", urlOrIdOrName));
               if (gitblit.installPlugin(urlOrId, !disableChecksum)) {
                  stdout.println(String.format("Installed %s", urlOrId));
               } else {
                  new Failure(1, String.format("Failed to install %s", urlOrIdOrName));
                  new Failure(1, String.format("Failed to install %s", urlOrId));
               }
            } else {
               PluginRelease pv = gitblit.lookupRelease(urlOrIdOrName, version);
               PluginRelease pv = gitblit.lookupRelease(urlOrId, version);
               if (pv == null) {
                  throw new Failure(1,  String.format("Plugin \"%s\" is not in the registry!", urlOrIdOrName));
                  throw new Failure(1,  String.format("Plugin \"%s\" is not in the registry!", urlOrId));
               }
               if (gitblit.installPlugin(pv.url, !disableChecksum)) {
                  stdout.println(String.format("Installed %s", urlOrIdOrName));
                  stdout.println(String.format("Installed %s", urlOrId));
               } else {
                  throw new Failure(1, String.format("Failed to install %s", urlOrIdOrName));
                  throw new Failure(1, String.format("Failed to install %s", urlOrId));
               }
            }
         } catch (Exception e) {
            log.error("Failed to install " + urlOrIdOrName, e);
            throw new Failure(1, String.format("Failed to install %s", urlOrIdOrName), e);
         } catch (IOException e) {
            log.error("Failed to install " + urlOrId, e);
            throw new Failure(1, String.format("Failed to install %s", urlOrId), e);
         }
      }
   }
   @CommandMetaData(name = "upgrade", description = "Upgrade a plugin")
   public static class UpgradePlugin extends PluginCommand {
      @Argument(index = 0, required = true, metaVar = "<ID>|<INDEX>", usage = "the plugin to upgrade")
      protected String id;
      @Option(name = "--version", usage = "The specific version to install")
      private String version;
      @Option(name = "--noverify", usage = "Disable checksum verification")
      private boolean disableChecksum;
      @Override
      public void run() throws Failure {
         IGitblit gitblit = getContext().getGitblit();
         PluginWrapper pluginWrapper = getPlugin(id);
         if (pluginWrapper == null) {
            throw new UnloggedFailure("Invalid plugin specified!");
         }
         PluginRelease pv = gitblit.lookupRelease(pluginWrapper.getPluginId(), version);
         if (pv == null) {
            throw new Failure(1,  String.format("Plugin \"%s\" is not in the registry!", pluginWrapper.getPluginId()));
         }
         try {
            if (gitblit.upgradePlugin(pluginWrapper.getPluginId(), pv.url, !disableChecksum)) {
               stdout.println(String.format("Upgraded %s", pluginWrapper.getPluginId()));
            } else {
               throw new Failure(1, String.format("Failed to upgrade %s", pluginWrapper.getPluginId()));
            }
         } catch (IOException e) {
            log.error("Failed to upgrade " + pluginWrapper.getPluginId(), e);
            throw new Failure(1, String.format("Failed to upgrade %s", pluginWrapper.getPluginId()), e);
         }
      }
   }
@@ -528,7 +577,7 @@
   @CommandMetaData(name = "uninstall", aliases = { "rm", "del" }, description = "Uninstall a plugin")
   public static class UninstallPlugin extends PluginCommand {
      @Argument(index = 0, required = true, metaVar = "<id>", usage = "the plugin to uninstall")
      @Argument(index = 0, required = true, metaVar = "<ID>|<INDEX>", usage = "the plugin to uninstall")
      protected String id;
      @Override