diff --git a/src/main/java/com/serliunx/ddns/ManagerLite.java b/src/main/java/com/serliunx/ddns/ManagerLite.java index 572c457..2a04d3d 100644 --- a/src/main/java/com/serliunx/ddns/ManagerLite.java +++ b/src/main/java/com/serliunx/ddns/ManagerLite.java @@ -10,6 +10,7 @@ import com.serliunx.ddns.support.InstanceContextHolder; import com.serliunx.ddns.support.SystemInitializer; import com.serliunx.ddns.support.command.CommandDispatcher; import com.serliunx.ddns.support.command.target.HelpCommand; +import com.serliunx.ddns.support.command.target.ReloadCommand; import com.serliunx.ddns.support.log.JLineAdaptAppender; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; @@ -119,6 +120,8 @@ public final class ManagerLite { commandDispatcher = CommandDispatcher.getInstance(); // help commandDispatcher.register(new HelpCommand()); + // reload + commandDispatcher.register(new ReloadCommand(configuration)); } /** diff --git a/src/main/java/com/serliunx/ddns/support/command/CommandDispatcher.java b/src/main/java/com/serliunx/ddns/support/command/CommandDispatcher.java index eed6dcc..1d96795 100644 --- a/src/main/java/com/serliunx/ddns/support/command/CommandDispatcher.java +++ b/src/main/java/com/serliunx/ddns/support/command/CommandDispatcher.java @@ -1,12 +1,11 @@ package com.serliunx.ddns.support.command; -import com.serliunx.ddns.ManagerLite; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * 指令调度器 @@ -26,7 +25,16 @@ public final class CommandDispatcher { /** * 最顶层指令缓存 */ - private final Map commands = new ConcurrentHashMap<>(128); + private final Map commands = new LinkedHashMap<>(128); + + /** + * 获取所有已注册的指令 + * + * @return 已注册的指令 + */ + public Map getCommands() { + return commands; + } /** * 指令注册 @@ -80,6 +88,16 @@ public final class CommandDispatcher { return newArgs; } + /** + * 检查是否存在参数 + * + * @param args 参数 + * @return 参数长度大于0返回真, 否则返回假 + */ + public static boolean hasArgs(String[] args) { + return args.length > 0; + } + /** * 获取实例 */ diff --git a/src/main/java/com/serliunx/ddns/support/command/target/HelpCommand.java b/src/main/java/com/serliunx/ddns/support/command/target/HelpCommand.java index cafc776..225cdc5 100644 --- a/src/main/java/com/serliunx/ddns/support/command/target/HelpCommand.java +++ b/src/main/java/com/serliunx/ddns/support/command/target/HelpCommand.java @@ -2,15 +2,19 @@ package com.serliunx.ddns.support.command.target; import com.serliunx.ddns.ManagerLite; import com.serliunx.ddns.support.command.AbstractCommand; +import com.serliunx.ddns.support.command.Command; +import com.serliunx.ddns.support.command.CommandDispatcher; import org.slf4j.Logger; -import java.util.Arrays; +import java.util.Map; + +import static com.serliunx.ddns.support.command.CommandDispatcher.hasArgs; /** * 指令: help * * @author SerLiunx - * @version 1.0.0 + * @version 1.0.4 * @since 2025/1/15 */ public class HelpCommand extends AbstractCommand { @@ -18,12 +22,35 @@ public class HelpCommand extends AbstractCommand { private static final Logger log = ManagerLite.getLogger(); public HelpCommand() { - super("help", null, "查看帮助信息", "help cmd"); + super("help", null, "查看帮助信息", "help <指令>"); } @Override public boolean onCommand(String[] args) { - System.out.println(Arrays.toString(args)); + final Map commands = CommandDispatcher.getInstance().getCommands(); + + log.info("=========================================="); + if (hasArgs(args)) { + final String cmd = args[0]; + final Command command = commands.get(cmd); + if (command == null) { + log.warn("无法找到指令 {} 的相关信息, 请使用 help 查看可用的指令及帮助!", cmd); + } else { + log.info("指令:\t{}\t-\t{}\t-\t{}", cmd, command.getDescription(), command.getUsage()); + } + } else { + commands.forEach((k, v) -> { + // 忽略 help 自身 + if (k.equals(getName())) { + return; + } + log.info("{}\t-\t{}\t-\t{}", k, v.getDescription(), v.getUsage()); + }); + log.info("exit\t-\t退出程序\t-\texit"); + log.info(""); + log.info("使用 help <指令> 来查看更详细的帮助信息."); + } + log.info("=========================================="); return true; } } \ No newline at end of file diff --git a/src/main/java/com/serliunx/ddns/support/command/target/ReloadCommand.java b/src/main/java/com/serliunx/ddns/support/command/target/ReloadCommand.java new file mode 100644 index 0000000..c3bf5b9 --- /dev/null +++ b/src/main/java/com/serliunx/ddns/support/command/target/ReloadCommand.java @@ -0,0 +1,37 @@ +package com.serliunx.ddns.support.command.target; + +import com.serliunx.ddns.ManagerLite; +import com.serliunx.ddns.config.Configuration; +import com.serliunx.ddns.support.command.AbstractCommand; +import org.slf4j.Logger; + +/** + * 指令: reload + * + * @author SerLiunx + * @version 1.0.4 + * @since 2025/1/16 + */ +public class ReloadCommand extends AbstractCommand { + + private static final Logger log = ManagerLite.getLogger(); + + /** + * 配置信息 + */ + private final Configuration configuration; + + public ReloadCommand(Configuration configuration) { + super("reload", null, "重新载入配置文件.", "reload"); + this.configuration = configuration; + } + + @Override + public boolean onCommand(String[] args) { + if (configuration == null) { + return false; + } + configuration.refresh(); + return true; + } +}