Expose some BaseCommand's methods to support generic SSH commands

Exposing getName() to the derived SSH command implementation class allows us to
implement generic SSH commands.

For example Gerrit Shell plugin can now bind multiple commands to the same
class, which retrieves the shell command passed and executes it:

  public class SshShellModule extends PluginCommandModule {
    @Override
    protected void configureCommands() {
      command("ls").to(ShellCommand.class);
      command("ps").to(ShellCommand.class);
      [...]
    }
  }

With the possible implementation:

  public class ShellCommand extends SshCommand {
    @Override
    protected void run() throws UnloggedFailure {
      String cmd = getName().substring(getPluginName().length() + 1);
      ProcessBuilder proc = new ProcessBuilder(cmd);
      Process cmd = proc.start();
      [...]
    }
  }

And the call:

  davido@wizball:>ssh gerrit shell ls
  davido@wizball:>ssh gerrit shell ps

Change-Id: I43be66a99340e839b70c07e93752278cee5f039d
This commit is contained in:
David Ostrovsky
2013-10-13 14:19:13 +02:00
committed by Shawn Pearce
parent 1a2d39fd17
commit e3172b3496
2 changed files with 41 additions and 3 deletions

View File

@@ -403,6 +403,44 @@ by PrintHello class will be available to users as:
$ ssh -p 29418 review.example.com helloworld print
----
Multiple SSH commands can be bound to the same implementation class. For
example a Gerrit Shell plugin can bind different shell commands to the same
implementation class:
[source,java]
----
public class SshShellModule extends PluginCommandModule {
@Override
protected void configureCommands() {
command("ls").to(ShellCommand.class);
command("ps").to(ShellCommand.class);
[...]
}
}
----
With the possible implementation:
[source,java]
----
public class ShellCommand extends SshCommand {
@Override
protected void run() throws UnloggedFailure {
String cmd = getName().substring(getPluginName().length() + 1);
ProcessBuilder proc = new ProcessBuilder(cmd);
Process cmd = proc.start();
[...]
}
}
----
And the call:
----
$ ssh -p 29418 review.example.com shell ls
$ ssh -p 29418 review.example.com shell ps
----
[[configuration]]
Configuration
-------------

View File

@@ -127,11 +127,11 @@ public abstract class BaseCommand implements Command {
}
@Nullable
String getPluginName() {
protected String getPluginName() {
return pluginName;
}
String getName() {
protected String getName() {
return commandName;
}
@@ -139,7 +139,7 @@ public abstract class BaseCommand implements Command {
this.commandName = prefix;
}
String[] getArguments() {
public String[] getArguments() {
return argv;
}