@Override public void start() throws Exception { Command starwars = CommandBuilder.command("top"). processHandler(process -> {
@Override public void start() throws Exception { Command helloWorld = CommandBuilder.command("hello-world"). processHandler(process -> { process.write("hello world\n"); process.end(); }).build(vertx); ShellService service = ShellService.create(vertx, new ShellServiceOptions().setTelnetOptions( new TelnetTermOptions().setHost("localhost").setPort(3000) )); CommandRegistry.getShared(vertx).registerCommand(helloWorld); service.start(ar -> { if (!ar.succeeded()) { ar.cause().printStackTrace(); } }); } }
@Override public void start() throws Exception { Command starwars = CommandBuilder.command("echokeyboard"). processHandler(process -> { // Echo process.stdinHandler(keys -> { process.write(keys.replace('\r', '\n')); }); // Terminate when user hits Ctrl-C process.interruptHandler(v -> { process.end(); }); }).build(vertx); ShellService service = ShellService.create(vertx, new ShellServiceOptions().setTelnetOptions( new TelnetTermOptions().setHost("localhost").setPort(3000) )); CommandRegistry.getShared(vertx).registerCommand(starwars); service.start(ar -> { if (!ar.succeeded()) { ar.cause().printStackTrace(); } }); } }
@Override public void start() throws Exception { Command starwars = CommandBuilder.command("starwars"). processHandler(process -> {
Command helloWorld = CommandBuilder.command(cli). processHandler(process -> { URL url;
/** * Create a new commmand builder, the command is responsible for managing the options and arguments via the * {@link io.vertx.rxjava.ext.shell.command.CommandProcess #args() arguments}. * @param name the command name * @return the command */ public static io.vertx.rxjava.ext.shell.command.CommandBuilder command(String name) { io.vertx.rxjava.ext.shell.command.CommandBuilder ret = io.vertx.rxjava.ext.shell.command.CommandBuilder.newInstance(io.vertx.ext.shell.command.CommandBuilder.command(name)); return ret; }
/** * Create a new commmand builder, the command is responsible for managing the options and arguments via the * {@link io.vertx.rxjava.ext.shell.command.CommandProcess #args() arguments}. * @param name the command name * @return the command */ public static io.vertx.rxjava.ext.shell.command.CommandBuilder command(String name) { io.vertx.rxjava.ext.shell.command.CommandBuilder ret = io.vertx.rxjava.ext.shell.command.CommandBuilder.newInstance(io.vertx.ext.shell.command.CommandBuilder.command(name)); return ret; }
/** * Create a new commmand with its {@link io.vertx.rxjava.core.cli.CLI} descriptor. This command can then retrieve the parsed * {@link io.vertx.rxjava.ext.shell.command.CommandProcess#commandLine} when it executes to know get the command arguments and options. * @param cli the cli to use * @return the command */ public static io.vertx.rxjava.ext.shell.command.CommandBuilder command(io.vertx.rxjava.core.cli.CLI cli) { io.vertx.rxjava.ext.shell.command.CommandBuilder ret = io.vertx.rxjava.ext.shell.command.CommandBuilder.newInstance(io.vertx.ext.shell.command.CommandBuilder.command(cli.getDelegate())); return ret; }
/** * Create a new commmand with its {@link io.vertx.rxjava.core.cli.CLI} descriptor. This command can then retrieve the parsed * {@link io.vertx.rxjava.ext.shell.command.CommandProcess#commandLine} when it executes to know get the command arguments and options. * @param cli the cli to use * @return the command */ public static io.vertx.rxjava.ext.shell.command.CommandBuilder command(io.vertx.rxjava.core.cli.CLI cli) { io.vertx.rxjava.ext.shell.command.CommandBuilder ret = io.vertx.rxjava.ext.shell.command.CommandBuilder.newInstance(io.vertx.ext.shell.command.CommandBuilder.command(cli.getDelegate())); return ret; }
@Override public void start(Future<Void> startFuture) throws Exception { CommandBuilder command = CommandBuilder.command("hello"); command.processHandler(process -> { }); registry.registerCommand(command.build(vertx), ar -> { if (ar.succeeded()) { startFuture.complete(); } else { startFuture.fail(ar.cause()); } }); } }, context.asyncAssertSuccess(id -> {
@Test public void testDuplicateRegistration(TestContext context) { CommandRegistry registry = CommandRegistry.getShared(vertx); Command a = CommandBuilder.command("a").build(vertx); Command b = CommandBuilder.command("b").build(vertx); registry.registerCommand(a, context.asyncAssertSuccess(reg -> { registry.registerCommands(Arrays.asList(a, b), context.asyncAssertFailure(err -> { context.assertEquals(1, registry.commands().size()); context.assertNotNull(registry.getCommand("a")); })); })); }
@Test public void testRegister(TestContext context) { CommandRegistry registry = CommandRegistry.getShared(vertx); CommandBuilder command = CommandBuilder.command("hello"); registry.registerCommand(command.build(vertx), context.asyncAssertSuccess(reg -> { registry.unregisterCommand("hello", context.asyncAssertSuccess(done -> { context.assertEquals(Collections.emptyList(), registry.commands()); })); })); }
@Test public void testVertx(TestContext context) { TestTtyConnection conn = new TestTtyConnection(vertx); ShellImpl shell = createShell(conn); shell.init().readline(); Async done = context.async(); commands.add(CommandBuilder.command("foo").processHandler(process -> { context.assertEquals(vertx, process.vertx()); done.complete(); })); conn.read("foo\r"); }
@Test public void testSuspendReadyProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); builder.processHandler(process -> context.fail()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); try { process.suspend(); context.fail(); } catch (Exception ignore) { } }
@Test public void testThrowExceptionInProcess(TestContext context) { commands.add(CommandBuilder.command("foo").processHandler(process -> { throw new RuntimeException(); })); Shell shell = server.createShell(); Job job = shell.createJob("foo"); Async async = context.async(); Pty pty = Pty.create(); job.setTty(pty.slave()).statusUpdateHandler(CommandProcessTest.terminateHandler(code -> { context.assertEquals(1, job.process().exitCode()); async.complete(); })).run(); }
@Test public void testResumeReadyProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); builder.processHandler(process -> context.fail()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); try { process.resume(); context.fail(); } catch (Exception ignore) { } }
@Test public void testInterruptReadyProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); builder.processHandler(process -> context.fail()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); try { process.interrupt(); context.fail(); } catch (Exception ignore) { } }
@Test public void testRunRunningProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); Async runningLatch = context.async(); builder.processHandler(process -> runningLatch.complete()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); process.run(); runningLatch.awaitSuccess(10000); try { process.run(); context.fail(); } catch (IllegalStateException ignore) { } }
@Test public void testResumeRunningProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); Async runningLatch = context.async(); builder.processHandler(process -> runningLatch.complete()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); process.run(); runningLatch.awaitSuccess(10000); try { process.resume(); context.fail(); } catch (Exception ignore) { } }
@Test public void testRun(TestContext context) { commands.add(CommandBuilder.command("foo").processHandler(process -> { process.end(3); })); Shell shell = server.createShell(); Job job = shell.createJob(CliToken.tokenize("foo")); Async async = context.async(); job.setTty(Pty.create().slave()).statusUpdateHandler(CommandProcessTest.terminateHandler(code -> { context.assertEquals(3, job.process().exitCode()); async.complete(); })).run(); }