process.write(buf.toString()); });
url = new URL(process.commandLine().getArgumentValue(0)); } catch (MalformedURLException e) { process.write("Bad url\n").end(); return; process.write("Connecting to " + url + "\n"); int port = url.getPort(); if (port == -1) { process.write("wget: error " + err.getMessage() + "\n"); process.end(); }); req.handler(resp -> { process.write(resp.statusCode() + " " + resp.statusMessage() + "\n"); String contentType = resp.getHeader("Content-Type"); String contentLength = resp.getHeader("Content-Length"); process.write("Length: " + (contentLength != null ? contentLength : "unspecified")); if (contentType != null) { process.write("[" + contentType + "]"); process.write("\n"); process.end(); });
@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(); } }); } }
/** * Write some text to the standard output. * @param data the text * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.shell.command.CommandProcess write(String data) { delegate.write(data); return this; }
/** * Write some text to the standard output. * @param data the text * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.shell.command.CommandProcess write(String data) { delegate.write(data); return this; }
@Override public void process(CommandProcess process) { VertxInternal vertx = (VertxInternal) process.vertx(); process.write("\nNet Servers:\n"); for (Map.Entry<ServerID, NetServerImpl> server : vertx.sharedNetServers().entrySet()) { process.write(server.getKey().host + ":" + server.getKey().port + "\n"); } process.write("\nHTTP Servers:\n"); for (Map.Entry<ServerID, HttpServerImpl> server : vertx.sharedHttpServers().entrySet()) { process.write(server.getKey().host + ":" + server.getKey().port + "\n"); } process.end(); } }
@Override public void process(CommandProcess process) { process.vertx().undeploy(id, ar -> { if (ar.succeeded()) { process.write("Undeployed " + id + "\n").end(); } else { process.write("Could not undeploy " + id + "\n"); StringWriter buffer = new StringWriter(); PrintWriter writer = new PrintWriter(buffer); ar.cause().printStackTrace(writer); process.write(buffer.toString()).end(); } }); } }
@Override public void process(CommandProcess process) { InternalCommandManager mgr = process.session().get("vert.x-command-manager"); List<Command> commands = mgr.getResolvers().stream().flatMap(r -> r.commands().stream()).distinct().collect(Collectors.toList()); process.write("available commands:\n"); for (Command command : commands) { process.write(command.name()).write("\n"); } process.end(); } }
@Override public void process(CommandProcess process) { MetricsService metrics = MetricsService.create(process.vertx()); metrics.metricsNames().forEach(name -> { process.write(name + "\n"); }); process.end(); } }
@Override public void process(CommandProcess process) { VertxInternal vertx = (VertxInternal) process.vertx(); for (VerticleFactory factory : vertx.verticleFactories()) { process.write(factory.getClass().getName() + ": prefix=" + factory.prefix() + ", order=" + factory.order() + ", requiresResolve=" + factory.requiresResolve() + "\n"); } process.end(); } }
@Override public void process(CommandProcess process) { String name = process.args().get(0); MetricsService metrics = MetricsService.create(process.vertx()); JsonObject snapshot = metrics.getMetricsSnapshot(name); process.write(snapshot.encodePrettily() + "\n"); process.end(); } }
@Override public void process(CommandProcess process) { Vertx vertx = process.vertx(); SharedData sharedData = vertx.sharedData(); LocalMap<Object, Object> map = sharedData.getLocalMap(this.map); if (keys != null) { for (String key : keys) { Object value = map.get(key); process.write(key + ": " + value + "\n"); } } process.end(); } }
@Override public void process(CommandProcess process) { VertxInternal vertx = (VertxInternal) process.vertx(); for (String id : vertx.deploymentIDs()) { Deployment deployment = vertx.getDeployment(id); process.write(id + ": " + deployment.verticleIdentifier() + ", options=" + deployment.deploymentOptions().toJson() + "\n"); } process.end(); } }
@Override public void process(CommandProcess process) { if (process.args().size() > 0) { String cwd = process.session().get("cwd"); new FsHelper().cd(process.vertx().fileSystem(), cwd, dir, ar -> { if (ar.succeeded()) { process.session().put("cwd", ar.result()); process.end(); } else { process.write("cd: No such file or directory\n"); process.end(); } }); } else { process.session().remove("cwd"); process.end(); } }
@Test public void testStdout(TestContext context) { commands.add(CommandBuilder.command("foo").processHandler(process -> { process.write("bye_world"); process.end(0); })); Shell shell = server.createShell(); Job job = shell.createJob("foo"); Async async = context.async(); LinkedList<String> out = new LinkedList<>(); Pty pty = Pty.create(); pty.stdoutHandler(out::add); job.setTty(pty.slave()); job.statusUpdateHandler(CommandProcessTest.terminateHandler(code -> { context.assertEquals(0, job.process().exitCode()); context.assertEquals(Arrays.asList("bye_world"), out); async.complete(); })).run(); }
@Test public void testResize(TestContext context) { commands.add(CommandBuilder.command("foo").processHandler(process -> { context.assertEquals(20, process.width()); context.assertEquals(10, process.height()); process.resizehandler(v -> { context.assertEquals(25, process.width()); context.assertEquals(15, process.height()); process.end(0); }); process.write("ping"); })); Shell shell = server.createShell(); Job job = shell.createJob("foo"); Pty pty = Pty.create(); Async async = context.async(); pty.setSize(20, 10); pty.stdoutHandler(text -> { pty.setSize(25, 15); }); job.setTty(pty.slave()).statusUpdateHandler(CommandProcessTest.terminateHandler(status -> { async.complete(); })).run(); }