55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
//! Delete a file or empty directory.
|
|||
|
|
|
||
|
|
use crate::tools::{resolve_path, Tool, ToolCtx};
|
||
|
|
use anyhow::Result;
|
||
|
|
use serde_json::{json, Value};
|
||
|
|
use std::fs;
|
||
|
|
|
||
|
|
pub struct Delete;
|
||
|
|
|
||
|
|
impl Tool for Delete {
|
||
|
|
fn name(&self) -> &'static str {
|
||
|
|
"delete"
|
||
|
|
}
|
||
|
|
|
||
|
|
fn description(&self) -> &'static str {
|
||
|
|
"Delete a file or empty directory"
|
||
|
|
}
|
||
|
|
|
||
|
|
fn parameters(&self) -> Value {
|
||
|
|
json!({
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"path": {
|
||
|
|
"type": "string",
|
||
|
|
"description": "Path to delete (relative to workspace root)"
|
||
|
|
},
|
||
|
|
"reason": {
|
||
|
|
"type": "string",
|
||
|
|
"description": "Reason for deletion"
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"required": ["path"]
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
|
||
|
|
let rel = crate::tools::arg_str(args, "path")?;
|
||
|
|
let path = resolve_path(&ctx.workspaces, &rel)?;
|
||
|
|
|
||
|
|
if !path.exists() {
|
||
|
|
anyhow::bail!("path '{rel}' does not exist");
|
||
|
|
}
|
||
|
|
|
||
|
|
if path.is_file() {
|
||
|
|
fs::remove_file(&path)?;
|
||
|
|
Ok(format!("Deleted file '{rel}'"))
|
||
|
|
} else if path.is_dir() {
|
||
|
|
fs::remove_dir_all(&path)?;
|
||
|
|
Ok(format!("Deleted directory '{rel}' and all contents"))
|
||
|
|
} else {
|
||
|
|
anyhow::bail!("'{rel}' is neither a file nor a directory")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|