add rpm: get_installed_package()
Signed-off-by: Jia Chao <jiachao2130@126.com>
This commit is contained in:
parent
11a6ca013a
commit
bea65e445a
24
src/lib.rs
24
src/lib.rs
|
@ -1,14 +1,16 @@
|
|||
pub fn add(left: usize, right: usize) -> usize {
|
||||
left + right
|
||||
}
|
||||
use std::sync::Once;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use librpm::config;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
// use librpm
|
||||
pub mod rpm;
|
||||
|
||||
// 使用 Once 对配置进行一次性地初始化
|
||||
static CONFIGURE: Once = Once::new();
|
||||
|
||||
// 使用系统的默认配置
|
||||
pub fn configure() {
|
||||
CONFIGURE.call_once(|| {
|
||||
config::read_file(None).unwrap();
|
||||
});
|
||||
}
|
||||
|
|
37
src/rpm.rs
Normal file
37
src/rpm.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use librpm::db;
|
||||
use librpm::Package;
|
||||
|
||||
// 获取当前系统已安装的所有 RPM 包
|
||||
// 最后返回一个 map,其键为包名,因有多个版本可并存的软件包如 kernel,故其值为 `Vec<Package>`
|
||||
pub fn get_installed_packages() -> HashMap<String, Vec<Package>> {
|
||||
let mut installed: HashMap<String, Vec<Package>> = HashMap::new();
|
||||
db::installed_packages().for_each(|pkg| {
|
||||
if let Some(rpms) = installed.get_mut(pkg.name()) {
|
||||
rpms.push(pkg.clone());
|
||||
} else {
|
||||
installed.insert(pkg.name().into(), vec![pkg.clone()]);
|
||||
}
|
||||
});
|
||||
|
||||
installed
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn get_installed_pkgs() {
|
||||
crate::configure();
|
||||
|
||||
let pkgs = get_installed_packages();
|
||||
|
||||
if let Some(_) = pkgs.get("kernel") {
|
||||
assert!(true);
|
||||
} else {
|
||||
assert_eq!("", "Package kernel not installed?");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user