命令框架模块构建完成
This commit is contained in:
16
src/commands/db/migrate.rs
Normal file
16
src/commands/db/migrate.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub struct MigrateArgs {
|
||||
/// 目标版本
|
||||
#[arg(short, long)]
|
||||
pub version: Option<String>,
|
||||
}
|
||||
|
||||
pub fn run(args: MigrateArgs) -> Result<()> {
|
||||
match args.version {
|
||||
Some(v) => println!("🗄️ 迁移数据库到版本: {}", v),
|
||||
None => println!("🗄️ 执行最新数据库迁移"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
16
src/commands/db/mod.rs
Normal file
16
src/commands/db/mod.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
use clap::Subcommand;
|
||||
use anyhow::Result;
|
||||
|
||||
mod migrate;
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum DbCommands {
|
||||
/// 运行数据库迁移
|
||||
Migrate(migrate::MigrateArgs),
|
||||
}
|
||||
|
||||
pub fn execute(cmd: DbCommands) -> Result<()> {
|
||||
match cmd {
|
||||
DbCommands::Migrate(args) => migrate::run(args),
|
||||
}
|
||||
}
|
||||
23
src/commands/mod.rs
Normal file
23
src/commands/mod.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use clap::Subcommand;
|
||||
|
||||
// 声明子模块,对应 src/commands/ 下的目录
|
||||
pub mod server;
|
||||
pub mod db;
|
||||
// 未来扩展:pub mod new_feature;
|
||||
|
||||
/// 根级子命令枚举
|
||||
/// 每个变体对应一个子命令集(目录)
|
||||
#[derive(Subcommand)]
|
||||
pub enum CliCommands {
|
||||
/// 服务器管理相关命令 (server start, server stop)
|
||||
#[command(subcommand)]
|
||||
Server(server::ServerCommands),
|
||||
|
||||
/// 数据库管理相关命令 (db migrate, db seed)
|
||||
#[command(subcommand)]
|
||||
Db(db::DbCommands),
|
||||
|
||||
// 未来扩展示例:
|
||||
// #[command(subcommand)]
|
||||
// Auth(auth::AuthCommands),
|
||||
}
|
||||
33
src/commands/server/config/delete.rs
Normal file
33
src/commands/server/config/delete.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
// src/commands/server/config/delete.rs
|
||||
use clap::Args;
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct DeleteArgs {
|
||||
/// 要删除的配置键名
|
||||
#[arg(required = true)]
|
||||
pub key: String,
|
||||
|
||||
/// 跳过确认提示
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
pub yes: bool,
|
||||
}
|
||||
|
||||
pub fn run(args: DeleteArgs) -> Result<()> {
|
||||
if !args.yes {
|
||||
print!("⚠️ 确定要删除配置 '{}' 吗?(y/N): ", args.key);
|
||||
use std::io::{self, Write};
|
||||
io::stdout().flush().unwrap();
|
||||
|
||||
let mut input = String::new();
|
||||
io::stdin().read_line(&mut input).unwrap();
|
||||
|
||||
if !input.trim().eq_ignore_ascii_case("y") {
|
||||
println!("操作已取消。");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
println!("🗑️ 配置 '{}' 已成功删除。", args.key);
|
||||
Ok(())
|
||||
}
|
||||
28
src/commands/server/config/get.rs
Normal file
28
src/commands/server/config/get.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
// src/commands/server/config/get.rs
|
||||
use clap::Args;
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct GetArgs {
|
||||
/// 要获取的配置键名
|
||||
#[arg(required = true)]
|
||||
pub key: String,
|
||||
|
||||
/// 是否显示详细元数据
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
pub verbose: bool,
|
||||
}
|
||||
|
||||
pub fn run(args: GetArgs) -> Result<()> {
|
||||
println!("🔍 正在获取配置: {}", args.key);
|
||||
|
||||
if args.verbose {
|
||||
println!(" [INFO] 来源: 本地配置文件");
|
||||
println!(" [INFO] 类型: String");
|
||||
}
|
||||
|
||||
// 模拟返回值
|
||||
println!(" 值: \"secret_value_123\"");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
28
src/commands/server/config/mod.rs
Normal file
28
src/commands/server/config/mod.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
// src/commands/server/config/mod.rs
|
||||
use clap::Subcommand;
|
||||
use anyhow::Result;
|
||||
|
||||
// 引入具体的子命令文件
|
||||
mod get;
|
||||
mod set;
|
||||
mod delete;
|
||||
|
||||
/// 第三级子命令枚举:config 下的具体动作
|
||||
#[derive(Subcommand)]
|
||||
pub enum ConfigCommands {
|
||||
/// 获取配置项
|
||||
Get(get::GetArgs),
|
||||
/// 设置配置项
|
||||
Set(set::SetArgs),
|
||||
/// 删除配置项
|
||||
Delete(delete::DeleteArgs),
|
||||
}
|
||||
|
||||
/// Config 命令集的总入口函数
|
||||
pub fn execute(cmd: ConfigCommands) -> Result<()> {
|
||||
match cmd {
|
||||
ConfigCommands::Get(args) => get::run(args),
|
||||
ConfigCommands::Set(args) => set::run(args),
|
||||
ConfigCommands::Delete(args) => delete::run(args),
|
||||
}
|
||||
}
|
||||
30
src/commands/server/config/set.rs
Normal file
30
src/commands/server/config/set.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
// src/commands/server/config/set.rs
|
||||
use clap::Args;
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct SetArgs {
|
||||
/// 配置键名
|
||||
#[arg(required = true)]
|
||||
pub key: String,
|
||||
|
||||
/// 配置值
|
||||
#[arg(required = true)]
|
||||
pub value: String,
|
||||
|
||||
/// 立即生效而不重启服务
|
||||
#[arg(long, default_value_t = false)]
|
||||
pub hot_reload: bool,
|
||||
}
|
||||
|
||||
pub fn run(args: SetArgs) -> Result<()> {
|
||||
println!("✏️ 正在设置配置: {} = {}", args.key, args.value);
|
||||
|
||||
if args.hot_reload {
|
||||
println!(" ⚡ 热重载已触发,新配置立即生效");
|
||||
} else {
|
||||
println!(" ⚠️ 需重启服务器以应用新配置");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
33
src/commands/server/mod.rs
Normal file
33
src/commands/server/mod.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use clap::Subcommand;
|
||||
use anyhow::Result;
|
||||
|
||||
// 引入具体的命令逻辑文件
|
||||
mod start;
|
||||
mod stop;
|
||||
|
||||
// 引入新的 config 模块
|
||||
pub mod config;
|
||||
|
||||
/// Server 子命令集的具体命令
|
||||
#[derive(Subcommand)]
|
||||
pub enum ServerCommands {
|
||||
/// 启动服务器
|
||||
Start(start::StartArgs),
|
||||
/// 停止服务器
|
||||
Stop(stop::StopArgs),
|
||||
|
||||
/// 服务器配置管理(三级命令入口)
|
||||
#[command(subcommand)]
|
||||
Config(config::ConfigCommands), // 引用 config 模块中的枚举
|
||||
}
|
||||
|
||||
/// 执行 server 命令集的入口函数
|
||||
pub fn execute(cmd: ServerCommands) -> Result<()> {
|
||||
match cmd {
|
||||
ServerCommands::Start(args) => start::run(args),
|
||||
ServerCommands::Stop(args) => stop::run(args),
|
||||
|
||||
// 分发到 config 模块的 execute 函数
|
||||
ServerCommands::Config(args) => config::execute(args),
|
||||
}
|
||||
}
|
||||
20
src/commands/server/start.rs
Normal file
20
src/commands/server/start.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub struct StartArgs {
|
||||
/// 端口号
|
||||
#[arg(short, long, default_value = "8080")]
|
||||
pub port: u16,
|
||||
|
||||
/// 是否以守护进程运行
|
||||
#[arg(long)]
|
||||
pub daemon: bool,
|
||||
}
|
||||
|
||||
pub fn run(args: StartArgs) -> Result<()> {
|
||||
println!("🚀 正在启动服务器...");
|
||||
println!(" 端口: {}", args.port);
|
||||
println!(" 守护模式: {}", if args.daemon { "是" } else { "否" });
|
||||
// 在这里编写具体的启动逻辑
|
||||
Ok(())
|
||||
}
|
||||
13
src/commands/server/stop.rs
Normal file
13
src/commands/server/stop.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub struct StopArgs {
|
||||
/// 强制停止
|
||||
#[arg(short, long)]
|
||||
pub force: bool,
|
||||
}
|
||||
|
||||
pub fn run(args: StopArgs) -> Result<()> {
|
||||
println!("🛑 正在停止服务器... (force: {})", args.force);
|
||||
Ok(())
|
||||
}
|
||||
43
src/main.rs
43
src/main.rs
@@ -1,3 +1,42 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use clap::Parser;
|
||||
use anyhow::Result;
|
||||
|
||||
// 引入 commands 模块
|
||||
mod commands;
|
||||
mod utils; // 假设有通用工具
|
||||
|
||||
use commands::CliCommands;
|
||||
|
||||
/// 我的超级 CLI 工具
|
||||
#[derive(Parser)]
|
||||
#[command(name = "sunhpc")]
|
||||
#[command(author = "Qichao.Sun")]
|
||||
#[command(version = "0.1.0")]
|
||||
#[command(about = "一个可扩展的多级命令行工具框架", long_about = None)]
|
||||
struct Cli {
|
||||
/// 全局调试模式
|
||||
#[arg(short, long, global = true)]
|
||||
debug: bool,
|
||||
|
||||
/// 子命令入口
|
||||
#[command(subcommand)]
|
||||
command: CliCommands,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
if cli.debug {
|
||||
println!("[DEBUG] 调试模式已开启");
|
||||
}
|
||||
|
||||
// 根据子命令分发逻辑
|
||||
match cli.command {
|
||||
CliCommands::Server(args) => commands::server::execute(args)?,
|
||||
CliCommands::Db(args) => commands::db::execute(args)?,
|
||||
// 未来扩展新命令时,只需在这里添加新的匹配臂
|
||||
// CliCommands::NewFeature(args) => commands::new_feature::execute(args)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
0
src/utils.rs
Normal file
0
src/utils.rs
Normal file
Reference in New Issue
Block a user