添加 auto 子命令,用于 cuavrs 系统自动转换,专用,尚未完成

Signed-off-by: Jia Chao <jiac13@chinaunicom.cn>
This commit is contained in:
Jia Chao 2024-07-02 09:37:28 +08:00
parent 653c68b30f
commit 6a75014e83
4 changed files with 86 additions and 0 deletions

View File

@ -8,4 +8,5 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.0", features = ["derive"] } clap = { version = "4.0", features = ["derive"] }
cvrf-xmlparser = { git = "https://git.zhgsun.com:8089/jiachao2130/cvrf-xmlparser.git", version = "0.1.0" } cvrf-xmlparser = { git = "https://git.zhgsun.com:8089/jiachao2130/cvrf-xmlparser.git", version = "0.1.0" }
serde = { version = "1", features = ["serde_derive"] }
serde_json = { version = "1.0" } serde_json = { version = "1.0" }

View File

@ -17,6 +17,10 @@ pub enum CliSub {
/// 创建并生成新的 CUSA 数据文件 /// 创建并生成新的 CUSA 数据文件
Db(SaDbCli), Db(SaDbCli),
/// CUVAS 定制化自动操作,通过读取配置文件中指定的源、目标目录,执行 CUVARS
/// 数据库更新操作,配置文件为 `/etc/cuvars/cvrf2cusa.json`
Auto(AutoCli),
} }
/// ConvertCli 用于将指定的 cvrf 格式 xml 文件转换为 cusa 格式 /// ConvertCli 用于将指定的 cvrf 格式 xml 文件转换为 cusa 格式
@ -67,3 +71,7 @@ pub struct SaDbCli {
pub fn parse() -> Cli { pub fn parse() -> Cli {
Cli::parse() Cli::parse()
} }
#[derive(Clone, Debug, Parser)]
#[command(author, version, about, long_about = None)]
pub struct AutoCli;

56
src/config.rs Normal file
View File

@ -0,0 +1,56 @@
use serde::{Deserialize, Serialize};
/// cvrf2cusa 自动化执行所需的配置
#[derive(Clone, Debug, Deserialize, Serialize)]
pub(crate) struct AutoConfig {
// 对应cuavrs-db-cvrf
source: String,
// cuavrs-db-sa
target: String,
}
impl AutoConfig {
pub fn from(path: &str) -> crate::Result<Self> {
let data = std::fs::read_to_string(path)?;
Ok(serde_json::from_str::<Self>(&data)?)
}
}
/// cuvars 中,关于源码追踪,自动修复相关的配置项,一般位于组件最底层的目录,`config.json`
#[derive(Clone, Debug, Deserialize, Serialize)]
pub(crate) struct RepairConfig {
// 对应了 openEuelr 的上游版本
upstream: String,
// 此组件是否支持自动修复
autobuild: bool,
// 当前已修复的最新版本号
fixed_version: String,
}
impl RepairConfig {
/// 从指定的路径读取文件并将之转换为 `RepairConfig`
pub fn read(path: &str) -> crate::Result<Self> {
let data = std::fs::read_to_string(path)?;
Ok(serde_json::from_str::<Self>(&data)?)
}
/// 获取对应的上游代码分支
pub fn upstream(&self) -> &str {
&self.upstream
}
/// 此组件是否支持自动构建发布
pub fn autobuild(&self) -> bool {
self.autobuild
}
/// 已修复的最新代码版本
pub fn fixed_version(&self) -> &str {
&self.fixed_version
}
}

View File

@ -11,6 +11,8 @@ use cvrf_xmlparser::{
pub mod cli; pub mod cli;
mod config;
/// 定义 crate::Error /// 定义 crate::Error
/// 大部分函数返回的错误 /// 大部分函数返回的错误
pub type Error = Box<dyn std::error::Error + Send + Sync>; pub type Error = Box<dyn std::error::Error + Send + Sync>;
@ -24,6 +26,7 @@ pub fn cumain() -> Result<()> {
match cli.subcommand { match cli.subcommand {
cli::CliSub::Convert(cli) => covert(&cli), cli::CliSub::Convert(cli) => covert(&cli),
cli::CliSub::Db(cli) => sadb(&cli), cli::CliSub::Db(cli) => sadb(&cli),
cli::CliSub::Auto(_) => auto(),
} }
} }
@ -163,3 +166,21 @@ fn walk_dir<P: AsRef<Path>>(path: P, nodir: bool) -> Vec<PathBuf>
res res
} }
/// 从配置文件中读取 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"
}
};
let auto = config::AutoConfig::from(config)?;
todo!();
Ok(())
}