Search in sources :

Example 1 with Command

use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.

the class HelloWorldCommand method start.

@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();
        }
    });
}
Also used : TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) CommandRegistry(io.vertx.ext.shell.command.CommandRegistry) AbstractVerticle(io.vertx.core.AbstractVerticle) ShellService(io.vertx.ext.shell.ShellService) CommandBuilder(io.vertx.ext.shell.command.CommandBuilder) Runner(io.vertx.example.util.Runner) ShellService(io.vertx.ext.shell.ShellService) TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions)

Example 2 with Command

use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.

the class StarwarsCommand method start.

@Override
public void start() throws Exception {
    Command starwars = CommandBuilder.command("starwars").processHandler(process -> {
        // Connect the client
        NetClient client = process.vertx().createNetClient();
        client.connect(23, "towel.blinkenlights.nl", ar -> {
            if (ar.succeeded()) {
                NetSocket socket = ar.result();
                // Ctrl-C closes the socket
                process.interruptHandler(v -> {
                    socket.close();
                });
                // 
                socket.handler(buff -> {
                    // Push the data to the Shell
                    process.write(buff.toString("UTF-8"));
                }).exceptionHandler(err -> {
                    err.printStackTrace();
                    socket.close();
                });
                // When socket closes, end the command
                socket.closeHandler(v -> {
                    process.end();
                });
            } else {
                process.write("Could not connect to remote Starwars server\n").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();
        }
    });
}
Also used : TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) CommandRegistry(io.vertx.ext.shell.command.CommandRegistry) AbstractVerticle(io.vertx.core.AbstractVerticle) ShellService(io.vertx.ext.shell.ShellService) CommandBuilder(io.vertx.ext.shell.command.CommandBuilder) NetClient(io.vertx.core.net.NetClient) Runner(io.vertx.example.util.Runner) NetSocket(io.vertx.core.net.NetSocket) NetSocket(io.vertx.core.net.NetSocket) ShellService(io.vertx.ext.shell.ShellService) NetClient(io.vertx.core.net.NetClient) TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions)

Example 3 with Command

use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.

the class TopCommand method start.

@Override
public void start() throws Exception {
    Command starwars = CommandBuilder.command("top").processHandler(process -> {
        long id = process.vertx().setPeriodic(500, id_ -> {
            StringBuilder buf = new StringBuilder();
            Formatter formatter = new Formatter(buf);
            List<Thread> threads = new ArrayList<>(Thread.getAllStackTraces().keySet());
            for (int i = 1; i <= process.height(); i++) {
                // Change cursor position and erase line with ANSI escape code magic
                buf.append("\033[").append(i).append(";1H\033[K");
                // 
                String format = "  %1$-5s %2$-20s %3$-50s %4$s";
                if (i == 1) {
                    formatter.format(format, "ID", "STATE", "NAME", "GROUP");
                } else {
                    int index = i - 2;
                    if (index < threads.size()) {
                        Thread thread = threads.get(index);
                        formatter.format(format, thread.getId(), thread.getState().name(), thread.getName(), thread.getThreadGroup().getName());
                    }
                }
            }
            process.write(buf.toString());
        });
        // Terminate when user hits Ctrl-C
        process.interruptHandler(v -> {
            vertx.cancelTimer(id);
            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();
        }
    });
}
Also used : TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) List(java.util.List) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) CommandRegistry(io.vertx.ext.shell.command.CommandRegistry) AbstractVerticle(io.vertx.core.AbstractVerticle) ShellService(io.vertx.ext.shell.ShellService) CommandBuilder(io.vertx.ext.shell.command.CommandBuilder) Runner(io.vertx.example.util.Runner) ArrayList(java.util.ArrayList) Formatter(java.util.Formatter) ShellService(io.vertx.ext.shell.ShellService) TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) Formatter(java.util.Formatter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with Command

use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.

the class EchoKeyboardCommand method start.

@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();
        }
    });
}
Also used : TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) List(java.util.List) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) CommandRegistry(io.vertx.ext.shell.command.CommandRegistry) AbstractVerticle(io.vertx.core.AbstractVerticle) ShellService(io.vertx.ext.shell.ShellService) CommandBuilder(io.vertx.ext.shell.command.CommandBuilder) Runner(io.vertx.example.util.Runner) ArrayList(java.util.ArrayList) Formatter(java.util.Formatter) ShellService(io.vertx.ext.shell.ShellService) TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions)

Example 5 with Command

use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.

the class WgetCommand method start.

@Override
public void start() throws Exception {
    // Create the wget CLI
    CLI cli = CLI.create("wget").setSummary("Wget implemented with Vert.x HTTP client").addArgument(new Argument().setIndex(0).setArgName("http-url").setDescription("the HTTP uri to get"));
    // Create the command
    Command helloWorld = CommandBuilder.command(cli).processHandler(process -> {
        URL url;
        try {
            url = new URL(process.commandLine().getArgumentValue(0));
        } catch (MalformedURLException e) {
            process.write("Bad url\n").end();
            return;
        }
        HttpClient client = process.vertx().createHttpClient();
        process.write("Connecting to " + url + "\n");
        int port = url.getPort();
        if (port == -1) {
            port = 80;
        }
        HttpClientRequest req = client.get(port, url.getHost(), url.getPath());
        req.exceptionHandler(err -> {
            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();
        });
        req.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();
        }
    });
}
Also used : TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) ShellService(io.vertx.ext.shell.ShellService) CommandBuilder(io.vertx.ext.shell.command.CommandBuilder) Argument(io.vertx.core.cli.Argument) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Command(io.vertx.ext.shell.command.Command) CLI(io.vertx.core.cli.CLI) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) CommandRegistry(io.vertx.ext.shell.command.CommandRegistry) AbstractVerticle(io.vertx.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) HttpClient(io.vertx.core.http.HttpClient) CLI(io.vertx.core.cli.CLI) ShellService(io.vertx.ext.shell.ShellService) MalformedURLException(java.net.MalformedURLException) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Argument(io.vertx.core.cli.Argument) TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) HttpClient(io.vertx.core.http.HttpClient) URL(java.net.URL)

Aggregations

AbstractVerticle (io.vertx.core.AbstractVerticle)5 Runner (io.vertx.example.util.Runner)5 ShellService (io.vertx.ext.shell.ShellService)5 ShellServiceOptions (io.vertx.ext.shell.ShellServiceOptions)5 Command (io.vertx.ext.shell.command.Command)5 CommandBuilder (io.vertx.ext.shell.command.CommandBuilder)5 CommandRegistry (io.vertx.ext.shell.command.CommandRegistry)5 TelnetTermOptions (io.vertx.ext.shell.term.TelnetTermOptions)5 ArrayList (java.util.ArrayList)2 Formatter (java.util.Formatter)2 List (java.util.List)2 Argument (io.vertx.core.cli.Argument)1 CLI (io.vertx.core.cli.CLI)1 HttpClient (io.vertx.core.http.HttpClient)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 NetClient (io.vertx.core.net.NetClient)1 NetSocket (io.vertx.core.net.NetSocket)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1