use alpm::{Alpm, SigLevel};
use anyhow::{Context, Result};
use pacmanconf::Config;
pub fn load_pacman_config(root: Option<&str>, config_path: Option<&str>) -> Result<Config> {
let config = if root.is_some() || config_path.is_some() {
let mut opts = Config::options();
if let Some(r) = root {
opts.root_dir(r);
}
if let Some(c) = config_path {
opts.pacman_conf(c);
}
opts.read()
} else {
Config::new()
}
.map_err(|e| anyhow::anyhow!("failed to parse pacman.conf: {e}"))?;
Ok(config)
}
pub fn init_alpm_with_opts(root: Option<&str>, config_path: Option<&str>) -> Result<Alpm> {
let config = load_pacman_config(root, config_path)?;
let handle =
Alpm::new(config.root_dir.as_str(), config.db_path.as_str()).with_context(|| {
format!(
"failed to initialize alpm at root: {} and db: {}",
config.root_dir, config.db_path
)
})?;
for repo in &config.repos {
handle
.register_syncdb(repo.name.as_str(), SigLevel::USE_DEFAULT)
.map_err(|e| anyhow::anyhow!("failed to register repo '{}': {}", repo.name, e))?;
}
Ok(handle)
}