From 74732a3b3ac1f48b97d9f58820e0d3de7fefa045 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 19 Mar 2014 12:10:16 -0400 Subject: [PATCH] Support local aliases within a dispatcher and warn on registration collisions --- .../ssh/commands/CommandMetaData.java | 1 + .../ssh/commands/DispatchCommand.java | 57 ++++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/gitblit/transport/ssh/commands/CommandMetaData.java b/src/main/java/com/gitblit/transport/ssh/commands/CommandMetaData.java index 133b9cbf..b3a9dd2c 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/CommandMetaData.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/CommandMetaData.java @@ -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; diff --git a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java index 2e874e61..f8503f89 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java @@ -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> commands; private final Map dispatchers; + private final Map aliasToCommand; + private final Map> commandToAliases; private final List instantiated; private Map> map; protected DispatchCommand() { commands = new HashSet>(); dispatchers = Maps.newHashMap(); + aliasToCommand = Maps.newHashMap(); + commandToAliases = Maps.newHashMap(); instantiated = new ArrayList(); } @@ -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()); + } + 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 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()); + } + commandToAliases.get(meta.name()).add(alias); + } + } } for (Map.Entry 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> map = getMap(); + String name = commandName; + if (aliasToCommand.containsKey(commandName)) { + name = aliasToCommand.get(name); + } + if (dispatchers.containsKey(name)) { + return dispatchers.get(name); } - final Class c = getMap().get(commandName); + final Class 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 commands = new TreeSet(); Set dispatchers = new TreeSet(); + Map displayNames = Maps.newHashMap(); int maxLength = -1; Map> 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 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 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"); -- 2.39.5