Skip to main content

zoi_cli/cmd/
owner.rs

1//! Implementation of the `owner` command, which finds the package that owns a
2//! given file.
3
4use std::path::Path;
5
6use anyhow::Result;
7use colored::Colorize;
8
9use crate::pkg::local;
10
11/// Runs the owner command.
12///
13/// # Errors
14///
15/// Returns an error if the path does not exist, or if there is an error
16/// querying the package database or ownership information.
17pub fn run(path: &Path) -> Result<()> {
18    let absolute_path = match path.canonicalize() {
19        Ok(p) => p,
20        Err(_) => path.to_path_buf()
21    };
22
23    println!("Querying for file: {}", absolute_path.display());
24
25    let installed_packages = local::get_installed_packages()?;
26
27    for pkg in installed_packages {
28        if pkg
29            .installed_files
30            .iter()
31            .any(|f| Path::new(f) == absolute_path)
32        {
33            println!(
34                "{} is owned by {} {}",
35                absolute_path.display(),
36                pkg.name.cyan(),
37                pkg.version.yellow()
38            );
39            return Ok(());
40        }
41    }
42
43    println!("No package owns file: {}", absolute_path.display());
44    Ok(())
45}