添加可配置支持

Signed-off-by: Jia Chao <jiachao2130@126.com>
This commit is contained in:
Jia Chao 2024-08-14 14:17:33 +08:00
parent 6c2b47d4f4
commit fcd1a4dc7d
3 changed files with 66 additions and 2 deletions

View File

@ -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();

44
src/config.rs Normal file
View File

@ -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<Self> {
let data = std::fs::read_to_string(path)?;
Ok(serde_json::from_str::<Self>(&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)
}
}

View File

@ -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<dyn std::error::Error + Send + Sync>;
/// 定义 crate::Result
pub type Result<T> = std::result::Result<T, Error>;
// 首先需要读取并载入配置信息
// 默认为 /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 默认配置