ud 0.1.0

UpDate! — A fast, universal dependency updater.
Documentation
use clap::{Parser, Subcommand};
use owo_colors::OwoColorize;
use std::path::PathBuf;
use ud::core::model::Verdict;
use ud::core::{HumanReporter, JsonReporter, RunOptions, UpdateOptions, run, update};
use ud::ecosystems;

#[derive(Parser)]
#[command(name = "ud")]
#[command(version, about = "Up to Date — universal dependency updater")]
#[command(
    long_about = "ud checks your manifest for outdated dependencies and optionally updates them.\n\n\
    Exit codes:\n  \
    0  All dependencies are current (or successfully updated)\n  \
    1  Outdated dependencies detected (check mode)\n  \
    2  Hard error (missing file, parse failure, network error)"
)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Path to the manifest file or a directory containing one (defaults to current directory)
    path: Option<PathBuf>,

    /// Update the manifest file losslessly with compatible versions
    #[arg(short = 'u', long)]
    update: bool,

    /// Also apply breaking version bumps when updating (requires --update)
    #[arg(long = "allow-breaking")]
    allow_breaking: bool,

    /// Include prerelease versions
    #[arg(long = "pre")]
    pre: bool,

    /// Output the report as JSON
    #[arg(long)]
    json: bool,

    /// Enable verbose logging
    #[arg(short = 'v', long)]
    verbose: bool,
}

#[derive(Subcommand)]
enum Commands {
    /// List all dependencies with their status
    Tree {
        /// Path to the manifest file or directory (defaults to current directory)
        path: Option<PathBuf>,
    },
}

#[tokio::main]
async fn main() {
    match run_cli().await {
        Ok(has_outdated) => {
            if has_outdated {
                std::process::exit(1);
            }
        }
        Err(e) => {
            eprintln!("{} {}", "error:".red().bold(), e);
            std::process::exit(2);
        }
    }
}

async fn run_cli() -> miette::Result<bool> {
    let cli = Cli::parse();
    ud::core::runtime::init_tracing(cli.verbose);

    let (input_path, is_tree) = match cli.command {
        Some(Commands::Tree { path }) => (path.unwrap_or_else(|| PathBuf::from(".")), true),
        None => (cli.path.unwrap_or_else(|| PathBuf::from(".")), false),
    };

    let ecosystems_list = ecosystems::all();

    let should_update = cli.update && !is_tree;

    if !cli.update && !is_tree && !cli.json {
        eprintln!(
            "{}",
            "Check mode: no changes will be made"
                .bright_black()
                .italic()
        );
    }

    let report = if should_update {
        let opts = UpdateOptions {
            allow_breaking: cli.allow_breaking,
            include_prerelease: cli.pre,
        };
        update(&input_path, &ecosystems_list, &opts)
            .await
            .map_err(|e| miette::miette!("{}", e))?
    } else {
        let opts = RunOptions {
            include_prerelease: cli.pre,
        };
        run(&input_path, &ecosystems_list, &opts)
            .await
            .map_err(|e| miette::miette!("{}", e))?
    };

    let mut has_outdated = false;
    if !cli.update || is_tree {
        for manifest in &report.manifests {
            for (_, verdict) in &manifest.verdicts {
                if matches!(verdict, Verdict::Outdated { .. }) {
                    has_outdated = true;
                    break;
                }
            }
        }
    }

    if cli.json {
        let output = JsonReporter::render(&report)
            .map_err(|e| miette::miette!("JSON serialization error: {}", e))?;
        println!("{}", output);
    } else {
        let output = HumanReporter::render(&report, is_tree, should_update, &ecosystems_list);
        print!("{}", output);
    }

    Ok(has_outdated)
}