From 1c7917fa526925c672d4e5dd336d5b93aa51e6a5 Mon Sep 17 00:00:00 2001 From: James Moger Date: Sun, 23 Mar 2014 16:29:48 -0400 Subject: [PATCH] Add --json flag to the list command for outputing JSON --- .../transport/ssh/commands/ListCommand.java | 31 +++++++++++++++---- ...gexCommand.java => ListFilterCommand.java} | 28 ++++++++++------- .../ssh/gitblit/ProjectsDispatcher.java | 8 ++--- .../ssh/gitblit/RepositoriesDispatcher.java | 8 ++--- .../ssh/gitblit/UsersDispatcher.java | 8 ++--- 5 files changed, 53 insertions(+), 30 deletions(-) rename src/main/java/com/gitblit/transport/ssh/commands/{ListRegexCommand.java => ListFilterCommand.java} (71%) diff --git a/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java index 45136262..8d4ddc3c 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java @@ -22,9 +22,11 @@ import java.util.List; import org.kohsuke.args4j.Option; +import com.gitblit.utils.JsonUtils; + /** * Parent class of a list command. - * + * * @author James Moger * * @param @@ -34,27 +36,40 @@ public abstract class ListCommand extends SshCommand { @Option(name = "--verbose", aliases = { "-v" }, usage = "verbose") protected boolean verbose; - @Option(name = "--tabbed", aliases = { "-t" }, usage = "as tabbed output") + @Option(name = "--tabbed", usage = "generate tabbed-text output") protected boolean tabbed; + @Option(name = "--json", usage = "generate JSON output") + protected boolean json; + private DateFormat df; protected abstract List getItems() throws UnloggedFailure; - + + protected void validateOutputFormat() throws UnloggedFailure { + if (tabbed && json) { + throw new UnloggedFailure(1, "Please specify --tabbed OR --json, not both!"); + } + } + @Override public void run() throws UnloggedFailure { + validateOutputFormat(); + List list = getItems(); if (tabbed) { asTabbed(list); + } else if (json) { + asJSON(list); } else { asTable(list); } } protected abstract void asTable(List list); - + protected abstract void asTabbed(List list); - + protected void outTabbed(Object... values) { StringBuilder pattern = new StringBuilder(); for (int i = 0; i < values.length; i++) { @@ -63,7 +78,11 @@ public abstract class ListCommand extends SshCommand { pattern.setLength(pattern.length() - 1); stdout.println(String.format(pattern.toString(), values)); } - + + protected void asJSON(List list) { + stdout.println(JsonUtils.toJsonString(list)); + } + protected String formatDate(Date date) { if (df == null) { df = new SimpleDateFormat("yyyy-MM-dd"); diff --git a/src/main/java/com/gitblit/transport/ssh/commands/ListRegexCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/ListFilterCommand.java similarity index 71% rename from src/main/java/com/gitblit/transport/ssh/commands/ListRegexCommand.java rename to src/main/java/com/gitblit/transport/ssh/commands/ListFilterCommand.java index 1e25775a..4cc0983e 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/ListRegexCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/ListFilterCommand.java @@ -23,31 +23,33 @@ import org.kohsuke.args4j.Argument; import com.gitblit.utils.StringUtils; /** - * List command that accepts a regex filter parameter. - * + * List command that accepts a filter parameter. + * * @author James Moger * * @param */ -public abstract class ListRegexCommand extends ListCommand { +public abstract class ListFilterCommand extends ListCommand { + + @Argument(index = 0, metaVar = "FILTER", usage = "filter expression") + private String filter; + + protected abstract boolean matches(String filter, T t); - @Argument(index = 0, metaVar = "REGEX", usage = "regex filter expression") - protected String regexFilter; - - protected abstract boolean matches(T t); - @Override public void run() throws UnloggedFailure { + validateOutputFormat(); + List list = getItems(); List filtered; - if (StringUtils.isEmpty(regexFilter)) { - // no regex filter + if (StringUtils.isEmpty(filter)) { + // no filter filtered = list; } else { - // regex filter the list + // filter the list filtered = new ArrayList(); for (T t : list) { - if (matches(t)) { + if (matches(filter, t)) { filtered.add(t); } } @@ -55,6 +57,8 @@ public abstract class ListRegexCommand extends ListCommand { if (tabbed) { asTabbed(filtered); + } else if (json) { + asJSON(filtered); } else { asTable(filtered); } diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/ProjectsDispatcher.java b/src/main/java/com/gitblit/transport/ssh/gitblit/ProjectsDispatcher.java index 1984ebcc..97076adf 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/ProjectsDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/ProjectsDispatcher.java @@ -22,7 +22,7 @@ import com.gitblit.models.ProjectModel; import com.gitblit.models.UserModel; import com.gitblit.transport.ssh.commands.CommandMetaData; import com.gitblit.transport.ssh.commands.DispatchCommand; -import com.gitblit.transport.ssh.commands.ListRegexCommand; +import com.gitblit.transport.ssh.commands.ListFilterCommand; import com.gitblit.utils.FlipTable; import com.gitblit.utils.FlipTable.Borders; @@ -36,7 +36,7 @@ public class ProjectsDispatcher extends DispatchCommand { /* List projects */ @CommandMetaData(name = "list", aliases= { "ls" }, description = "List projects") - public static class ListProjects extends ListRegexCommand { + public static class ListProjects extends ListFilterCommand { @Override protected List getItems() { @@ -48,8 +48,8 @@ public class ProjectsDispatcher extends DispatchCommand { } @Override - protected boolean matches(ProjectModel p) { - return p.name.matches(regexFilter); + protected boolean matches(String filter, ProjectModel p) { + return p.name.matches(filter); } @Override diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/RepositoriesDispatcher.java b/src/main/java/com/gitblit/transport/ssh/gitblit/RepositoriesDispatcher.java index 4798c6d7..4be60abc 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/RepositoriesDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/RepositoriesDispatcher.java @@ -22,7 +22,7 @@ import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.transport.ssh.commands.CommandMetaData; import com.gitblit.transport.ssh.commands.DispatchCommand; -import com.gitblit.transport.ssh.commands.ListRegexCommand; +import com.gitblit.transport.ssh.commands.ListFilterCommand; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.FlipTable; import com.gitblit.utils.FlipTable.Borders; @@ -38,7 +38,7 @@ public class RepositoriesDispatcher extends DispatchCommand { /* List repositories */ @CommandMetaData(name = "list", aliases = { "ls" }, description = "List repositories") - public static class ListRepositories extends ListRegexCommand { + public static class ListRepositories extends ListFilterCommand { @Override protected List getItems() { @@ -49,8 +49,8 @@ public class RepositoriesDispatcher extends DispatchCommand { } @Override - protected boolean matches(RepositoryModel r) { - return r.name.matches(regexFilter); + protected boolean matches(String filter, RepositoryModel r) { + return r.name.matches(filter); } @Override diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/UsersDispatcher.java b/src/main/java/com/gitblit/transport/ssh/gitblit/UsersDispatcher.java index c4fac3ea..bed966da 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/UsersDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/UsersDispatcher.java @@ -25,7 +25,7 @@ import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; import com.gitblit.transport.ssh.commands.CommandMetaData; import com.gitblit.transport.ssh.commands.DispatchCommand; -import com.gitblit.transport.ssh.commands.ListRegexCommand; +import com.gitblit.transport.ssh.commands.ListFilterCommand; import com.gitblit.transport.ssh.commands.SshCommand; import com.gitblit.utils.FlipTable; import com.gitblit.utils.FlipTable.Borders; @@ -96,7 +96,7 @@ public class UsersDispatcher extends DispatchCommand { } @CommandMetaData(name = "list", aliases= { "ls" }, description = "List users") - public static class ListUsers extends ListRegexCommand { + public static class ListUsers extends ListFilterCommand { @Override protected List getItems() { @@ -106,8 +106,8 @@ public class UsersDispatcher extends DispatchCommand { } @Override - protected boolean matches(UserModel u) { - return u.username.matches(regexFilter); + protected boolean matches(String filter, UserModel u) { + return u.username.matches(filter); } @Override -- 2.39.5