From: James Moger Date: Sun, 23 Mar 2014 02:52:31 +0000 (-0400) Subject: Refactor list commands into an abstract class X-Git-Tag: v1.5.0~68^2~38 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d5bccb1dd65f7f519780081edd77d00514039994;p=gitblit.git Refactor list commands into an abstract class --- diff --git a/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java new file mode 100644 index 00000000..39534330 --- /dev/null +++ b/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java @@ -0,0 +1,74 @@ +package com.gitblit.transport.ssh.commands; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.Option; + +import com.gitblit.utils.StringUtils; + +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") + private boolean tabbed; + + @Argument(index = 0, metaVar = "REGEX", usage = "regex filter expression") + protected String regexFilter; + + private DateFormat df; + + protected abstract List getItems(); + + protected abstract boolean matches(T t); + + @Override + public void run() { + List list = getItems(); + List filtered; + if (StringUtils.isEmpty(regexFilter)) { + // no regex filter + filtered = list; + } else { + // regex filter the list + filtered = new ArrayList(); + for (T t : list) { + if (matches(t)) { + filtered.add(t); + } + } + } + + if (tabbed) { + asTabbed(filtered); + } else { + asTable(filtered); + } + } + + 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++) { + pattern.append("%s\t"); + } + pattern.setLength(pattern.length() - 1); + stdout.println(String.format(pattern.toString(), values)); + } + + protected String formatDate(Date date) { + if (df == null) { + df = new SimpleDateFormat("yyyy-MM-dd"); + } + return df.format(date); + } +} \ No newline at end of file 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 4495e518..df878c45 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/ProjectsDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/ProjectsDispatcher.java @@ -15,20 +15,14 @@ */ package com.gitblit.transport.ssh.gitblit; -import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.List; -import org.kohsuke.args4j.Argument; -import org.kohsuke.args4j.Option; -import org.parboiled.common.StringUtils; - import com.gitblit.manager.IGitblit; 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.SshCommand; +import com.gitblit.transport.ssh.commands.ListCommand; import com.gitblit.utils.FlipTable; import com.gitblit.utils.FlipTable.Borders; @@ -42,44 +36,23 @@ public class ProjectsDispatcher extends DispatchCommand { /* List projects */ @CommandMetaData(name = "list", aliases= { "ls" }, description = "List projects") - public static class ListProjects extends SshCommand { - - @Option(name = "--verbose", aliases = { "-v" }, usage = "verbose") - private boolean verbose; - - @Option(name = "--tabbed", aliases = { "-t" }, usage = "as tabbed output") - private boolean tabbed; - - @Argument(index = 0, metaVar = "REGEX", usage = "regex filter expression") - protected String regexFilter; + public static class ListProjects extends ListCommand { @Override - public void run() { + protected List getItems() { IGitblit gitblit = getContext().getGitblit(); UserModel user = getContext().getClient().getUser(); List projects = gitblit.getProjectModels(user, false); - List filtered; - if (StringUtils.isEmpty(regexFilter)) { - // no regex filter - filtered = projects; - } else { - // regex filter the list - filtered = new ArrayList(); - for (ProjectModel p : projects) { - if (p.name.matches(regexFilter)) { - filtered.add(p); - } - } - } - - if (tabbed) { - asTabbed(filtered); - } else { - asTable(filtered); - } + return projects; + } + + @Override + protected boolean matches(ProjectModel p) { + return p.name.matches(regexFilter); } + @Override protected void asTable(List list) { String[] headers; if (verbose) { @@ -90,34 +63,31 @@ public class ProjectsDispatcher extends DispatchCommand { headers = h; } - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String[][] data = new String[list.size()][]; for (int i = 0; i < list.size(); i++) { ProjectModel p = list.get(i); if (verbose) { - data[i] = new String[] { p.name, p.description, df.format(p.lastChange), "" + p.repositories.size() }; + data[i] = new String[] { p.name, p.description, formatDate(p.lastChange), "" + p.repositories.size() }; } else { - data[i] = new String[] { p.name, df.format(p.lastChange), "" + p.repositories.size() }; + data[i] = new String[] { p.name, formatDate(p.lastChange), "" + p.repositories.size() }; } } stdout.println(FlipTable.of(headers, data, Borders.BODY_HCOLS)); } + @Override protected void asTabbed(List list) { - String pattern; if (verbose) { - pattern = "%s\t%s\t%s"; + for (ProjectModel project : list) { + outTabbed(project.name, + project.description == null ? "" : project.description, + formatDate(project.lastChange)); + } } else { - pattern = "%s"; - } - - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); - for (ProjectModel project : list) { - stdout.println(String.format(pattern, - project.name, - project.description == null ? "" : project.description, - df.format(project.lastChange))); + for (ProjectModel project : list) { + outTabbed(project.name); + } } } } 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 ab974dc0..ae39500d 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/RepositoriesDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/RepositoriesDispatcher.java @@ -15,20 +15,14 @@ */ package com.gitblit.transport.ssh.gitblit; -import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.List; -import org.kohsuke.args4j.Argument; -import org.kohsuke.args4j.Option; -import org.parboiled.common.StringUtils; - import com.gitblit.manager.IGitblit; 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.SshCommand; +import com.gitblit.transport.ssh.commands.ListCommand; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.FlipTable; import com.gitblit.utils.FlipTable.Borders; @@ -44,46 +38,23 @@ public class RepositoriesDispatcher extends DispatchCommand { /* List repositories */ @CommandMetaData(name = "list", aliases = { "ls" }, description = "List repositories") - public static class ListRepositories extends SshCommand { - - @Option(name = "--verbose", aliases = { "-v" }, usage = "verbose") - private boolean verbose; - - @Option(name = "--tabbed", aliases = { "-t" }, usage = "as tabbed output") - private boolean tabbed; - - @Argument(index = 0, metaVar = "REGEX", usage = "regex filter expression") - protected String regexFilter; + public static class ListRepositories extends ListCommand { @Override - public void run() { + protected List getItems() { IGitblit gitblit = getContext().getGitblit(); UserModel user = getContext().getClient().getUser(); - List repositories = gitblit.getRepositoryModels(user); - List filtered; - if (StringUtils.isEmpty(regexFilter)) { - // no regex filter - filtered = repositories; - } else { - // regex filter the list - filtered = new ArrayList(); - for (RepositoryModel r : repositories) { - if (r.name.matches(regexFilter)) { - filtered.add(r); - } - } - } - - if (tabbed) { - asTabbed(filtered); - } else { - asTable(filtered); - } + return repositories; } - + + @Override + protected boolean matches(RepositoryModel r) { + return r.name.matches(regexFilter); + } + + @Override protected void asTable(List list) { - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String[] headers; if (verbose) { String[] h = { "Name", "Description", "Owners", "Last Modified", "Size" }; @@ -97,7 +68,7 @@ public class RepositoriesDispatcher extends DispatchCommand { for (int i = 0; i < list.size(); i++) { RepositoryModel r = list.get(i); - String lm = df.format(r.lastChange); + String lm = formatDate(r.lastChange); String size = r.size; if (!r.hasCommits) { lm = ""; @@ -116,29 +87,28 @@ public class RepositoriesDispatcher extends DispatchCommand { stdout.println(FlipTable.of(headers, data, Borders.BODY_HCOLS)); } + @Override protected void asTabbed(List list) { - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); - String pattern; if (verbose) { - pattern = "%s\t%s\t%s\t%s\t%s"; - } else { - pattern = "%s"; - } + for (RepositoryModel r : list) { + String lm = formatDate(r.lastChange); + String owners = ""; + if (!ArrayUtils.isEmpty(r.owners)) { + owners = Joiner.on(",").join(r.owners); + } + String size = r.size; + if (!r.hasCommits) { + lm = ""; + size = "(empty)"; + } - for (RepositoryModel r : list) { - String lm = df.format(r.lastChange); - String owners = ""; - if (!ArrayUtils.isEmpty(r.owners)) { - owners = Joiner.on(",").join(r.owners); + outTabbed(r.name, r.description == null ? "" : r.description, + owners, lm, size); } - String size = r.size; - if (!r.hasCommits) { - lm = ""; - size = "(empty)"; + } else { + for (RepositoryModel r : list) { + outTabbed(r.name); } - - stdout.println(String.format(pattern, r.name, r.description == null ? "" : r.description, - owners, lm, size)); } } } diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/TicketsDispatcher.java b/src/main/java/com/gitblit/transport/ssh/gitblit/TicketsDispatcher.java index de8d3baf..b08fdfb3 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/TicketsDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/TicketsDispatcher.java @@ -20,7 +20,6 @@ import java.util.List; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Option; -import org.parboiled.common.StringUtils; import com.gitblit.manager.IGitblit; import com.gitblit.models.RepositoryModel; @@ -35,6 +34,7 @@ import com.gitblit.transport.ssh.commands.DispatchCommand; import com.gitblit.transport.ssh.commands.SshCommand; import com.gitblit.utils.FlipTable; import com.gitblit.utils.FlipTable.Borders; +import com.gitblit.utils.StringUtils; @CommandMetaData(name = "tickets", description = "Ticket commands", hidden = true) public class TicketsDispatcher extends DispatchCommand { 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 a078138a..a0ca08fc 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/UsersDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/UsersDispatcher.java @@ -15,12 +15,9 @@ */ package com.gitblit.transport.ssh.gitblit; -import java.util.ArrayList; import java.util.List; import org.kohsuke.args4j.Argument; -import org.kohsuke.args4j.Option; -import org.parboiled.common.StringUtils; import com.gitblit.manager.IGitblit; import com.gitblit.models.RegistrantAccessPermission; @@ -28,6 +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.ListCommand; import com.gitblit.transport.ssh.commands.SshCommand; import com.gitblit.utils.FlipTable; import com.gitblit.utils.FlipTable.Borders; @@ -35,10 +33,6 @@ import com.gitblit.utils.FlipTable.Borders; @CommandMetaData(name = "users", description = "User management commands", admin = true) public class UsersDispatcher extends DispatchCommand { - private static final String banner1 = "==========================================================="; - - private static final String banner2 = "-----------------------------------------------------------"; - @Override protected void setup(UserModel user) { register(user, ShowUser.class); @@ -102,43 +96,21 @@ public class UsersDispatcher extends DispatchCommand { } @CommandMetaData(name = "list", aliases= { "ls" }, description = "List users") - public static class ListUsers extends SshCommand { - - @Option(name = "--verbose", aliases = { "-v" }, usage = "verbose") - private boolean verbose; - - @Option(name = "--tabbed", aliases = { "-t" }, usage = "as tabbed output") - private boolean tabbed; - - @Argument(index = 0, metaVar = "REGEX", usage = "regex filter expression") - protected String regexFilter; + public static class ListUsers extends ListCommand { @Override - public void run() { + protected List getItems() { IGitblit gitblit = getContext().getGitblit(); List users = gitblit.getAllUsers(); - - List filtered; - if (StringUtils.isEmpty(regexFilter)) { - // no regex filter - filtered = users; - } else { - // regex filter the list - filtered = new ArrayList(); - for (UserModel u : users) { - if (u.username.matches(regexFilter)) { - filtered.add(u); - } - } - } - - if (tabbed) { - asTabbed(filtered); - } else { - asTable(filtered); - } + return users; + } + + @Override + protected boolean matches(UserModel u) { + return u.username.matches(regexFilter); } + @Override protected void asTable(List list) { String[] headers; if (verbose) { @@ -167,22 +139,21 @@ public class UsersDispatcher extends DispatchCommand { stdout.println(); } + @Override protected void asTabbed(List users) { - String pattern; if (verbose) { - pattern = "%s\ts\t%s\t%s\t%s\t%s"; + for (UserModel u : users) { + outTabbed(u.disabled ? "-" : ((u.canAdmin() ? "*" : " ")) + u.username, + u.getDisplayName(), + u.accountType, + u.emailAddress == null ? "" : u.emailAddress, + u.canCreate() ? "Y":"", + u.canFork() ? "Y" : ""); + } } else { - pattern = "%s"; - } - - for (UserModel u : users) { - stdout.println(String.format(pattern, - u.disabled ? "-" : ((u.canAdmin() ? "*" : " ")) + u.username, - u.getDisplayName(), - u.accountType, - u.emailAddress == null ? "" : u.emailAddress, - u.canCreate() ? "Y":"", - u.canFork() ? "Y" : "")); + for (UserModel u : users) { + outTabbed(u.disabled ? "-" : ((u.canAdmin() ? "*" : " ")) + u.username); + } } } }