From 8c68c71374bc6ffec7d765c83289da09ebd59189 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 21 Mar 2014 11:47:30 -0400 Subject: [PATCH] Hide dispatcher from usage() if it has no visible commands or visible nested dispatchers --- .../ssh/commands/DispatchCommand.java | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) 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 76012a21..4629bcc5 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java @@ -109,6 +109,12 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo log.debug("registering {} dispatcher", meta.name()); try { dispatcher.registerCommands(user); + if (dispatcher.commands.isEmpty() && dispatcher.dispatchers.isEmpty()) { + // exclude because there are no commands available to the user + log.debug(MessageFormat.format("excluding dispatcher {0} for {1}", meta.name(), user.username)); + return; + } + dispatchers.put(meta.name(), dispatcher); for (String alias : meta.aliases()) { aliasToCommand.put(alias, meta.name()); @@ -188,6 +194,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo } for (Map.Entry entry : dispatchers.entrySet()) { + DispatchCommand dispatcher = entry.getValue(); map.put(entry.getKey(), entry.getValue().getClass()); } } @@ -259,10 +266,27 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo return cmd; } + private boolean hasVisibleCommands() { + boolean visible = false; + for (Class cmd : commands) { + visible |= !cmd.getAnnotation(CommandMetaData.class).hidden(); + if (visible) { + return true; + } + } + for (DispatchCommand cmd : dispatchers.values()) { + visible |= cmd.hasVisibleCommands(); + if (visible) { + return true; + } + } + return false; + } + @Override public String usage() { - Set commands = new TreeSet(); - Set dispatchers = new TreeSet(); + Set cmds = new TreeSet(); + Set dcs = new TreeSet(); Map displayNames = Maps.newHashMap(); int maxLength = -1; Map> m = getMap(); @@ -281,15 +305,18 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo maxLength = Math.max(maxLength, displayName.length()); if (DispatchCommand.class.isAssignableFrom(c)) { - dispatchers.add(name); + DispatchCommand d = dispatchers.get(name); + if (d.hasVisibleCommands()) { + dcs.add(name); + } } else { - commands.add(name); + cmds.add(name); } } String format = "%-" + maxLength + "s %s"; final StringBuilder usage = new StringBuilder(); - if (!commands.isEmpty()) { + if (!cmds.isEmpty()) { usage.append("Available commands"); if (!getName().isEmpty()) { usage.append(" of "); @@ -297,7 +324,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo } usage.append(" are:\n"); usage.append("\n"); - for (String name : commands) { + for (String name : cmds) { final Class c = m.get(name); String displayName = displayNames.get(name); CommandMetaData meta = c.getAnnotation(CommandMetaData.class); @@ -308,7 +335,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo usage.append("\n"); } - if (!dispatchers.isEmpty()) { + if (!dcs.isEmpty()) { usage.append("Available command dispatchers"); if (!getName().isEmpty()) { usage.append(" of "); @@ -316,7 +343,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo } usage.append(" are:\n"); usage.append("\n"); - for (String name : dispatchers) { + for (String name : dcs) { final Class c = m.get(name); String displayName = displayNames.get(name); CommandMetaData meta = c.getAnnotation(CommandMetaData.class); -- 2.39.5