zoi-cli 1.25.2

Advanced Package Manager & Environment Orchestrator
Documentation
//! Implementation of the `owner` command, which finds the package that owns a
//! given file.

use std::path::Path;

use anyhow::Result;
use colored::Colorize;

use crate::pkg::local;

/// Runs the owner command.
///
/// # Errors
///
/// Returns an error if the path does not exist, or if there is an error
/// querying the package database or ownership information.
pub fn run(path: &Path) -> Result<()> {
    let absolute_path = match path.canonicalize() {
        Ok(p) => p,
        Err(_) => path.to_path_buf()
    };

    println!("Querying for file: {}", absolute_path.display());

    let installed_packages = local::get_installed_packages()?;

    for pkg in installed_packages {
        if pkg
            .installed_files
            .iter()
            .any(|f| Path::new(f) == absolute_path)
        {
            println!(
                "{} is owned by {} {}",
                absolute_path.display(),
                pkg.name.cyan(),
                pkg.version.yellow()
            );
            return Ok(());
        }
    }

    println!("No package owns file: {}", absolute_path.display());
    Ok(())
}