diff --git a/src/main/java/mc/server/Main.java b/src/main/java/mc/server/Main.java index 4df4f29..dc9741e 100644 --- a/src/main/java/mc/server/Main.java +++ b/src/main/java/mc/server/Main.java @@ -1,28 +1,22 @@ package mc.server; -import mc.server.shell.CommandStore; -import org.jline.builtins.Builtins; +import mc.server.shell.CommandService; +import mc.server.shell.CommandServiceImpl; +import mc.server.shell.SystemPropertiesCommand; import org.jline.builtins.CommandRegistry; import org.jline.reader.*; -import org.jline.reader.impl.completer.NullCompleter; import java.io.PrintWriter; -import java.util.Collections; public class Main { public static void main(String[] args) { - CommandStore commandStore = new CommandStore(); - commandStore.register("system-properties", new Builtins.CommandMethods( - commandInput -> { - final PrintWriter writer = commandInput.terminal().writer(); - System.getProperties().forEach((key, value) -> - writer.println(String.format("%s = %s", key, value.toString()))); - }, - commandName -> Collections.singletonList(NullCompleter.INSTANCE) - )); + System.out.println("mc-server"); - final Completer completer = CommandRegistry.compileCompleters(commandStore); + CommandService commandService = new CommandServiceImpl(); + commandService.register("system-properties", new SystemPropertiesCommand()); + + final Completer completer = CommandRegistry.compileCompleters(commandService); final LineReader reader = LineReaderBuilder.builder() .completer(completer) @@ -38,8 +32,8 @@ public class Main { final ParsedLine parsedLine = reader.getParser().parse(line, 0); final String commandName = parsedLine.word(); - if (commandStore.hasCommand(commandName)) { - commandStore.execute(session, commandName); + if (commandService.hasCommand(commandName)) { + commandService.execute(session, commandName); } terminalWriter.println("line = " + String.join(", ", parsedLine.words())); diff --git a/src/main/java/mc/server/shell/Command.java b/src/main/java/mc/server/shell/Command.java new file mode 100644 index 0000000..584c5dd --- /dev/null +++ b/src/main/java/mc/server/shell/Command.java @@ -0,0 +1,8 @@ +package mc.server.shell; + +import org.jline.builtins.Builtins; + +public interface Command { + + void execute(Builtins.CommandInput commandInput); +} diff --git a/src/main/java/mc/server/shell/CommandService.java b/src/main/java/mc/server/shell/CommandService.java new file mode 100644 index 0000000..f9cdef6 --- /dev/null +++ b/src/main/java/mc/server/shell/CommandService.java @@ -0,0 +1,10 @@ +package mc.server.shell; + +import org.jline.builtins.CommandRegistry; + +public interface CommandService extends CommandRegistry { + + void register(String commandName, Command command); + + void execute(CommandSession session, String commandName); +} diff --git a/src/main/java/mc/server/shell/CommandServiceImpl.java b/src/main/java/mc/server/shell/CommandServiceImpl.java new file mode 100644 index 0000000..887fe66 --- /dev/null +++ b/src/main/java/mc/server/shell/CommandServiceImpl.java @@ -0,0 +1,51 @@ +package mc.server.shell; + +import org.jline.builtins.Builtins; +import org.jline.builtins.Builtins.CommandMethods; +import org.jline.builtins.Completers.SystemCompleter; +import org.jline.reader.impl.completer.NullCompleter; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class CommandServiceImpl implements CommandService { + + private final Map commands = new HashMap<>(); + + public void register(String commandName, Command command) { + commands.put(commandName, new CommandMethods( + command::execute, + cmdName -> Collections.singletonList(NullCompleter.INSTANCE) + )); + } + + public void execute(CommandSession session, String commandName) { + commands.get(commandName).execute().accept(new Builtins.CommandInput(commandName, new Object[0], session)); + } + + @Override + public Set commandNames() { + return commands.keySet(); + } + + @Override + public Map commandAliases() { + return Collections.emptyMap(); + } + + @Override + public boolean hasCommand(String commandName) { + return commands.containsKey(commandName); + } + + @Override + public SystemCompleter compileCompleters() { + final SystemCompleter systemCompleter = new SystemCompleter(); + systemCompleter.addAliases(commandAliases()); + commands.forEach((commandName, command) -> systemCompleter.add(commandName, NullCompleter.INSTANCE)); + + return systemCompleter; + } +} diff --git a/src/main/java/mc/server/shell/CommandStore.java b/src/main/java/mc/server/shell/CommandStore.java deleted file mode 100644 index c0a8c1f..0000000 --- a/src/main/java/mc/server/shell/CommandStore.java +++ /dev/null @@ -1,50 +0,0 @@ -package mc.server.shell; - -import org.jline.builtins.Builtins.CommandInput; -import org.jline.builtins.Builtins.CommandMethods; -import org.jline.builtins.CommandRegistry; -import org.jline.builtins.Completers.SystemCompleter; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -public class CommandStore implements CommandRegistry { - - private final Map commandExecutors = new HashMap<>(); - - public void register(String name, CommandMethods commandMethods) { - commandExecutors.put(name, commandMethods); - } - - public void execute(CommandSession session, String command) { - commandExecutors.get(command).execute().accept(new CommandInput(command, new Object[0], session)); - } - - @Override - public Set commandNames() { - return commandExecutors.keySet(); - } - - @Override - public Map commandAliases() { - return Collections.emptyMap(); - } - - @Override - public boolean hasCommand(String command) { - return commandExecutors.containsKey(command); - } - - @Override - public SystemCompleter compileCompleters() { - SystemCompleter systemCompleter = new SystemCompleter(); - - commandExecutors.forEach((name, commandMethods) -> - systemCompleter.add(name, commandMethods.compileCompleter().apply(name))); - systemCompleter.addAliases(commandAliases()); - - return systemCompleter; - } -} diff --git a/src/main/java/mc/server/shell/SystemPropertiesCommand.java b/src/main/java/mc/server/shell/SystemPropertiesCommand.java new file mode 100644 index 0000000..edda06c --- /dev/null +++ b/src/main/java/mc/server/shell/SystemPropertiesCommand.java @@ -0,0 +1,15 @@ +package mc.server.shell; + +import org.jline.builtins.Builtins; + +import java.io.PrintWriter; + +public class SystemPropertiesCommand implements Command { + + @Override + public void execute(Builtins.CommandInput commandInput) { + final PrintWriter writer = commandInput.terminal().writer(); + System.getProperties().forEach((key, value) -> + writer.println(String.format("%s = %s", key, value.toString()))); + } +}