Skip to content

Commit

Permalink
Eldritch File.Remove (#21)
Browse files Browse the repository at this point in the history
* File.Remove Added docs, testing, and impl.
  • Loading branch information
hulto authored Apr 2, 2022
1 parent decef24 commit e22c508
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Generated by Cargo
# will have compiled files and executables
/target/
cmd/implants/eldritch/target/
cmd/implants/target/
cmd/implants/Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
Expand Down Expand Up @@ -29,4 +32,4 @@ build/**
.stats/**

# Credentials
.creds/**
.creds/**
2 changes: 1 addition & 1 deletion docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The <b>file.read</b> method will read the contents of a file. If the file or dir
### file.remove
`file.remove(path: str) -> None`

The <b>file.remove</b> method is very cool, and will be even cooler when Nick documents it.
The <b>file.remove</b> method will delete a file or directory (and it's contents) specified by path.

### file.rename
`file.rename(src: str, dst: str) -> None`
Expand Down
50 changes: 47 additions & 3 deletions implants/eldritch/src/file/remove_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
use anyhow::Result;
use std::path::Path;
use std::fs;

pub fn remove(_path: String) -> Result<()> {
unimplemented!("Method unimplemented")
}
pub fn remove(path: String) -> Result<()> {
let res = Path::new(&path);
if res.is_file() {
fs::remove_file(path)?;
} else if res.is_dir() {
fs::remove_dir_all(path)?;
}
Ok(())
}


#[cfg(test)]
mod tests {
use super::*;
use tempfile::{NamedTempFile,tempdir};

#[test]
fn remove_file() -> anyhow::Result<()> {
// Create file
let tmp_file = NamedTempFile::new()?;
let path = String::from(tmp_file.path().to_str().unwrap()).clone();

// Run our code
remove(path.clone())?;

// Verify that file has been removed
let res = Path::new(&path).exists();
assert_eq!(res, false);
Ok(())
}
#[test]
fn remove_dir() -> anyhow::Result<()> {
// Create dir
let tmp_dir = tempdir()?;
let path = String::from(tmp_dir.path().to_str().unwrap());

// Run our code
remove(path.clone())?;

// Verify that file has been removed
let res = Path::new(&path).exists();
assert_eq!(res, false);
Ok(())
}
}

0 comments on commit e22c508

Please sign in to comment.