aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart/src/commands/depends.rs
blob: 91b3829ee81464c5723b940c2b642922fb7661b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use clap::Args;
use std::path::PathBuf;

#[derive(Args)]
pub struct DependsArgs {
    /// Package to inspect
    pub package: String,

    /// Recursively resolve up to the root package
    #[arg(short, long)]
    pub recursive: bool,

    /// Prints the results as a nested tree
    #[arg(short, long)]
    pub tree: bool,

    /// Read dependency information from the lock file
    #[arg(long)]
    pub locked: bool,
}

pub fn execute(
    args: &DependsArgs,
    cli: &super::Cli,
    _console: &mozart_core::console::Console,
) -> anyhow::Result<()> {
    let working_dir = match &cli.working_dir {
        Some(dir) => PathBuf::from(dir),
        None => std::env::current_dir()?,
    };

    let packages = super::dependency::load_packages(&working_dir, args.locked)?;

    if packages.is_empty() {
        println!(
            "{}",
            mozart_core::console::info("No packages found. Run `mozart install` first.")
        );
        return Ok(());
    }

    let target = args.package.to_lowercase();

    // Verify the target package is known
    let target_known = packages.iter().any(|p| p.name.to_lowercase() == target);
    if !target_known {
        anyhow::bail!(
            "Package '{}' not found in the dependency graph.",
            args.package
        );
    }

    let recursive = args.tree || args.recursive;
    let needles = vec![target];
    let results = super::dependency::get_dependents(&packages, &needles, None, false, recursive)?;

    if args.tree {
        super::dependency::print_tree(&results, 0);
    } else {
        super::dependency::print_table(&results);
    }

    Ok(())
}