Skip to main content

zoi_cli/cmd/
files.rs

1//! Command for listing files associated with an installed package.
2
3use anyhow::{Result, anyhow};
4use colored::Colorize;
5
6use crate::pkg::{local, resolve};
7
8/// Runs the 'files' command.
9///
10/// Lists all files installed as part of the specified package.
11///
12/// # Errors
13///
14/// Returns an error if the package is not installed or if there is an issue
15/// resolving the package metadata or files.
16pub fn run(package_name: &str) -> Result<()> {
17    let (pkg_meta, _, _, _, _, _, _) =
18        resolve::resolve_package_and_version(package_name, None, false, false)?;
19
20    let installed_packages = local::get_installed_packages()?;
21
22    let Some(pkg) = installed_packages.iter().find(|p| p.name == pkg_meta.name)
23    else {
24        return Err(anyhow!("Package '{package_name}' is not installed."));
25    };
26
27    println!("Files for {} {}:", pkg.name.cyan(), pkg.version.yellow());
28
29    let version_dir = local::get_package_version_dir(
30        pkg.scope,
31        &pkg.registry_handle,
32        &pkg.repo,
33        &pkg.name,
34        &pkg.version
35    )?;
36
37    if pkg.installed_files.is_empty() {
38        println!("(No files recorded for this package)");
39    } else {
40        let mut sorted_files = pkg.installed_files.clone();
41        sorted_files.sort();
42        for file in &sorted_files {
43            let expanded = crate::pkg::utils::expand_placeholders(
44                file,
45                &version_dir,
46                pkg.scope
47            )?;
48            println!("{expanded}");
49        }
50    }
51
52    Ok(())
53}