feat: 调整命令逻辑, 新增reload命令.
This commit is contained in:
@@ -10,6 +10,7 @@ import com.serliunx.ddns.support.InstanceContextHolder;
|
|||||||
import com.serliunx.ddns.support.SystemInitializer;
|
import com.serliunx.ddns.support.SystemInitializer;
|
||||||
import com.serliunx.ddns.support.command.CommandDispatcher;
|
import com.serliunx.ddns.support.command.CommandDispatcher;
|
||||||
import com.serliunx.ddns.support.command.target.HelpCommand;
|
import com.serliunx.ddns.support.command.target.HelpCommand;
|
||||||
|
import com.serliunx.ddns.support.command.target.ReloadCommand;
|
||||||
import com.serliunx.ddns.support.log.JLineAdaptAppender;
|
import com.serliunx.ddns.support.log.JLineAdaptAppender;
|
||||||
import org.jline.reader.LineReader;
|
import org.jline.reader.LineReader;
|
||||||
import org.jline.reader.LineReaderBuilder;
|
import org.jline.reader.LineReaderBuilder;
|
||||||
@@ -119,6 +120,8 @@ public final class ManagerLite {
|
|||||||
commandDispatcher = CommandDispatcher.getInstance();
|
commandDispatcher = CommandDispatcher.getInstance();
|
||||||
// help
|
// help
|
||||||
commandDispatcher.register(new HelpCommand());
|
commandDispatcher.register(new HelpCommand());
|
||||||
|
// reload
|
||||||
|
commandDispatcher.register(new ReloadCommand(configuration));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
package com.serliunx.ddns.support.command;
|
package com.serliunx.ddns.support.command;
|
||||||
|
|
||||||
import com.serliunx.ddns.ManagerLite;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指令调度器
|
* 指令调度器
|
||||||
@@ -26,7 +25,16 @@ public final class CommandDispatcher {
|
|||||||
/**
|
/**
|
||||||
* 最顶层指令缓存
|
* 最顶层指令缓存
|
||||||
*/
|
*/
|
||||||
private final Map<String, Command> commands = new ConcurrentHashMap<>(128);
|
private final Map<String, Command> commands = new LinkedHashMap<>(128);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有已注册的指令
|
||||||
|
*
|
||||||
|
* @return 已注册的指令
|
||||||
|
*/
|
||||||
|
public Map<String, Command> getCommands() {
|
||||||
|
return commands;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指令注册
|
* 指令注册
|
||||||
@@ -80,6 +88,16 @@ public final class CommandDispatcher {
|
|||||||
return newArgs;
|
return newArgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否存在参数
|
||||||
|
*
|
||||||
|
* @param args 参数
|
||||||
|
* @return 参数长度大于0返回真, 否则返回假
|
||||||
|
*/
|
||||||
|
public static boolean hasArgs(String[] args) {
|
||||||
|
return args.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取实例
|
* 获取实例
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,15 +2,19 @@ package com.serliunx.ddns.support.command.target;
|
|||||||
|
|
||||||
import com.serliunx.ddns.ManagerLite;
|
import com.serliunx.ddns.ManagerLite;
|
||||||
import com.serliunx.ddns.support.command.AbstractCommand;
|
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 org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.serliunx.ddns.support.command.CommandDispatcher.hasArgs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指令: help
|
* 指令: help
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
* @version 1.0.0
|
* @version 1.0.4
|
||||||
* @since 2025/1/15
|
* @since 2025/1/15
|
||||||
*/
|
*/
|
||||||
public class HelpCommand extends AbstractCommand {
|
public class HelpCommand extends AbstractCommand {
|
||||||
@@ -18,12 +22,35 @@ public class HelpCommand extends AbstractCommand {
|
|||||||
private static final Logger log = ManagerLite.getLogger();
|
private static final Logger log = ManagerLite.getLogger();
|
||||||
|
|
||||||
public HelpCommand() {
|
public HelpCommand() {
|
||||||
super("help", null, "查看帮助信息", "help cmd");
|
super("help", null, "查看帮助信息", "help <指令>");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(String[] args) {
|
public boolean onCommand(String[] args) {
|
||||||
System.out.println(Arrays.toString(args));
|
final Map<String, Command> 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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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 <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user