SSH: Inherit some commands from SshCommand

Some SSH command classes were unnecessary inherited from BaseCommand
and were doing stdout/stderr initialization on their own. That's what
SshCommand class is for.

Change-Id: Ia31f1dedbc46b0e19019c905d6b3fad1d87ba7a0
This commit is contained in:
David Ostrovsky
2014-08-06 09:10:31 +02:00
parent 7c51481994
commit 54525966c3
7 changed files with 59 additions and 124 deletions

View File

@@ -29,11 +29,7 @@ import com.google.inject.Inject;
import org.kohsuke.args4j.Option;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@@ -66,27 +62,12 @@ public class ListPlugins implements RestReadView<TopLevelResource> {
}
@Override
public Object apply(TopLevelResource resource)
throws UnsupportedEncodingException {
public Object apply(TopLevelResource resource) {
format = OutputFormat.JSON;
return display(null);
}
public JsonElement display(OutputStream displayOutputStream)
throws UnsupportedEncodingException {
PrintWriter stdout = null;
if (displayOutputStream != null) {
try {
stdout = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(displayOutputStream, "UTF-8")));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("JVM lacks UTF-8 encoding", e);
}
} else if (!format.isJson()) {
throw new IllegalStateException(
"Text output requires that a display OutputStream is provided.");
}
public JsonElement display(PrintWriter stdout) {
Map<String, PluginInfo> output = Maps.newTreeMap();
List<Plugin> plugins = Lists.newArrayList(pluginLoader.getPlugins(all));
Collections.sort(plugins, new Comparator<Plugin>() {

View File

@@ -24,16 +24,13 @@ import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.git.GarbageCollection;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import org.apache.sshd.server.Environment;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@@ -41,7 +38,7 @@ import java.util.List;
@RequiresCapability(GlobalCapability.RUN_GC)
@CommandMetaData(name = "gc", description = "Run Git garbage collection",
runsAt = MASTER_OR_SLAVE)
public class GarbageCollectionCommand extends BaseCommand {
public class GarbageCollectionCommand extends SshCommand {
@Option(name = "--all", usage = "runs the Git garbage collection for all projects")
private boolean all;
@@ -59,23 +56,10 @@ public class GarbageCollectionCommand extends BaseCommand {
@Inject
private GarbageCollection.Factory garbageCollectionFactory;
private PrintWriter stdout;
@Override
public void start(Environment env) throws IOException {
startThread(new CommandRunnable() {
@Override
public void run() throws Exception {
stdout = toPrintWriter(out);
try {
parseCommandLine();
verifyCommandLine();
runGC();
} finally {
stdout.flush();
}
}
});
public void run() throws Exception {
verifyCommandLine();
runGC();
}
private void verifyCommandLine() throws UnloggedFailure {

View File

@@ -28,43 +28,36 @@ import com.google.gerrit.server.group.GroupJson;
import com.google.gerrit.server.group.GroupJson.GroupInfo;
import com.google.gerrit.server.group.ListGroups;
import com.google.gerrit.server.ioutil.ColumnFormatter;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import org.apache.sshd.server.Environment;
import org.kohsuke.args4j.Option;
import java.io.PrintWriter;
@CommandMetaData(name = "ls-groups", description = "List groups visible to the caller",
runsAt = MASTER_OR_SLAVE)
public class ListGroupsCommand extends BaseCommand {
public class ListGroupsCommand extends SshCommand {
@Inject
private MyListGroups impl;
@Override
public void start(final Environment env) {
startThread(new CommandRunnable() {
@Override
public void run() throws Exception {
parseCommandLine(impl);
if (impl.getUser() != null && !impl.getProjects().isEmpty()) {
throw new UnloggedFailure(1, "fatal: --user and --project options are not compatible.");
}
final PrintWriter stdout = toPrintWriter(out);
try {
impl.display(stdout);
} finally {
stdout.flush();
}
}
});
public void run() throws Exception {
if (impl.getUser() != null && !impl.getProjects().isEmpty()) {
throw new UnloggedFailure(1, "fatal: --user and --project options are not compatible.");
}
impl.display(stdout);
}
private static class MyListGroups extends ListGroups {
@Override
protected void parseCommandLine() throws UnloggedFailure {
parseCommandLine(impl);
}
private static class MyListGroups extends ListGroups {
@Option(name = "--verbose", aliases = {"-v"},
usage = "verbose output format with tab-separated columns for the " +
"group name, UUID, description, owner group name, " +

View File

@@ -26,11 +26,10 @@ import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.account.GroupDetailFactory.Factory;
import com.google.gerrit.server.group.ListMembers;
import com.google.gerrit.server.ioutil.ColumnFormatter;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.gwtorm.server.OrmException;
import org.apache.sshd.server.Environment;
import org.kohsuke.args4j.Argument;
import java.io.PrintWriter;
@@ -43,24 +42,18 @@ import javax.inject.Inject;
*/
@CommandMetaData(name = "ls-members", description = "List the members of a given group",
runsAt = MASTER_OR_SLAVE)
public class ListMembersCommand extends BaseCommand {
public class ListMembersCommand extends SshCommand {
@Inject
ListMembersCommandImpl impl;
@Override
public void start(Environment env) {
startThread(new CommandRunnable() {
@Override
public void run() throws Exception {
parseCommandLine(impl);
final PrintWriter stdout = toPrintWriter(out);
try {
impl.display(stdout);
} finally {
stdout.flush();
}
}
});
public void run() throws Exception {
impl.display(stdout);
}
@Override
protected void parseCommandLine() throws UnloggedFailure {
parseCommandLine(impl);
}
private static class ListMembersCommandImpl extends ListMembers {

View File

@@ -17,37 +17,34 @@ package com.google.gerrit.sshd.commands;
import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
import com.google.gerrit.server.project.ListProjects;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import org.apache.sshd.server.Environment;
import java.util.List;
@CommandMetaData(name = "ls-projects", description = "List projects visible to the caller",
runsAt = MASTER_OR_SLAVE)
final class ListProjectsCommand extends BaseCommand {
final class ListProjectsCommand extends SshCommand {
@Inject
private ListProjects impl;
@Override
public void start(final Environment env) {
startThread(new CommandRunnable() {
@Override
public void run() throws Exception {
parseCommandLine(impl);
if (!impl.getFormat().isJson()) {
List<String> showBranch = impl.getShowBranch();
if (impl.isShowTree() && (showBranch != null) && !showBranch.isEmpty()) {
throw new UnloggedFailure(1, "fatal: --tree and --show-branch options are not compatible.");
}
if (impl.isShowTree() && impl.isShowDescription()) {
throw new UnloggedFailure(1, "fatal: --tree and --description options are not compatible.");
}
}
impl.display(out);
public void run() throws Exception {
if (!impl.getFormat().isJson()) {
List<String> showBranch = impl.getShowBranch();
if (impl.isShowTree() && (showBranch != null) && !showBranch.isEmpty()) {
throw new UnloggedFailure(1, "fatal: --tree and --show-branch options are not compatible.");
}
});
if (impl.isShowTree() && impl.isShowDescription()) {
throw new UnloggedFailure(1, "fatal: --tree and --description options are not compatible.");
}
}
impl.display(out);
}
@Override
protected void parseCommandLine() throws UnloggedFailure {
parseCommandLine(impl);
}
}

View File

@@ -19,29 +19,23 @@ import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.server.plugins.ListPlugins;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import org.apache.sshd.server.Environment;
import java.io.IOException;
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
@CommandMetaData(name = "ls", description = "List the installed plugins",
runsAt = MASTER_OR_SLAVE)
final class PluginLsCommand extends BaseCommand {
final class PluginLsCommand extends SshCommand {
@Inject
private ListPlugins impl;
@Override
public void start(Environment env) throws IOException {
startThread(new CommandRunnable() {
@Override
public void run() throws Exception {
parseCommandLine(impl);
impl.display(out);
}
});
public void run() throws Exception {
impl.display(stdout);
}
protected void parseCommandLine() throws UnloggedFailure {
parseCommandLine(impl);
}
}

View File

@@ -37,12 +37,11 @@ import com.google.gerrit.server.account.GetSshKeys.SshKeyInfo;
import com.google.gerrit.server.account.PutActive;
import com.google.gerrit.server.account.PutHttpPassword;
import com.google.gerrit.server.account.PutName;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import org.apache.sshd.server.Environment;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@@ -59,7 +58,7 @@ import java.util.List;
/** Set a user's account settings. **/
@CommandMetaData(name = "set-account", description = "Change an account's settings")
@RequiresCapability(GlobalCapability.MODIFY_ACCOUNT)
final class SetAccountCommand extends BaseCommand {
final class SetAccountCommand extends SshCommand {
@Argument(index = 0, required = true, metaVar = "USER", usage = "full name, email-address, ssh username or account id")
private Account.Id id;
@@ -128,15 +127,9 @@ final class SetAccountCommand extends BaseCommand {
private AccountResource rsrc;
@Override
public void start(final Environment env) {
startThread(new CommandRunnable() {
@Override
public void run() throws Exception {
parseCommandLine();
validate();
setAccount();
}
});
public void run() throws Exception {
validate();
setAccount();
}
private void validate() throws UnloggedFailure {