diff --git a/src/cli/commands/rm.rs b/src/cli/commands/rm.rs index 2c72283..cee440a 100644 --- a/src/cli/commands/rm.rs +++ b/src/cli/commands/rm.rs @@ -12,18 +12,46 @@ pub async fn execute( lockfile_path: &Path, _config_path: &Path, ) -> Result<()> { - log::info!("Removing projects: {:?}", args.inputs); - // Load expects directory path, so get parent directory let lockfile_dir = lockfile_path.parent().unwrap_or(Path::new(".")); let mut lockfile = LockFile::load(lockfile_dir)?; + // Determine which projects to remove + let inputs: Vec = if args.all { + log::info!("Removing all projects from lockfile"); + lockfile + .projects + .iter() + .filter_map(|p| { + p.pakku_id + .clone() + .or_else(|| p.slug.values().next().cloned()) + }) + .collect() + } else { + args.inputs.clone() + }; + + if inputs.is_empty() { + return if args.all { + Err(PakkerError::ProjectNotFound( + "No projects found in lockfile".to_string(), + )) + } else { + Err(PakkerError::ProjectNotFound( + "No projects specified".to_string(), + )) + }; + } + + log::info!("Removing projects: {:?}", inputs); + let mut removed_count = 0; let mut removed_ids = Vec::new(); let mut projects_to_remove = Vec::new(); // First, identify all projects to remove - for input in &args.inputs { + for input in &inputs { // Find project by various identifiers if let Some(project) = lockfile.projects.iter().find(|p| { p.pakku_id.as_deref() == Some(input) @@ -32,7 +60,7 @@ pub async fn execute( || p.aliases.contains(input) }) { projects_to_remove.push(project.get_name()); - } else { + } else if !args.all { log::warn!("Project not found: {input}"); } } @@ -43,7 +71,8 @@ pub async fn execute( )); } - // Ask for confirmation unless --yes flag is provided + // Ask for confirmation unless --yes flag is provided or --all with no + // projects if !args.yes { println!("The following projects will be removed:"); for name in &projects_to_remove { @@ -57,7 +86,7 @@ pub async fn execute( } // Now actually remove the projects - for input in &args.inputs { + for input in &inputs { if let Some(pos) = lockfile.projects.iter().position(|p| { p.pakku_id.as_deref() == Some(input) || p.slug.values().any(|s| s == input)