use clap::{Parser, Subcommand};
use owo_colors::OwoColorize;
use std::path::PathBuf;
use ud::core::contract::Ecosystem;
use ud::core::model::Verdict;
use ud::core::pipeline::Pipeline;
use ud::core::report::{HumanReporter, JsonReporter};
use ud::ecosystems::cargo::CargoEcosystem;
#[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: Option<PathBuf>,
#[arg(short = 'u', long)]
update: bool,
#[arg(long = "allow-breaking")]
allow_breaking: bool,
#[arg(long = "pre")]
pre: bool,
#[arg(long)]
json: bool,
#[arg(short = 'v', long)]
verbose: bool,
}
#[derive(Subcommand)]
enum Commands {
Tree {
path: Option<PathBuf>,
},
}
#[tokio::main]
async fn main() {
match run().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() -> 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 manifest_path = if input_path.is_dir() {
let cargo_toml = input_path.join("Cargo.toml");
if cargo_toml.exists() {
cargo_toml
} else {
return Err(miette::miette!(
"file not found: {}/Cargo.toml",
input_path.display()
));
}
} else if !input_path.exists() {
return Err(miette::miette!("file not found: {}", input_path.display()));
} else {
input_path
};
let should_update = cli.update && !is_tree;
if !cli.update && !is_tree {
println!(
"{}",
"Check mode: no changes will be made"
.bright_black()
.italic()
);
}
let mut pipeline = Pipeline::new().with_prerelease(cli.pre);
pipeline.register(Box::new(CargoEcosystem::new()));
let report = pipeline.run(&manifest_path).await?;
let mut has_outdated = false;
if should_update {
let mut edits = Vec::new();
for (dep, verdict) in &report.verdicts {
if let Verdict::Outdated {
compatible, latest, ..
} = verdict
{
has_outdated = true;
let target = if cli.allow_breaking {
Some(latest)
} else {
compatible.as_ref()
};
if let Some(version) = target {
edits.push((dep, version));
}
}
}
if !edits.is_empty() {
let content = tokio::fs::read_to_string(&manifest_path)
.await
.map_err(|e| miette::miette!("could not read manifest: {}", e))?;
let eco = CargoEcosystem::new();
let new_content = eco.write_batch(&content, &edits).await?;
tokio::fs::write(&manifest_path, new_content)
.await
.map_err(|e| miette::miette!("could not write manifest: {}", e))?;
}
} else {
for (_, verdict) in &report.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);
print!("{}", output);
}
Ok(has_outdated)
}