Add --json flag to the list command for outputing JSON
1 files renamed
4 files modified
| | |
| | | |
| | | import org.kohsuke.args4j.Option; |
| | | |
| | | import com.gitblit.utils.JsonUtils; |
| | | |
| | | /** |
| | | * Parent class of a list command. |
| | | * |
| | |
| | | @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<T> 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<T> list = getItems(); |
| | | if (tabbed) { |
| | | asTabbed(list); |
| | | } else if (json) { |
| | | asJSON(list); |
| | | } else { |
| | | asTable(list); |
| | | } |
| | |
| | | stdout.println(String.format(pattern.toString(), values)); |
| | | } |
| | | |
| | | protected void asJSON(List<T> list) { |
| | | stdout.println(JsonUtils.toJsonString(list)); |
| | | } |
| | | |
| | | protected String formatDate(Date date) { |
| | | if (df == null) { |
| | | df = new SimpleDateFormat("yyyy-MM-dd"); |
File was renamed from src/main/java/com/gitblit/transport/ssh/commands/ListRegexCommand.java |
| | |
| | | import com.gitblit.utils.StringUtils; |
| | | |
| | | /** |
| | | * List command that accepts a regex filter parameter. |
| | | * List command that accepts a filter parameter. |
| | | * |
| | | * @author James Moger |
| | | * |
| | | * @param <T> |
| | | */ |
| | | public abstract class ListRegexCommand<T> extends ListCommand<T> { |
| | | public abstract class ListFilterCommand<T> extends ListCommand<T> { |
| | | |
| | | @Argument(index = 0, metaVar = "REGEX", usage = "regex filter expression") |
| | | protected String regexFilter; |
| | | @Argument(index = 0, metaVar = "FILTER", usage = "filter expression") |
| | | private String filter; |
| | | |
| | | protected abstract boolean matches(T t); |
| | | protected abstract boolean matches(String filter, T t); |
| | | |
| | | @Override |
| | | public void run() throws UnloggedFailure { |
| | | validateOutputFormat(); |
| | | |
| | | List<T> list = getItems(); |
| | | List<T> 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<T>(); |
| | | for (T t : list) { |
| | | if (matches(t)) { |
| | | if (matches(filter, t)) { |
| | | filtered.add(t); |
| | | } |
| | | } |
| | |
| | | |
| | | if (tabbed) { |
| | | asTabbed(filtered); |
| | | } else if (json) { |
| | | asJSON(filtered); |
| | | } else { |
| | | asTable(filtered); |
| | | } |
| | |
| | | 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; |
| | | |
| | |
| | | |
| | | /* List projects */ |
| | | @CommandMetaData(name = "list", aliases= { "ls" }, description = "List projects") |
| | | public static class ListProjects extends ListRegexCommand<ProjectModel> { |
| | | public static class ListProjects extends ListFilterCommand<ProjectModel> { |
| | | |
| | | @Override |
| | | protected List<ProjectModel> getItems() { |
| | |
| | | } |
| | | |
| | | @Override |
| | | protected boolean matches(ProjectModel p) { |
| | | return p.name.matches(regexFilter); |
| | | protected boolean matches(String filter, ProjectModel p) { |
| | | return p.name.matches(filter); |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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; |
| | |
| | | |
| | | /* List repositories */ |
| | | @CommandMetaData(name = "list", aliases = { "ls" }, description = "List repositories") |
| | | public static class ListRepositories extends ListRegexCommand<RepositoryModel> { |
| | | public static class ListRepositories extends ListFilterCommand<RepositoryModel> { |
| | | |
| | | @Override |
| | | protected List<RepositoryModel> getItems() { |
| | |
| | | } |
| | | |
| | | @Override |
| | | protected boolean matches(RepositoryModel r) { |
| | | return r.name.matches(regexFilter); |
| | | protected boolean matches(String filter, RepositoryModel r) { |
| | | return r.name.matches(filter); |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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; |
| | |
| | | } |
| | | |
| | | @CommandMetaData(name = "list", aliases= { "ls" }, description = "List users") |
| | | public static class ListUsers extends ListRegexCommand<UserModel> { |
| | | public static class ListUsers extends ListFilterCommand<UserModel> { |
| | | |
| | | @Override |
| | | protected List<UserModel> getItems() { |
| | |
| | | } |
| | | |
| | | @Override |
| | | protected boolean matches(UserModel u) { |
| | | return u.username.matches(regexFilter); |
| | | protected boolean matches(String filter, UserModel u) { |
| | | return u.username.matches(filter); |
| | | } |
| | | |
| | | @Override |