]> source.dussan.org Git - gitblit.git/commitdiff
Refactor list commands into an abstract class
authorJames Moger <james.moger@gitblit.com>
Sun, 23 Mar 2014 02:52:31 +0000 (22:52 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 22:58:10 +0000 (18:58 -0400)
src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java [new file with mode: 0644]
src/main/java/com/gitblit/transport/ssh/gitblit/ProjectsDispatcher.java
src/main/java/com/gitblit/transport/ssh/gitblit/RepositoriesDispatcher.java
src/main/java/com/gitblit/transport/ssh/gitblit/TicketsDispatcher.java
src/main/java/com/gitblit/transport/ssh/gitblit/UsersDispatcher.java

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 (file)
index 0000000..3953433
--- /dev/null
@@ -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
index 4495e518c78ee6c08473218db1fe36cbc328547d..df878c4502d699c18b06db6e17954e818a70fb70 100644 (file)
  */
 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);
+                               }
                        }
                }
        }
index ab974dc076c03ba4172ebbb8cd551f5c7f627041..ae39500db92e21430111f6e2f3cd53a48dc22ede 100644 (file)
  */
 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));
                        }
                }
        }
index de8d3baf22a59db1f5a557b5e44acfe924d5c8ab..b08fdfb3c7b841978e80aa55d10ecd82dbec1171 100644 (file)
@@ -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 {
index a078138a7bc54c4073bc41834d2579b3e7c3222e..a0ca08fc03463df9e41cd42c11780d244eaeb912 100644 (file)
  */
 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);
+                               }
                        }
                }
        }