Skip to content

Commit

Permalink
Add rmdir command
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Mar 20, 2024
1 parent fa58930 commit c9225d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod ls;
mod mkdir;
mod pwd;
mod rm;
mod rmdir;
mod touch;
mod tree;

Expand All @@ -30,6 +31,7 @@ async fn interpret(args: &[&str], ctx: &mut Context) -> Result<()> {
"mkdir" => mkdir::invoke(args, ctx).await?,
"pwd" => pwd::invoke(args, ctx).await?,
"rm" => rm::invoke(args, ctx).await?,
"rmdir" => rmdir::invoke(args, ctx).await?,
"touch" => touch::invoke(args, ctx).await?,
"tree" => tree::invoke(args, ctx).await?,
cmd => bail!("Unrecognized command {}", cmd),
Expand Down
22 changes: 22 additions & 0 deletions src/cmd/rmdir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use anyhow::{bail, Context as _, Result};
use clap::Parser;

use crate::{context::Context, path::VirtualPathBuf};

#[derive(Parser)]
#[command(bin_name = "rmdir")]
struct Args {
#[arg(default_value = ".", help = "The directory to remove")]
path: VirtualPathBuf,
}

pub async fn invoke(args: &[&str], ctx: &mut Context) -> Result<()> {
let args = Args::try_parse_from(args)?;
let path = ctx.cwd.join(args.path);
let tree = ctx.lh.list(&path.as_lh_vec()).await.context("Not a directory, use rm instead!")?.payload;
if !tree.entries.is_empty() {
bail!("{} is not empty, use rm -r instead!", path);
}
ctx.lh.delete(&path.as_lh_vec()).await?;
Ok(())
}

0 comments on commit c9225d8

Please sign in to comment.