knott 0.1.8

Fast Rust package manager helper for Arch Linux repos and the AUR
Documentation
use crate::aur::AurClient;
use crate::cli::Options;
use crate::output;
use crate::pacman::Toolchain;
use anyhow::Result;
use std::env;
use std::path::{Path, PathBuf};

pub async fn get_pkgbuilds(
    options: &Options,
    tools: &Toolchain,
    aur: &AurClient,
    targets: &[String],
    print: bool,
) -> Result<i32> {
    if targets.is_empty() {
        return Ok(1);
    }

    let packages = aur.info_map(targets).await?;
    let mut failed = false;

    for target in targets {
        let Some(pkg) = packages.get(target) else {
            output::error(format!("AUR package '{target}' was not found"));
            failed = true;
            continue;
        };

        if print {
            print!("{}", aur.raw_pkgbuild(pkg.base()).await?);
        } else {
            let dest = env::current_dir()?.join(pkg.base());
            let code = sync_aur_base(options, tools, aur, pkg.base(), &dest).await?;
            if code != 0 {
                failed = true;
            }
        }
    }

    Ok(failed as i32)
}

pub async fn sync_aur_base(
    options: &Options,
    tools: &Toolchain,
    aur: &AurClient,
    base: &str,
    dest: &Path,
) -> Result<i32> {
    tools
        .sync_git_repo(&aur.git_url(base), dest, options.dry_run)
        .await
}

pub fn build_path(tools: &Toolchain, base: &str) -> PathBuf {
    tools.build_dir.join(base)
}