]> source.dussan.org Git - gitblit.git/commitdiff
Support local aliases within a dispatcher and warn on registration collisions
authorJames Moger <james.moger@gitblit.com>
Wed, 19 Mar 2014 16:10:16 +0000 (12:10 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 22:58:09 +0000 (18:58 -0400)
src/main/java/com/gitblit/transport/ssh/commands/CommandMetaData.java
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java

index 133b9cbfe6aa4598b8407797d3e5027aa00e6f21..b3a9dd2cb4218f9a7a6a1bdc93dc4dfc1fed415f 100644 (file)
@@ -27,6 +27,7 @@ import java.lang.annotation.Target;
 @Retention(RUNTIME)
 public @interface CommandMetaData {
 String name();
+String [] aliases() default {};
 String description() default "";
 boolean admin() default false;
 boolean hidden() default false;
index 2e874e617785fa22f0a8ec3b87c6d357727031e2..f8503f897f8d4b2ee889448ca62d4dd499a84ef7 100644 (file)
@@ -36,6 +36,7 @@ import com.gitblit.models.UserModel;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.utils.cli.SubcommandHandler;
 import com.google.common.base.Charsets;
+import com.google.common.base.Joiner;
 import com.google.common.base.Strings;
 import com.google.common.collect.Maps;
 
@@ -51,12 +52,16 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
 
        private final Set<Class<? extends BaseCommand>> commands;
        private final Map<String, DispatchCommand> dispatchers;
+       private final Map<String, String> aliasToCommand;
+       private final Map<String, List<String>> commandToAliases;
        private final List<BaseCommand> instantiated;
        private Map<String, Class<? extends BaseCommand>> map;
 
        protected DispatchCommand() {
                commands = new HashSet<Class<? extends BaseCommand>>();
                dispatchers = Maps.newHashMap();
+               aliasToCommand = Maps.newHashMap();
+               commandToAliases = Maps.newHashMap();
                instantiated = new ArrayList<BaseCommand>();
        }
 
@@ -64,6 +69,8 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
        public void destroy() {
                super.destroy();
                commands.clear();
+               aliasToCommand.clear();
+               commandToAliases.clear();
                map = null;
 
                for (BaseCommand command : instantiated) {
@@ -103,6 +110,13 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
                try {
                        dispatcher.registerCommands(user);
                        dispatchers.put(meta.name(), dispatcher);
+                       for (String alias : meta.aliases()) {
+                               aliasToCommand.put(alias, meta.name());
+                               if (!commandToAliases.containsKey(meta.name())) {
+                                       commandToAliases.put(meta.name(), new ArrayList<String>());
+                               }
+                               commandToAliases.get(meta.name()).add(alias);
+                       }
                } catch (Exception e) {
                        log.error("failed to register {} dispatcher", meta.name());
                }
@@ -135,7 +149,22 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
                        map = Maps.newHashMapWithExpectedSize(commands.size());
                        for (Class<? extends BaseCommand> cmd : commands) {
                                CommandMetaData meta = cmd.getAnnotation(CommandMetaData.class);
-                               map.put(meta.name(), cmd);
+                               if (map.containsKey(meta.name()) || aliasToCommand.containsKey(meta.name())) {
+                                       log.warn("{} already contains the \"{}\" command!", getName(), meta.name());
+                               } else {
+                                       map.put(meta.name(), cmd);
+                               }
+                               for (String alias : meta.aliases()) {
+                                       if (map.containsKey(alias) || aliasToCommand.containsKey(alias)) {
+                                               log.warn("{} already contains the \"{}\" command!", getName(), alias);
+                                       } else {
+                                               aliasToCommand.put(alias, meta.name());
+                                               if (!commandToAliases.containsKey(meta.name())) {
+                                                       commandToAliases.put(meta.name(), new ArrayList<String>());
+                                               }
+                                               commandToAliases.get(meta.name()).add(alias);
+                                       }
+                               }
                        }
 
                        for (Map.Entry<String, DispatchCommand> entry : dispatchers.entrySet()) {
@@ -179,10 +208,15 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
        }
 
        private BaseCommand getCommand() throws UnloggedFailure {
-               if (dispatchers != null && dispatchers.containsKey(commandName)) {
-                       return dispatchers.get(commandName);
+               Map<String, Class<? extends BaseCommand>> map = getMap();
+               String name = commandName;
+               if (aliasToCommand.containsKey(commandName)) {
+                       name = aliasToCommand.get(name);
+               }
+               if (dispatchers.containsKey(name)) {
+                       return dispatchers.get(name);
                }
-               final Class<? extends BaseCommand> c = getMap().get(commandName);
+               final Class<? extends BaseCommand> c = map.get(name);
                if (c == null) {
                        String msg = (getName().isEmpty() ? "Gitblit" : getName()) + ": " + commandName + ": not found";
                        throw new UnloggedFailure(1, msg);
@@ -202,6 +236,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
        public String usage() {
                Set<String> commands = new TreeSet<String>();
                Set<String> dispatchers = new TreeSet<String>();
+               Map<String, String> displayNames = Maps.newHashMap();
                int maxLength = -1;
                Map<String, Class<? extends BaseCommand>> m = getMap();
                for (String name : m.keySet()) {
@@ -211,7 +246,13 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
                                continue;
                        }
 
-                       maxLength = Math.max(maxLength, name.length());
+                       String displayName = name;
+                       if (commandToAliases.containsKey(meta.name())) {
+                               displayName = name + " (" + Joiner.on(',').join(commandToAliases.get(meta.name())) + ")";
+                       }
+                       displayNames.put(name, displayName);
+
+                       maxLength = Math.max(maxLength, displayName.length());
                        if (DispatchCommand.class.isAssignableFrom(c)) {
                                dispatchers.add(name);
                        } else {
@@ -231,9 +272,10 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
                        usage.append("\n");
                        for (String name : commands) {
                                final Class<? extends Command> c = m.get(name);
+                               String displayName = displayNames.get(name);
                                CommandMetaData meta = c.getAnnotation(CommandMetaData.class);
                                usage.append("   ");
-                               usage.append(String.format(format, name, Strings.nullToEmpty(meta.description())));
+                               usage.append(String.format(format, displayName, Strings.nullToEmpty(meta.description())));
                                usage.append("\n");
                        }
                        usage.append("\n");
@@ -249,9 +291,10 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
                        usage.append("\n");
                        for (String name : dispatchers) {
                                final Class<? extends BaseCommand> c = m.get(name);
+                               String displayName = displayNames.get(name);
                                CommandMetaData meta = c.getAnnotation(CommandMetaData.class);
                                usage.append("   ");
-                               usage.append(String.format(format, name, Strings.nullToEmpty(meta.description())));
+                               usage.append(String.format(format, displayName, Strings.nullToEmpty(meta.description())));
                                usage.append("\n");
                        }
                        usage.append("\n");