haj 0.3.5

the beautiful, cross-distro package manager and tui.
use alpm::{Alpm, SigLevel};
use anyhow::{Context, Result};
use pacmanconf::Config;

pub fn init_alpm_with_opts(root: Option<&str>, config_path: Option<&str>) -> Result<Alpm> {
    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}"))?;

    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)
}