@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(); } }); } }
@Test public void testHandleReadlineBuffered(TestContext context) { TestTtyConnection conn = new TestTtyConnection(vertx); ShellImpl shell = createShell(conn); shell.init().readline(); Async async = context.async(); commands.add(CommandBuilder.command("_not_consumed").processHandler(process -> { async.complete(); })); commands.add(CommandBuilder.command("read").processHandler(process -> { StringBuilder buffer = new StringBuilder(); process.stdinHandler(line -> { buffer.append(line); if (buffer.toString().equals("the_line")) { process.end(); } }); })); conn.read("read\rthe_line_not_consumed\r"); }
@Before public void before(TestContext context) { registry = CommandRegistry.getShared(rule.vertx()); registry.registerCommand(CommandBuilder.command("foo").processHandler(proc -> { }).completionHandler( completion -> { completion.complete("completed_by_foo", false); } ).build(rule.vertx()), context.asyncAssertSuccess(v1 -> { registry.registerCommand(CommandBuilder.command("bar").processHandler(proc -> { }).build(rule.vertx()), context.asyncAssertSuccess(v2 -> { registry.registerCommand(CommandBuilder.command("baz").processHandler(proc -> { }).build(rule.vertx()), context.asyncAssertSuccess(v3 -> { registry.registerCommand(CommandBuilder.command("err").processHandler(proc -> { }).completionHandler(completion -> { throw new RuntimeException("expected"); }).build(rule.vertx()), context.asyncAssertSuccess()); })); })); })); mgr = new InternalCommandManager(registry); }
@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")); })); })); }
/** * 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; }
/** * Build the command * @param vertx the vertx instance * @return the built command */ public io.vertx.rxjava.ext.shell.command.Command build(io.vertx.rxjava.core.Vertx vertx) { io.vertx.rxjava.ext.shell.command.Command ret = io.vertx.rxjava.ext.shell.command.Command.newInstance(delegate.build(vertx.getDelegate())); return ret; }
/** * Set the command process handler, the process handler is called when the command is executed. * @param handler the process handler * @return this command object */ public io.vertx.rxjava.ext.shell.command.CommandBuilder processHandler(Handler<io.vertx.rxjava.ext.shell.command.CommandProcess> handler) { delegate.processHandler(new Handler<io.vertx.ext.shell.command.CommandProcess>() { public void handle(io.vertx.ext.shell.command.CommandProcess event) { handler.handle(io.vertx.rxjava.ext.shell.command.CommandProcess.newInstance(event)); } }); return this; }
/** * Set the command completion handler, the completion handler when the user asks for contextual command line * completion, usually hitting the <i>tab</i> key. * @param handler the completion handler * @return this command object */ public io.vertx.rxjava.ext.shell.command.CommandBuilder completionHandler(Handler<io.vertx.rxjava.ext.shell.cli.Completion> handler) { delegate.completionHandler(new Handler<io.vertx.ext.shell.cli.Completion>() { public void handle(io.vertx.ext.shell.cli.Completion event) { handler.handle(io.vertx.rxjava.ext.shell.cli.Completion.newInstance(event)); } }); return this; }
@Test public void testExecuteReadlineBuffered(TestContext context) { TestTtyConnection conn = new TestTtyConnection(vertx); ShellImpl shell = createShell(conn); shell.init().readline(); Async async = context.async(); AtomicInteger count = new AtomicInteger(); commands.add(CommandBuilder.command("read").processHandler(process -> { if (count.incrementAndGet() == 2) { async.complete(); } process.end(0); })); conn.read("read\rread\r"); }
@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()); })); })); }
/** * 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; }
/** * Build the command * @param vertx the vertx instance * @return the built command */ public io.vertx.rxjava.ext.shell.command.Command build(io.vertx.rxjava.core.Vertx vertx) { io.vertx.rxjava.ext.shell.command.Command ret = io.vertx.rxjava.ext.shell.command.Command.newInstance(delegate.build(vertx.getDelegate())); return ret; }
/** * Set the command process handler, the process handler is called when the command is executed. * @param handler the process handler * @return this command object */ public io.vertx.rxjava.ext.shell.command.CommandBuilder processHandler(Handler<io.vertx.rxjava.ext.shell.command.CommandProcess> handler) { delegate.processHandler(new Handler<io.vertx.ext.shell.command.CommandProcess>() { public void handle(io.vertx.ext.shell.command.CommandProcess event) { handler.handle(io.vertx.rxjava.ext.shell.command.CommandProcess.newInstance(event)); } }); return this; }
/** * Set the command completion handler, the completion handler when the user asks for contextual command line * completion, usually hitting the <i>tab</i> key. * @param handler the completion handler * @return this command object */ public io.vertx.rxjava.ext.shell.command.CommandBuilder completionHandler(Handler<io.vertx.rxjava.ext.shell.cli.Completion> handler) { delegate.completionHandler(new Handler<io.vertx.ext.shell.cli.Completion>() { public void handle(io.vertx.ext.shell.cli.Completion event) { handler.handle(io.vertx.rxjava.ext.shell.cli.Completion.newInstance(event)); } }); return this; }
@Override public void start() throws Exception { Command starwars = CommandBuilder.command("top"). processHandler(process -> { }); }).build(vertx);
@Test public void testSessionExpires(TestContext context) throws Exception { Async ended = context.async(); registry.add(CommandBuilder.command("cmd").processHandler(process -> { process.endHandler(v -> { ended.complete(); }); })); startShellServer(context, 100, 100); long now = System.currentTimeMillis(); TestTtyConnection conn = termServer.openConnection(); conn.read("cmd\r"); ended.awaitSuccess(20000); context.assertTrue(conn.isClosed()); long elapsed = System.currentTimeMillis() - now; context.assertTrue(elapsed < 1000); }
@Test public void testUndeployCommands(TestContext context) throws Exception { Async async = context.async(); registry.registerCommands( Arrays.asList(CommandBuilder.command("a").build(vertx), CommandBuilder.command("b").build(vertx)), context.asyncAssertSuccess(list -> async.complete())); async.awaitSuccess(2000); Set<String> afterIds = new HashSet<>(vertx.deploymentIDs()); System.out.println(afterIds); context.assertEquals(1, afterIds.size()); String deploymentId = afterIds.iterator().next(); Async async2 = context.async(); registry.unregisterCommand("a", context.asyncAssertSuccess(v -> async2.complete())); async2.awaitSuccess(2000); context.assertTrue(vertx.deploymentIDs().contains(deploymentId)); Async async3 = context.async(); registry.unregisterCommand("b", context.asyncAssertSuccess(v -> async3.complete())); async3.awaitSuccess(2000); context.assertFalse(vertx.deploymentIDs().contains(deploymentId)); } }
/** * 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; }
public void add(CommandBuilder builder) { add(builder.build(vertx)); }
@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(); } }); } }