From 2fa030c3889231815fcdc395083443b9b85591c0 Mon Sep 17 00:00:00 2001 From: Jia Chao Date: Thu, 5 Sep 2024 17:56:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=90=E5=91=BD=E4=BB=A4=20auto=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8F=82=E6=95=B0=E6=8C=87=E5=AE=9A=E6=BA=90=E5=92=8C?= =?UTF-8?q?=E7=9B=AE=E6=A0=87=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jia Chao --- src/cli.rs | 10 +++++++++- src/config.rs | 5 +++++ src/lib.rs | 40 +++++++++++++++++++++++++--------------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index a8ba10b..1ac28d3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -74,4 +74,12 @@ pub fn parse() -> Cli { #[derive(Clone, Debug, Parser)] #[command(author, version, about, long_about = None)] -pub struct AutoCli; +pub struct AutoCli { + /// 指定 CVRF 公告所在的源路径 + #[arg(long, short)] + pub source: Option, + + /// 转换 CUSA 后存储的目标路径 + #[arg(long, short)] + pub target: Option, +} diff --git a/src/config.rs b/src/config.rs index 3285038..8f486da 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,11 @@ pub(crate) struct AutoConfig { } impl AutoConfig { + // 新建一个配置结构 + pub fn new(source: String, target: String) -> Self { + Self { source, target } + } + // 从文件中读取配置 pub fn from>(path: P) -> crate::Result { let data = std::fs::read_to_string(path)?; diff --git a/src/lib.rs b/src/lib.rs index 9029c8d..91bd64b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ use regex::Regex; use tracing::{debug, error, info, trace}; use tracing_subscriber::{fmt, EnvFilter}; -use config::RepairConfig; +use config::{AutoConfig, RepairConfig}; use cvrf_xmlparser::{ CVRF, // SaInfo 即为 CUSA @@ -41,7 +41,7 @@ pub fn cumain() -> Result<()> { match cli.subcommand { cli::CliSub::Convert(cli) => covert(&cli), cli::CliSub::Db(cli) => sadb(&cli), - cli::CliSub::Auto(_) => auto(), + cli::CliSub::Auto(cli) => auto(&cli), } } @@ -203,20 +203,30 @@ fn walk_dir>(path: P, nodir: bool) -> Vec { } /// 从配置文件中读取 cvrf 和 cusa 数据库的路径,并执行自动转换操作 -pub fn auto() -> Result<()> { - // 默认配置为,但也可读取执行命令路径下的配置 - let config = { - let default = Path::new("/etc/cuvars/cvrf2cusa.json"); - if default.is_file() { - default.to_str().unwrap() - } else { - "cvrf2cusa.json" - } - }; - info!("Config file is: {}", config); +pub fn auto(cli: &cli::AutoCli) -> Result<()> { + let auto = if cli.source.is_some() && cli.target.is_some() { + let auto = AutoConfig::new( + cli.source.as_ref().unwrap().to_owned(), + cli.target.as_ref().unwrap().to_owned(), + ); + info!("从参数中读取 AutoConfig: {:#?}", auto); + auto + } else { + // 默认配置为,但也可读取执行命令路径下的配置 + let config = { + let default = Path::new("/etc/cuvars/cvrf2cusa.json"); + if default.is_file() { + default.to_str().unwrap() + } else { + "cvrf2cusa.json" + } + }; + info!("Config file is: {}", config); - let auto = config::AutoConfig::from(config)?; - info!("Load AutoConfig: {:?}", auto); + let auto = config::AutoConfig::from(config)?; + info!("Load AutoConfig: {:#?}", auto); + auto + }; let files = walk_dir(auto.source(), true);