diff --git a/src/main/java/com/serliunx/ddns/ManagerLite.java b/src/main/java/com/serliunx/ddns/ManagerLite.java index d08a60d..ed00292 100644 --- a/src/main/java/com/serliunx/ddns/ManagerLite.java +++ b/src/main/java/com/serliunx/ddns/ManagerLite.java @@ -4,10 +4,6 @@ import com.serliunx.ddns.config.PropertiesConfiguration; import com.serliunx.ddns.constant.SystemConstants; import com.serliunx.ddns.core.context.FileInstanceContext; import com.serliunx.ddns.support.SystemInitializer; -import com.serliunx.ddns.support.command.CommandManager; -import com.serliunx.ddns.support.command.cmd.*; - -import java.util.Scanner; /** * 启动类 @@ -30,28 +26,4 @@ public final class ManagerLite { systemInitializer.refresh(); return systemInitializer; } - - @Deprecated - private static CommandManager registerCommand(SystemInitializer systemInitializer) { - CommandManager commandManager = new CommandManager(systemInitializer); - commandManager.register(new HelpCommand(commandManager)); - commandManager.register(new IpCommand(commandManager)); - commandManager.register(new ExitCommand(commandManager)); - commandManager.register(new InstanceCommand(commandManager)); - commandManager.register(new RefreshCommand(commandManager)); - return commandManager; - } - - @Deprecated - @SuppressWarnings("all") - private static void handleCommand(CommandManager commandManager, SystemInitializer systemInitializer) { - Scanner scanner = new Scanner(System.in); - String commandString = ""; - - while (true) { - System.out.print("> "); - commandString = scanner.nextLine(); - commandManager.handle(commandString); - } - } } diff --git a/src/main/java/com/serliunx/ddns/exception/CommandException.java b/src/main/java/com/serliunx/ddns/exception/CommandException.java deleted file mode 100644 index 5520dd6..0000000 --- a/src/main/java/com/serliunx/ddns/exception/CommandException.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.serliunx.ddns.exception; - -import com.serliunx.ddns.support.command.cmd.Command; - -/** - * 指令相关异常 - * @author SerLiunx - * @since 1.0.0 - */ -public abstract class CommandException extends RuntimeException { - - private Command command; - - public CommandException() {} - - public CommandException(String message) { - super(message); - } - - public CommandException(Command command) { - this.command = command; - } - - public CommandException(Command command, String message) { - super(message); - this.command = command; - } - - public CommandException(Command command, String message, Throwable cause) { - super(message, cause); - this.command = command; - } - - public CommandException(Command command, Throwable cause) { - super(cause); - this.command = command; - } - - public CommandException(Command command, String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - this.command = command; - } -} diff --git a/src/main/java/com/serliunx/ddns/exception/CommandExceptionNotExistsException.java b/src/main/java/com/serliunx/ddns/exception/CommandExceptionNotExistsException.java deleted file mode 100644 index 6e96986..0000000 --- a/src/main/java/com/serliunx/ddns/exception/CommandExceptionNotExistsException.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.serliunx.ddns.exception; - -/** - * 指令相关异常: 指令不存在. - * @author SerLiunx - * @since 1.0.0 - */ -public class CommandExceptionNotExistsException extends CommandException { - - public CommandExceptionNotExistsException(String message) { - super(message); - } -} diff --git a/src/main/java/com/serliunx/ddns/support/command/CommandArg.java b/src/main/java/com/serliunx/ddns/support/command/CommandArg.java deleted file mode 100644 index cb50a1b..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/CommandArg.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.serliunx.ddns.support.command; - -/** - * 指令参数信息包装 - * @author SerLiunx - * @since 1.0.0 - */ -public class CommandArg { - - /** - * 参数名称 - */ - private final String arg; - - /** - * 参数说明 - */ - private final String description; - - public CommandArg(String arg, String description) { - this.arg = arg; - this.description = description; - } - - public String getArg() { - return arg; - } - - public String getDescription() { - return description; - } -} diff --git a/src/main/java/com/serliunx/ddns/support/command/CommandManager.java b/src/main/java/com/serliunx/ddns/support/command/CommandManager.java deleted file mode 100644 index 4dfb8e3..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/CommandManager.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.serliunx.ddns.support.command; - -import com.serliunx.ddns.exception.CommandExceptionNotExistsException; -import com.serliunx.ddns.support.SystemInitializer; -import com.serliunx.ddns.support.command.cmd.Command; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashMap; -import java.util.Map; - -/** - * 指令管理 - * @author SerLiunx - * @since 1.0.0 - */ -public final class CommandManager { - - private static final Logger log = LoggerFactory.getLogger(CommandManager.class); - - private final Map commandMap = new HashMap<>(); - private final SystemInitializer initializer; - - public CommandManager(SystemInitializer systemInitializer) { - this.initializer = systemInitializer; - } - - public Command getCommand(String commandName) { - Command command = commandMap.get(commandName); - if (command == null) - throw new CommandExceptionNotExistsException(String.format("指令 %s 不存在!", commandName)); - return command; - } - - public SystemInitializer getInitializer() { - return initializer; - } - - /** - * 注册指令 - * @param command 指令 - */ - public void register(Command command) { - boolean result = register0(command); - if (result) - log.debug("成功注册指令 => {}", command.getName()); - else - log.warn("指令注册失败 => {}", command); - } - - public void handle(String commandString) { - String[] args = commandString.split(" "); - if (args.length < 1) { - log.warn("未找到相关指令! 输入 help 查看指令手册."); - return; - } - Command cmd = commandMap.get(args[0]); - if (cmd == null) { - log.warn("未找到相关指令! 输入 help 查看指令手册."); - return; - } - String[] subArgs = new String[args.length - 1]; - System.arraycopy(args, 1, subArgs, 0, args.length - 1); - cmd.execute(subArgs); - } - - private boolean register0(Command command) { - if (command == null) return false; - String cmdName = command.getName(); - if (cmdName == null || cmdName.isEmpty()) - return false; - if (commandMap.containsKey(cmdName)) - return false; - commandMap.put(cmdName, command); - return true; - } -} diff --git a/src/main/java/com/serliunx/ddns/support/command/cmd/AbstractCommand.java b/src/main/java/com/serliunx/ddns/support/command/cmd/AbstractCommand.java deleted file mode 100644 index bf5cd82..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/cmd/AbstractCommand.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.serliunx.ddns.support.command.cmd; - -import com.serliunx.ddns.support.command.CommandArg; -import com.serliunx.ddns.support.command.CommandManager; - -import java.util.Collections; -import java.util.List; - -/** - * 指令抽象实现 - * @author SerLiunx - * @since 1.0.0 - */ -public abstract class AbstractCommand implements Command { - - protected final String name; - protected final Command[] children; - protected final CommandManager commandManager; - - public AbstractCommand(String name, Command[] children, CommandManager commandManager) { - this.name = name; - this.children = children; - this.commandManager = commandManager; - } - - public AbstractCommand(String name, CommandManager commandManager) { - this(name, null, commandManager); - } - - @Override - public String getName() { - return name; - } - - @Override - public Command[] getChildren() { - return children; - } - - @Override - public List getArgs() { - return Collections.emptyList(); - } - - @Override - public abstract boolean execute(String[] args); - - @Override - public String toString() { - return "Command{" + - "name='" + name + '\'' + - '}'; - } -} diff --git a/src/main/java/com/serliunx/ddns/support/command/cmd/Command.java b/src/main/java/com/serliunx/ddns/support/command/cmd/Command.java deleted file mode 100644 index b27ce39..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/cmd/Command.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.serliunx.ddns.support.command.cmd; - -import com.serliunx.ddns.support.command.CommandArg; - -import java.util.List; - -/** - * 指令接口 - * @author SerLiunx - * @since 1.0.0 - */ -public interface Command { - - /** - * 获取指令名称 - * @return 名称 - */ - String getName(); - - /** - * 指令逻辑 - * @param args 参数 - * @return 执行结果 - */ - @SuppressWarnings("all") - boolean execute(String[] args); - - /** - * 获取子命令 - * @return 子命令 - */ - Command[] getChildren(); - - /** - * 获取指令参数信息 - * @return 参数信息 - */ - List getArgs(); -} diff --git a/src/main/java/com/serliunx/ddns/support/command/cmd/ExitCommand.java b/src/main/java/com/serliunx/ddns/support/command/cmd/ExitCommand.java deleted file mode 100644 index 9796e36..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/cmd/ExitCommand.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.serliunx.ddns.support.command.cmd; - -import com.serliunx.ddns.support.command.CommandManager; - -/** - * @author SerLiunx - * @since 1.0.0 - */ -public class ExitCommand extends AbstractCommand { - - public ExitCommand(Command[] children, CommandManager commandManager) { - super("exit", children, commandManager); - } - - public ExitCommand(CommandManager commandManager) { - this(null, commandManager); - } - - @Override - public boolean execute(String[] args) { - System.exit(0); - return false; - } -} diff --git a/src/main/java/com/serliunx/ddns/support/command/cmd/HelpCommand.java b/src/main/java/com/serliunx/ddns/support/command/cmd/HelpCommand.java deleted file mode 100644 index e2c159b..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/cmd/HelpCommand.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.serliunx.ddns.support.command.cmd; - -import com.serliunx.ddns.support.command.CommandArg; -import com.serliunx.ddns.support.command.CommandManager; - -import java.util.List; - -/** - * 帮助指令: help - * @author SerLiunx - * @since 1.0.0 - */ -public class HelpCommand extends AbstractCommand { - - private static final String NO_ARGS_INFORMATION = "未找到相关参数信息"; - - public HelpCommand(Command[] children, CommandManager commandManager) { - super("help", children, commandManager); - } - - public HelpCommand(CommandManager commandManager) { - this(null, commandManager); - } - - @Override - public boolean execute(String[] args) { - int length = args.length; - if (length < 1) - return false; - Command targetCommand = commandManager.getCommand(args[0]); - List commandArgs = targetCommand.getArgs(); - if (commandArgs == null || commandArgs.isEmpty()) { - System.out.println(NO_ARGS_INFORMATION); - return true; - } - System.out.println(buildArgs(commandArgs)); - return true; - } - - private String buildArgs(List args) { - StringBuilder argsBuilder = new StringBuilder(); - for (int i = 0; i < args.size(); i++) { - argsBuilder.append(args.get(i).getArg()).append(" - ").append(args.get(i).getDescription()); - if (i != args.size() - 1) - argsBuilder.append("\n"); - } - return argsBuilder.toString(); - } -} diff --git a/src/main/java/com/serliunx/ddns/support/command/cmd/InstanceCommand.java b/src/main/java/com/serliunx/ddns/support/command/cmd/InstanceCommand.java deleted file mode 100644 index c4eb03a..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/cmd/InstanceCommand.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.serliunx.ddns.support.command.cmd; - -import com.serliunx.ddns.support.command.CommandManager; - -import java.util.Arrays; - -/** - * 实例相关指令 - * @author SerLiunx - * @since 1.0.0 - */ -public class InstanceCommand extends AbstractCommand { - - public InstanceCommand(Command[] children, CommandManager commandManager) { - super("instance", children, commandManager); - } - - public InstanceCommand(CommandManager commandManager) { - this(null, commandManager); - } - - @Override - public boolean execute(String[] args) { - System.out.println(Arrays.toString(args)); - return false; - } -} diff --git a/src/main/java/com/serliunx/ddns/support/command/cmd/IpCommand.java b/src/main/java/com/serliunx/ddns/support/command/cmd/IpCommand.java deleted file mode 100644 index ce3fce7..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/cmd/IpCommand.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.serliunx.ddns.support.command.cmd; - -import com.serliunx.ddns.support.NetworkContextHolder; -import com.serliunx.ddns.support.command.CommandManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * ip 相关指令 - * @author SerLiunx - * @since 1.0.0 - */ -public class IpCommand extends AbstractCommand { - - private static final Logger log = LoggerFactory.getLogger(IpCommand.class); - - public IpCommand(Command[] children, CommandManager commandManager) { - super("ip", children, commandManager); - } - - public IpCommand(CommandManager commandManager) { - this(null, commandManager); - } - - @Override - public boolean execute(String[] args) { - String newestIp = NetworkContextHolder.getIpAddress(); - log.info("当前最新IP地址为: {}", newestIp); - return true; - } -} diff --git a/src/main/java/com/serliunx/ddns/support/command/cmd/RefreshCommand.java b/src/main/java/com/serliunx/ddns/support/command/cmd/RefreshCommand.java deleted file mode 100644 index fdb0752..0000000 --- a/src/main/java/com/serliunx/ddns/support/command/cmd/RefreshCommand.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.serliunx.ddns.support.command.cmd; - -import com.serliunx.ddns.support.command.CommandManager; - -/** - * @author SerLiunx - * @since 1.0.0 - */ -public class RefreshCommand extends AbstractCommand { - - public RefreshCommand(Command[] children, CommandManager commandManager) { - super("refresh", children, commandManager); - } - - public RefreshCommand(CommandManager commandManager) { - this(null, commandManager); - } - - @Override - public boolean execute(String[] args) { - return false; - } -}