From fcd1a4dc7dccc266951ee6cc4a62d380366280f3 Mon Sep 17 00:00:00 2001 From: Jia Chao Date: Wed, 14 Aug 2024 14:17:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=AF=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jia Chao --- src/analyzer/mod.rs | 5 +++-- src/config.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 19 +++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/config.rs diff --git a/src/analyzer/mod.rs b/src/analyzer/mod.rs index 1670c1b..1e6498a 100644 --- a/src/analyzer/mod.rs +++ b/src/analyzer/mod.rs @@ -9,11 +9,12 @@ use updateinfo_xmlparser::{RpmInfo, UpdateInfoDb}; use crate::analyzer::db::{CveDb, PackgeDb, SaDb}; use crate::cli::Cli; +use crate::CONFIG; lazy_static! { pub static ref update_pkgs: PackgeDb = { let mut updatedb = UpdateInfoDb::new(); - updatedb.load_xml("test/updateinfo.xml").unwrap(); + updatedb.load_xml(&CONFIG.updateinfo()).unwrap(); let mut pkgdb = PackgeDb::new(); pkgdb.load_from_updateinfodb(&updatedb); @@ -22,7 +23,7 @@ lazy_static! { pub static ref sadb: SaDb = { let mut _sadb = SaDb::new(); - _sadb.load_from_file("test/cvrf_sainfos.json").unwrap(); + _sadb.load_from_file(&CONFIG.sainfo()).unwrap(); _sadb }; pub static ref cvedb: CveDb = sadb.get_cvedb(); diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..3df4984 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,44 @@ +use serde::{Deserialize, Serialize}; + +// 可编辑的配置,一般位于 /etc/cuavrs 下 +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Config { + // 从此处更新 updateinfo, 以及 cusa 数据库 + remote: String, + + // 本地数据存储位置 + // /var/cache/cuvars + local: String, +} + +impl Config { + // 从文件中读取配置信息 + pub fn from(path: &str) -> crate::Result { + let data = std::fs::read_to_string(path)?; + + Ok(serde_json::from_str::(&data)?) + } + + // 从此地址获取更新 + pub fn remote(&self) -> &str { + &self.remote + } + + // 本地数据存储位置 + pub fn local(&self) -> &str { + &self.local + } + + pub fn update(&self) -> crate::Result<()> { + // TODO + Ok(()) + } + + pub fn updateinfo(&self) -> String { + format!("{}/updateinfo.xml", &self.local) + } + + pub fn sainfo(&self) -> String { + format!("{}/sainfos.json", &self.local) + } +} diff --git a/src/lib.rs b/src/lib.rs index bd7bc79..b54d07d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,10 @@ +use std::path::Path; + +use lazy_static::lazy_static; + pub mod cli; +pub mod config; +use config::Config; mod analyzer; @@ -9,6 +15,19 @@ pub type Error = Box; /// 定义 crate::Result pub type Result = std::result::Result; +// 首先需要读取并载入配置信息 +// 默认为 /etc/cuavrs 下 +lazy_static!{ + pub static ref CONFIG: Config = { + let default = Path::new("/etc/cuavrs/cuvat.json"); + if default.is_file() { + Config::from(default.to_str().unwrap()).unwrap() + } else { + Config::from("cuvat.json").unwrap() + } + }; +} + pub fn cumain() -> Result<()> { let cli = cli::parse(); // 初始化使用 rpm 默认配置