feat(core): Add subcommand to remove projects
This commit is contained in:
parent
ef191c6a40
commit
04edeb9216
40
src/main.rs
40
src/main.rs
@ -4,7 +4,7 @@ mod tui;
|
||||
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{ensure, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
@ -43,6 +43,9 @@ enum Command {
|
||||
/// Add a project
|
||||
Add { path: PathBuf },
|
||||
|
||||
/// Remove a project
|
||||
Remove { path: PathBuf },
|
||||
|
||||
/// List existing projects
|
||||
List,
|
||||
|
||||
@ -93,6 +96,9 @@ fn handle_subcommands(mut projects: Projects, cmd: &Command) -> Result<()> {
|
||||
Command::Add { path } => {
|
||||
add_project(&mut projects, path)?;
|
||||
}
|
||||
Command::Remove { path } => {
|
||||
remove_project(&mut projects, path)?;
|
||||
}
|
||||
Command::List => {
|
||||
list_projects(&projects)?;
|
||||
}
|
||||
@ -129,6 +135,38 @@ where
|
||||
}
|
||||
|
||||
|
||||
fn remove_project<P>(projects: &mut Projects, path: P) -> Result<()>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let path = path.as_ref();
|
||||
|
||||
ensure!(
|
||||
projects.list.contains(&path.to_path_buf()),
|
||||
"Project path not in registry"
|
||||
);
|
||||
|
||||
let idx =
|
||||
projects.list.iter().enumerate().find_map(
|
||||
|(idx, elem)| {
|
||||
if elem == path {
|
||||
Some(idx)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
if let Some(idx) = idx {
|
||||
let proj = projects.list.remove(idx);
|
||||
write_projects_file(projects)?;
|
||||
println!("Removed {}", proj.display());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn list_projects(projects: &Projects) -> Result<()> {
|
||||
for project in &projects.list {
|
||||
println!("{project:?}")
|
||||
|
Loading…
Reference in New Issue
Block a user