Search in sources :

Example 1 with Command

use of de.jackwhite20.japs.server.command.Command in project JaPS by JackWhite20.

the class JaPS method start.

public void start() throws Exception {
    logger.info("Starting JaPS server");
    running = true;
    jaPSServer = new JaPSServer(config);
    String line;
    while (running) {
        line = consoleReader.readLine("> ");
        if (!line.isEmpty()) {
            String[] split = ARGS_SPLIT.split(line);
            if (split.length == 0) {
                continue;
            }
            String commandName = split[0].toLowerCase();
            // Try to get the command with the name
            Command command = commandManager.findCommand(commandName);
            if (command != null) {
                logger.log(Level.INFO, "Executing command: {0}", line);
                String[] cmdArgs = Arrays.copyOfRange(split, 1, split.length);
                command.execute(cmdArgs);
            } else {
                logger.log(Level.INFO, "Command not found!");
            }
        }
    }
}
Also used : SubCommand(de.jackwhite20.japs.server.command.impl.sub.SubCommand) Command(de.jackwhite20.japs.server.command.Command)

Example 2 with Command

use of de.jackwhite20.japs.server.command.Command in project JaPS by JackWhite20.

the class JaPS method init.

public void init() {
    System.setProperty("library.jansi.version", "JaPS");
    AnsiConsole.systemInstall();
    try {
        consoleReader = new ConsoleReader();
        consoleReader.setExpandEvents(false);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Setup logging system
    logger = new JaPSLogger(consoleReader);
    System.setErr(new PrintStream(new LoggingOutputStream(logger, Level.SEVERE), true));
    System.setOut(new PrintStream(new LoggingOutputStream(logger, Level.INFO), true));
    // Register command
    commandManager = new CommandManager();
    commandManager.addCommand(new HelpCommand("help", new String[] { "h" }, "Show this output"));
    commandManager.addCommand(new SubCommand("sub", new String[] { "s", "subscribe" }, "Subscribe a channel and view it's prepareCacheSync passing"));
    commandManager.addCommand(new UnsubCommand("unsub", new String[] {}, "Unsubscribe previous subscribed channels"));
    commandManager.addCommand(new EndCommand("end", new String[] { "stop" }, "Shutdown the server"));
    commandManager.addCommand(new SetCommand("set", new String[] { "add" }, "Sets a key and value"));
    commandManager.addCommand(new GetCommand("get", new String[] {}, "Gets a value from a key"));
    commandManager.addCommand(new DeleteCommand("delete", new String[] { "remove", "del", "rm" }, "Gets a value from a key"));
    commandManager.addCommand(new SnapshotCommand("snapshot", new String[] {}, "Takes a memory snapshot"));
    // Autocomplete commands
    consoleReader.addCompleter(new StringsCompleter(commandManager.getCommands().stream().map(Command::getName).collect(Collectors.toList())));
    // Autocomplete files
    consoleReader.addCompleter(new FileNameCompleter());
    logger.info("Initialized");
}
Also used : PrintStream(java.io.PrintStream) JaPSLogger(de.jackwhite20.japs.server.logging.JaPSLogger) SubCommand(de.jackwhite20.japs.server.command.impl.sub.SubCommand) ConsoleReader(jline.console.ConsoleReader) LoggingOutputStream(de.jackwhite20.japs.server.logging.out.LoggingOutputStream) IOException(java.io.IOException) FileNameCompleter(jline.console.completer.FileNameCompleter) CommandManager(de.jackwhite20.japs.server.command.CommandManager) StringsCompleter(jline.console.completer.StringsCompleter)

Aggregations

SubCommand (de.jackwhite20.japs.server.command.impl.sub.SubCommand)2 Command (de.jackwhite20.japs.server.command.Command)1 CommandManager (de.jackwhite20.japs.server.command.CommandManager)1 JaPSLogger (de.jackwhite20.japs.server.logging.JaPSLogger)1 LoggingOutputStream (de.jackwhite20.japs.server.logging.out.LoggingOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ConsoleReader (jline.console.ConsoleReader)1 FileNameCompleter (jline.console.completer.FileNameCompleter)1 StringsCompleter (jline.console.completer.StringsCompleter)1