From f76fee63ed9cb3a30d3c0c092d860b1cb93a481b Mon Sep 17 00:00:00 2001
From: Gerard Smyth <gerard.smyth@gmail.com>
Date: Thu, 08 May 2014 13:09:30 -0400
Subject: [PATCH] Updated the SyndicationServlet to provide an additional option to return details of the tags in the repository instead of the commits. This uses a new 'ot' request parameter to indicate the object type of the content to return, which can be ither TAG or COMMIT. If this is not provided, then COMMIT is assumed to maintain backwards compatability. If tags are returned, then the paging parameters, 'l' and 'pg' are still supported, but searching options are currently ignored.

---
 src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java |   84 +++++++++++++++++++++++++----------------
 1 files changed, 51 insertions(+), 33 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
index 3953433..8d4ddc3 100644
--- a/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java
+++ b/src/main/java/com/gitblit/transport/ssh/commands/ListCommand.java
@@ -1,61 +1,75 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 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;
+import com.gitblit.utils.JsonUtils;
 
+/**
+ * Parent class of a list command.
+ *
+ * @author James Moger
+ *
+ * @param <T>
+ */
 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;
+	@Option(name = "--tabbed", usage = "generate tabbed-text output")
+	protected boolean tabbed;
 
-	@Argument(index = 0, metaVar = "REGEX", usage = "regex filter expression")
-	protected String regexFilter;
-	
+	@Option(name = "--json", usage = "generate JSON output")
+	protected boolean json;
+
 	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);
-				}
-			}
-		}
+	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(filtered);
+			asTabbed(list);
+		} else if (json) {
+			asJSON(list);
 		} else {
-			asTable(filtered);
+			asTable(list);
 		}
 	}
 
 	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++) {
@@ -64,7 +78,11 @@
 		pattern.setLength(pattern.length() - 1);
 		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");

--
Gitblit v1.9.1