Skip to main content

zoi_cli/cmd/package/
doctor.rs

1//! Implementation of the `package doctor` command, which validates package
2//! specification files.
3
4use std::path::PathBuf;
5
6use anyhow::{Result, anyhow};
7use clap::Parser;
8use colored::Colorize;
9
10/// Arguments for the `package doctor` command.
11#[derive(Parser, Debug)]
12pub struct DoctorCommand {
13    /// Path to the package file (e.g. path/to/name.pkg.lua)
14    #[arg(required = true)]
15    pub package_file: PathBuf,
16
17    /// Validate as this target platform (defaults to current platform)
18    #[arg(long)]
19    pub platform: Option<String>,
20
21    /// Override package version while validating
22    #[arg(long)]
23    pub version_override: Option<String>
24}
25
26/// Runs the `package doctor` command.
27///
28/// # Errors
29///
30/// Returns an error if the doctor check fails.
31pub fn run(args: &DoctorCommand) -> Result<()> {
32    println!(
33        "{} Running package doctor for {}",
34        "::".bold().blue(),
35        args.package_file.display()
36    );
37
38    let report = crate::pkg::package::doctor::run(
39        &args.package_file,
40        args.platform.as_deref(),
41        args.version_override.as_deref()
42    )?;
43
44    for error in &report.errors {
45        eprintln!("{} {}", "Error:".red().bold(), error);
46    }
47
48    for warning in &report.warnings {
49        println!("{} {}", "Warning:".yellow().bold(), warning);
50    }
51
52    if report.errors.is_empty() {
53        println!(
54            "{} package doctor completed (warnings: {}).",
55            "::".bold().green(),
56            report.warnings.len()
57        );
58        Ok(())
59    } else {
60        Err(anyhow!(
61            "package doctor found {} error(s)",
62            report.errors.len()
63        ))
64    }
65}