use clap::{CommandFactory, Parser, Subcommand}; use std::sync::Arc; use colored::Colorize; use std::path::{Path, PathBuf}; mod sync; mod install; mod config; mod info; mod group; mod delete; mod listinstalled; mod update; mod rootfs; mod parallel; mod debug; mod search; mod repo; mod progress; mod check; mod apply; mod resolver; #[derive(Parser)] #[command( name = "ror", about = "ror package manager", version = concat!(env!("CARGO_PKG_VERSION"), " MPL 2"), disable_help_subcommand = true )] struct Cli { #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { /// install one or more packages or groups #[command(alias = "i")] Install { /// package names or @group specifications to install #[arg(required = true)] packages: Vec, }, /// list available package groups #[command(alias = "lg")] ListGroups, /// synchronize repository package recipes from remote #[command(alias = "s")] Sync, /// remove an installed package #[command(alias = "d")] Delete { /// name of the package to remove package: String, }, /// list all installed packages #[command(alias = "l")] ListInstalled, /// print the total count of installed packages #[command(alias = "c")] Count, /// display detailed information about a package Info { /// name of the package package: String, }, /// update a specific package to the latest available version Update { /// name of the package to update package: String, /// check for update without making modifications #[arg(long)] dry_run: bool, }, /// upgrade all installed packages to their latest versions #[command(alias = "u")] Upgrade, /// search for packages by query in repositories Search { /// search query string query: String, }, /// add a new package repository RepoAdd { /// repository name name: String, /// repository recipe git url url: String, /// optional binary package mirror url mirror: Option, }, /// remove a configured package repository RepoRemove { /// name of the repository to remove name: String, }, /// list all configured package repositories RepoList, /// build a rootfs environment from a package group BuildRootfs { /// package group name to build #[arg(long)] group: String, /// target directory path for rootfs #[arg(long)] target: PathBuf, /// target architecture (default: native) #[arg(long, default_value = "native")] arch: String, }, /// atomically replace one package with another Di { /// package to delete delete: String, /// package to install install: String, }, /// apply declarative system configuration state #[command(alias = "a")] Apply { /// show changes without applying them #[arg(long)] dry_run: bool, /// custom system.yaml configuration file path #[arg(long)] config: Option, }, /// check integrity of installed packages Check, /// generate default configuration file at /etc/ror/ror.conf GenConfig, /// display ror version information Version, /// print this message or the help of the given subcommand Help { /// name of the subcommand subcommand: Option, }, } #[tokio::main] async fn main() { let cli = Cli::parse(); match cli.command { Commands::Help { subcommand } => { let mut cmd = Cli::command(); if let Some(sub) = subcommand { if let Some(subcmd) = cmd.find_subcommand_mut(&sub) { let _ = subcmd.print_help(); } else { let _ = cmd.print_help(); } } else { let _ = cmd.print_help(); } println!(); } Commands::Version => { println!("ror {} MPL 2", env!("CARGO_PKG_VERSION")); } Commands::GenConfig => { let config_path = Path::new("/etc/ror/ror.conf"); match config::Config::create_default_config(config_path) { Ok(()) => println!( "{} Default config created at {:?}", "[ror]".blue().bold(), config_path ), Err(e) => eprintln!( "{} Failed to create config: {}", "[ror]".red().bold(), e ), } } Commands::Sync => { let cfg = config::Config::load(); sync::run_sync(&cfg); } Commands::ListGroups => { group::list_groups(); } Commands::ListInstalled => { listinstalled::print_installed(); } Commands::Count => { listinstalled::print_count(); } Commands::Info { package } => { info::print_package_info(&package); } Commands::Delete { package } => { delete::remove_package(&package); } Commands::Search { query } => { search::search_packages(&query); } Commands::Update { package, dry_run } => { let cfg = config::Config::load(); update::update_package(&package, &cfg, dry_run); } Commands::Upgrade => { let cfg = config::Config::load(); if let Err(e) = update::upgrade_system(&cfg) { eprintln!("{} Upgrade failed: {}", "[ror]".red().bold(), e); } } Commands::RepoAdd { name, url, mirror } => { if let Err(e) = repo::add_repository(&name, &url, mirror.as_deref()) { eprintln!("{} {}", "[ror]".red().bold(), e); } } Commands::RepoRemove { name } => { if let Err(e) = repo::remove_repository(&name) { eprintln!("{} {}", "[ror]".red().bold(), e); } } Commands::RepoList => { repo::list_repositories(); } Commands::BuildRootfs { group, target, arch } => { let cfg = config::Config::load(); let arch = if arch == "native" { "native" } else { &arch }; if let Err(e) = rootfs::build_rootfs( &group, &target, &cfg, arch, ) { eprintln!("{} {}", "[ror]".red().bold(), e); } } Commands::Di { delete, install } => { let cfg = Arc::new(config::Config::load()); run_di(delete, install, cfg); } Commands::Apply { dry_run, config } => { let cfg = config::Config::load(); if let Err(e) = apply::apply_system(&cfg, dry_run, config.as_deref()) { eprintln!("{} {}", "[ror]".red().bold(), e); } } Commands::Check => { let cfg = config::Config::load(); check::check_integrity(&cfg); } Commands::Install { packages } => { let cfg = Arc::new(config::Config::load()); let db = install::InstalledDB::load(); let mut all_packages = Vec::new(); for item in packages { if let Some(group_name) = item.strip_prefix('@') { match group::load_group(group_name) { Ok(grp) => all_packages.extend(grp.packages), Err(e) => { eprintln!("{} Failed to load group '{}': {}", "[ror]".red().bold(), group_name, e); return; } } } else { all_packages.push(item); } } for pkg_name in &all_packages { let is_local = pkg_name.starts_with('/') || pkg_name.starts_with("./") || pkg_name.ends_with(".ror") || pkg_name.ends_with(".sror"); if is_local { continue; } if let Err(e) = delete::check_critical_install(pkg_name, &db) { eprintln!("{}", e); return; } } let (local_pkgs, remote_pkgs): (Vec, Vec) = all_packages .into_iter() .partition(|p| { p.starts_with('/') || p.starts_with("./") || p.ends_with(".ror") || p.ends_with(".sror") }); for path_str in &local_pkgs { install::install_local_package( std::path::Path::new(path_str), &cfg, ); } let remote_pkgs: Vec = remote_pkgs.into_iter().map(|p| { let (name, variant) = install::parse_package_spec(&p); install::resolve_package_name(&name).map(|canonical| { match variant { Some(v) => format!("{}{{{}}}", canonical, v), None => canonical, } }).unwrap_or(p) }).collect(); if !remote_pkgs.is_empty() { if let Err(e) = parallel::install_packages_parallel( &remote_pkgs, Arc::clone(&cfg), ) .await { eprintln!("{} {}", "[ror]".red().bold(), e); } } } } } fn run_di(del_pkg: String, inst_pkg: String, cfg: Arc) { let db = install::InstalledDB::load(); if !db.is_installed(&del_pkg) { eprintln!( "{} Package '{}' is not installed.", "[ror]".red().bold(), del_pkg ); return; } let del_role = delete::critical_role_of(&del_pkg); let inst_role = delete::critical_role_of(&inst_pkg); if let Some(dr) = del_role { match inst_role { None => { eprintln!( "{} '{}' provides critical role '{}' but '{}' does not. Replacement rejected.", "[ror]".red().bold(), del_pkg, dr, inst_pkg ); return; } Some(ir) if ir != dr => { eprintln!( "{} Role mismatch: '{}' is '{}' but '{}' is '{}'. Roles must match.", "[ror]".red().bold(), del_pkg, dr, inst_pkg, ir ); return; } _ => {} } } if let Err(e) = delete::check_critical_removal_with_replacement(&del_pkg, &inst_pkg, &db) { eprintln!("{}", e); return; } if let Err(e) = delete::check_critical_install(&inst_pkg, &db) { if !db.packages.contains_key(&inst_pkg) && !e.contains(&del_pkg) { eprintln!("{}", e); return; } } let record_files = db .packages .get(&del_pkg) .map(|r| r.files.clone()) .unwrap_or_default(); println!( "{} Replacing '{}' with '{}'...", "[ror]".blue().bold(), del_pkg, inst_pkg ); let installed_pkg = match install::install_package_with_result(&inst_pkg, &cfg) { Ok(pkg) => pkg, Err(e) => { eprintln!( "{} Failed to install '{}': {}. Old package remains intact.", "[ror]".red().bold(), inst_pkg, e ); return; } }; let new_files: std::collections::HashSet<_> = installed_pkg.files.iter().collect(); let obsolete_files: Vec<_> = record_files.into_iter() .filter(|f| !new_files.contains(f)) .collect(); let del_ok = if obsolete_files.is_empty() { true } else { delete::remove_package_files_only(&del_pkg, &obsolete_files) }; let mut db = install::InstalledDB::load(); if del_ok { db.packages.remove(&del_pkg); } db.add_package(installed_pkg); if let Err(e) = db.save() { eprintln!("{} Failed to save DB: {}", "[ror]".red().bold(), e); } println!( "{} Replace finished: '{}' -> '{}'", "[ror]".green().bold(), del_pkg, inst_pkg ); }