diff --git a/crates/cli/src/command.rs b/crates/cli/src/command.rs index 790a4cb7..5a6f6729 100644 --- a/crates/cli/src/command.rs +++ b/crates/cli/src/command.rs @@ -18,3 +18,6 @@ pub use explorer::{explorer, ExplorerCommand}; mod init; pub use init::init; + +mod clean; +pub use clean::clean; diff --git a/crates/cli/src/command/clean.rs b/crates/cli/src/command/clean.rs new file mode 100644 index 00000000..a08f7e9f --- /dev/null +++ b/crates/cli/src/command/clean.rs @@ -0,0 +1,9 @@ +use anyhow::Error; +use fehler::throws; +use trdelnik_client::Cleaner; + +#[throws] +pub async fn clean() { + let cleaner = Cleaner::new(); + cleaner.clean_target().await?; +} diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index aef06f2e..4b21d49a 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -7,6 +7,7 @@ mod command; // bring nested subcommand enums into scope use command::ExplorerCommand; use command::FuzzCommand; + use command::KeyPairCommand; #[derive(Parser)] @@ -56,6 +57,8 @@ enum Command { #[arg(short, long)] skip_fuzzer: bool, }, + /// Removes target contents except for KeyPair and removes hfuzz_target folder + Clean, } #[throws] @@ -70,5 +73,6 @@ pub async fn start() { Command::Localnet => command::localnet().await?, Command::Explorer { subcmd } => command::explorer(subcmd).await?, Command::Init { skip_fuzzer } => command::init(skip_fuzzer).await?, + Command::Clean => command::clean().await?, } } diff --git a/crates/client/src/cleaner.rs b/crates/client/src/cleaner.rs new file mode 100644 index 00000000..ff753338 --- /dev/null +++ b/crates/client/src/cleaner.rs @@ -0,0 +1,59 @@ +use crate::config::Config; +use fehler::{throw, throws}; +use std::{ + io, + path::{Path, PathBuf}, +}; +use thiserror::Error; +use tokio::{fs, process::Command}; + +#[derive(Error, Debug)] +pub enum Error { + #[error("{0:?}")] + Io(#[from] io::Error), + #[error("Cannot find the Anchor.toml file to locate the root folder")] + BadWorkspace, +} + +pub struct Cleaner; + +impl Default for Cleaner { + fn default() -> Self { + Self::new() + } +} + +impl Cleaner { + pub fn new() -> Self { + Self + } + #[throws] + pub async fn clean_target(&self) { + let root = match Config::discover_root() { + Ok(root) => root, + Err(_) => throw!(Error::BadWorkspace), + }; + self.clean_anchor_target().await?; + self.clean_hfuzz_target(&root).await?; + } + + #[throws] + async fn clean_anchor_target(&self) { + Command::new("anchor").arg("clean").spawn()?.wait().await?; + } + #[throws] + async fn clean_hfuzz_target(&self, root: &PathBuf) { + let hfuzz_target_path = Path::new(root) + .join(crate::test_generator::TESTS_WORKSPACE) + .join(crate::test_generator::HFUZZ_TARGET); + if hfuzz_target_path.exists() { + fs::remove_dir_all(hfuzz_target_path).await?; + } else { + println!( + "skipping {}/{} directory: not found", + crate::test_generator::TESTS_WORKSPACE, + crate::test_generator::HFUZZ_TARGET + ) + } + } +} diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 1fbb4e5d..e4e992f2 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -69,3 +69,6 @@ pub use test_generator::TestGenerator; pub mod error_reporter; pub use error_reporter::*; + +pub mod cleaner; +pub use cleaner::*; diff --git a/crates/client/src/test_generator.rs b/crates/client/src/test_generator.rs index 97435ca6..a82a261b 100644 --- a/crates/client/src/test_generator.rs +++ b/crates/client/src/test_generator.rs @@ -22,6 +22,7 @@ const TESTS_DIRECTORY: &str = "tests"; const FUZZ_DIRECTORY: &str = "src/bin"; const TESTS_FILE_NAME: &str = "test.rs"; const FUZZ_TEST_FILE_NAME: &str = "fuzz_target.rs"; +pub(crate) const HFUZZ_TARGET: &str = "hfuzz_target"; #[derive(Error, Debug)] pub enum Error {