zoi-cli 1.25.2

Advanced Package Manager & Environment Orchestrator
Documentation
//! Command for listing files associated with an installed package.

use anyhow::{Result, anyhow};
use colored::Colorize;

use crate::pkg::{local, resolve};

/// Runs the 'files' command.
///
/// Lists all files installed as part of the specified package.
///
/// # Errors
///
/// Returns an error if the package is not installed or if there is an issue
/// resolving the package metadata or files.
pub fn run(package_name: &str) -> Result<()> {
    let (pkg_meta, _, _, _, _, _, _) =
        resolve::resolve_package_and_version(package_name, None, false, false)?;

    let installed_packages = local::get_installed_packages()?;

    let Some(pkg) = installed_packages.iter().find(|p| p.name == pkg_meta.name)
    else {
        return Err(anyhow!("Package '{package_name}' is not installed."));
    };

    println!("Files for {} {}:", pkg.name.cyan(), pkg.version.yellow());

    let version_dir = local::get_package_version_dir(
        pkg.scope,
        &pkg.registry_handle,
        &pkg.repo,
        &pkg.name,
        &pkg.version
    )?;

    if pkg.installed_files.is_empty() {
        println!("(No files recorded for this package)");
    } else {
        let mut sorted_files = pkg.installed_files.clone();
        sorted_files.sort();
        for file in &sorted_files {
            let expanded = crate::pkg::utils::expand_placeholders(
                file,
                &version_dir,
                pkg.scope
            )?;
            println!("{expanded}");
        }
    }

    Ok(())
}