diff options
author | James Moger <james.moger@gitblit.com> | 2014-03-22 22:52:31 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-04-10 18:58:10 -0400 |
commit | d5bccb1dd65f7f519780081edd77d00514039994 (patch) | |
tree | 8bd53de4eaeac79a8a02afe42e8de83acac8ee30 /src/main/java/com/gitblit/transport | |
parent | eb55cdc3109577ac0fc299c8561dd8a03d6947b6 (diff) | |
download | gitblit-d5bccb1dd65f7f519780081edd77d00514039994.tar.gz gitblit-d5bccb1dd65f7f519780081edd77d00514039994.zip |
Refactor list commands into an abstract class
Diffstat (limited to 'src/main/java/com/gitblit/transport')
5 files changed, 147 insertions, 162 deletions
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<T> 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<T> getItems(); + + protected abstract boolean matches(T t); + + @Override + public void run() { + List<T> list = getItems(); + List<T> filtered; + if (StringUtils.isEmpty(regexFilter)) { + // no regex filter + filtered = list; + } else { + // regex filter the list + filtered = new ArrayList<T>(); + for (T t : list) { + if (matches(t)) { + filtered.add(t); + } + } + } + + if (tabbed) { + asTabbed(filtered); + } else { + asTable(filtered); + } + } + + protected abstract void asTable(List<T> list); + + protected abstract void asTabbed(List<T> 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<ProjectModel> { @Override - public void run() { + protected List<ProjectModel> getItems() { IGitblit gitblit = getContext().getGitblit(); UserModel user = getContext().getClient().getUser(); List<ProjectModel> projects = gitblit.getProjectModels(user, false); - List<ProjectModel> filtered; - if (StringUtils.isEmpty(regexFilter)) { - // no regex filter - filtered = projects; - } else { - // regex filter the list - filtered = new ArrayList<ProjectModel>(); - 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<ProjectModel> 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<ProjectModel> 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<RepositoryModel> { @Override - public void run() { + protected List<RepositoryModel> getItems() { IGitblit gitblit = getContext().getGitblit(); UserModel user = getContext().getClient().getUser(); - List<RepositoryModel> repositories = gitblit.getRepositoryModels(user); - List<RepositoryModel> filtered; - if (StringUtils.isEmpty(regexFilter)) { - // no regex filter - filtered = repositories; - } else { - // regex filter the list - filtered = new ArrayList<RepositoryModel>(); - 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<RepositoryModel> 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<RepositoryModel> 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<UserModel> { @Override - public void run() { + protected List<UserModel> getItems() { IGitblit gitblit = getContext().getGitblit(); List<UserModel> users = gitblit.getAllUsers(); - - List<UserModel> filtered; - if (StringUtils.isEmpty(regexFilter)) { - // no regex filter - filtered = users; - } else { - // regex filter the list - filtered = new ArrayList<UserModel>(); - 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<UserModel> list) { String[] headers; if (verbose) { @@ -167,22 +139,21 @@ public class UsersDispatcher extends DispatchCommand { stdout.println(); } + @Override protected void asTabbed(List<UserModel> 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); + } } } } |