diff --git a/cli/src/main/java/hudson/cli/CLI.java b/cli/src/main/java/hudson/cli/CLI.java index 3a6d114df4..4ec57cb99f 100644 --- a/cli/src/main/java/hudson/cli/CLI.java +++ b/cli/src/main/java/hudson/cli/CLI.java @@ -25,6 +25,7 @@ package hudson.cli; import hudson.remoting.Channel; import hudson.remoting.RemoteOutputStream; +import hudson.remoting.RemoteInputStream; import java.net.URL; import java.util.concurrent.ExecutorService; @@ -52,7 +53,8 @@ public class CLI { int r=-1; try { CliEntryPoint cli = (CliEntryPoint)channel.getRemoteProperty(CliEntryPoint.class.getName()); - r = cli.main(args, new RemoteOutputStream(System.out), new RemoteOutputStream(System.err)); + r = cli.main(args, new RemoteInputStream(System.in), + new RemoteOutputStream(System.out), new RemoteOutputStream(System.err)); } finally { channel.close(); pool.shutdown(); diff --git a/cli/src/main/java/hudson/cli/CliEntryPoint.java b/cli/src/main/java/hudson/cli/CliEntryPoint.java index c039cc8903..3d7b54225f 100644 --- a/cli/src/main/java/hudson/cli/CliEntryPoint.java +++ b/cli/src/main/java/hudson/cli/CliEntryPoint.java @@ -24,6 +24,7 @@ package hudson.cli; import java.io.OutputStream; +import java.io.InputStream; import java.util.List; /** @@ -35,5 +36,5 @@ public interface CliEntryPoint { /** * Just like the static main method. */ - int main(List args, OutputStream stdout, OutputStream stderr); + int main(List args, InputStream stdin, OutputStream stdout, OutputStream stderr); } diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index 3562a6c0df..dca2df8121 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -33,22 +33,42 @@ import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.CmdLineException; import java.io.PrintStream; +import java.io.InputStream; +import java.io.BufferedInputStream; import java.util.List; /** * Base class for Hudson CLI. * - *

How to implement a CLI command

+ *

How does a CLI command work

*

- * CLI commands are defined on the server. + * The users starts {@linkplain CLI the "CLI agent"} on a remote system, by specifying arguments, like + * "java -jar hudson-cli.jar command arg1 arg2 arg3". The CLI agent creates + * a remoting channel with the server, and it sends the entire arguments to the server, along with + * the remoted stdin/out/err. * *

+ * The Hudson master then picks the right {@link CLICommand} to execute, clone it, and + * calls {@link #main(List, InputStream, PrintStream, PrintStream)} method. + * + *

Note for CLI command implementor

+ * * * @author Kohsuke Kawaguchi * @since 1.302 @@ -59,11 +79,20 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { * IOW, if you write to these streams, the person who launched the CLI command * will see the messages in his terminal. * + *

* (In contrast, calling {@code System.out.println(...)} would print out * the message to the server log file, which is probably not what you want. */ protected PrintStream stdout,stderr; + /** + * Connected to stdin of the CLI agent. + * + *

+ * This input stream is buffered to hide the latency in the remoting. + */ + protected InputStream stdin; + /** * {@link Channel} that represents the CLI JVM. You can use this to * execute {@link Callable} on the CLI JVM, among other things. @@ -92,7 +121,8 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { return name.replaceAll("([a-z0-9])([A-Z])","$1-$2").toLowerCase(); } - public int main(List args, PrintStream stdout, PrintStream stderr) { + public int main(List args, InputStream stdin, PrintStream stdout, PrintStream stderr) { + this.stdin = new BufferedInputStream(stdin); this.stdout = stdout; this.stderr = stderr; this.channel = Channel.current(); diff --git a/core/src/main/java/hudson/cli/GroovyshCommand.java b/core/src/main/java/hudson/cli/GroovyshCommand.java new file mode 100644 index 0000000000..68292c971b --- /dev/null +++ b/core/src/main/java/hudson/cli/GroovyshCommand.java @@ -0,0 +1,35 @@ +package hudson.cli; + +import hudson.Extension; +import org.codehaus.groovy.tools.shell.Groovysh; +import org.codehaus.groovy.tools.shell.IO; + +import java.util.List; +import java.io.PrintStream; +import java.io.InputStream; +import java.io.BufferedInputStream; + +import jline.UnsupportedTerminal; +import jline.Terminal; + +/** + * Executes Groovy shell. + * + * @author Kohsuke Kawaguchi + */ +@Extension +public class GroovyshCommand extends CLICommand { + @Override + public int main(List args, InputStream stdin, PrintStream stdout, PrintStream stderr) { + // this being remote means no jline capability is available + System.setProperty("jline.terminal", UnsupportedTerminal.class.getName()); + Terminal.resetTerminal(); + + Groovysh shell = new Groovysh(new IO(new BufferedInputStream(stdin),stdout,stderr)); + return shell.run(args.toArray(new String[args.size()])); + } + + protected int run() { + throw new UnsupportedOperationException(); + } +} diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index 1ae6071fa3..5b1701a3fb 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -2660,14 +2660,14 @@ public final class Hudson extends Node implements ItemGroup, Stapl * {@link CliEntryPoint} implementation exposed to the remote CLI. */ private final class CliManager implements CliEntryPoint, Serializable { - public int main(List args, OutputStream stdout, OutputStream stderr) { + public int main(List args, InputStream stdin, OutputStream stdout, OutputStream stderr) { PrintStream err = new PrintStream(stderr); String subCmd = args.get(0); CLICommand cmd = CLICommand.clone(subCmd); if(cmd!=null) return cmd.main(args.subList(1,args.size()), - new PrintStream(stdout), err); + stdin, new PrintStream(stdout), err); err.println("No such command: "+subCmd); for (CLICommand c : CLICommand.all())