--- /dev/null
+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
*/
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;
/* 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) {
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);
+ }
}
}
}
*/
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;
/* 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" };
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 = "";
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));
}
}
}
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.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 {
*/
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;
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;
@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);
}
@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) {
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);
+ }
}
}
}