diff --git a/aderyn/src/lib.rs b/aderyn/src/lib.rs index 3abe1cbc..2ee753a5 100644 --- a/aderyn/src/lib.rs +++ b/aderyn/src/lib.rs @@ -4,6 +4,9 @@ use serde_json::Value; use std::{fs::File, io::Write, path::PathBuf, str::FromStr}; use strum::IntoEnumIterator; +pub mod lsp; +mod panic; + pub fn create_aderyn_toml_file_at(directory: String) { let aderyn_toml_path = PathBuf::from_str(&directory).unwrap().join("aderyn.toml"); let mut file = File::create_new(aderyn_toml_path.clone()).expect("File already exists!"); @@ -12,15 +15,11 @@ pub fn create_aderyn_toml_file_at(directory: String) { println!("Created aderyn.toml at {}", aderyn_toml_path.display()); } -mod panic; - pub fn initialize_niceties() { // Crash with a nice message on panic panic::add_handler() } -pub mod lsp; - pub fn print_detail_view(detector_name: &str) { let all_detector_names = get_all_detectors_names(); if !all_detector_names.contains(&detector_name.to_string()) { diff --git a/aderyn/src/main.rs b/aderyn/src/main.rs index 8c1a55df..f0b857aa 100644 --- a/aderyn/src/main.rs +++ b/aderyn/src/main.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use aderyn::{ aderyn_is_currently_running_newest_version, create_aderyn_toml_file_at, initialize_niceties, lsp::spin_up_language_server, print_all_detectors_view, print_detail_view, @@ -9,18 +11,14 @@ use clap::{ArgGroup, Parser, Subcommand}; #[command(author, version, about, long_about = None)] #[command(group(ArgGroup::new("stdout_dependent").requires("stdout")))] pub struct CommandLineArgs { - /// Print every detector available - #[clap(subcommand, name = "registry")] - registry: Option, + /// Commands to initialize a config file for aderyn [BETA] and other utils + #[clap(subcommand)] + subcommand: Option, /// Foundry or Hardhat project root directory (or path to single solidity file) #[arg(default_value = ".")] root: String, - /// Initialize aderyn.toml in [ROOT] which hosts all the configuration to override defaults - #[arg(long)] - init: bool, - /// Path to the source contracts. If not provided, the ROOT directory will be used. /// /// For example, in a foundry repo: @@ -77,7 +75,7 @@ pub struct CommandLineArgs { } #[derive(Debug, Subcommand)] -enum RegistryCommand { +enum Command { /// Browse detector registry Registry { /// all - View all available detectors @@ -86,32 +84,50 @@ enum RegistryCommand { #[arg(default_value = "all")] detector: String, }, + /// Initialize aderyn.toml in the root directory or in an optional subdirectory + Init { + /// Optional path inside root where aderyn.toml will be created + path: Option, + }, } fn main() { initialize_niceties(); let cmd_args = CommandLineArgs::parse(); - if let Some(reg) = cmd_args.registry { - match reg { - RegistryCommand::Registry { detector } => { + if let Some(subcommand) = cmd_args.subcommand { + match subcommand { + Command::Registry { detector } => { if detector == "all" { print_all_detectors_view(); } else { print_detail_view(&detector); } } + Command::Init { path } => { + let creation_path = match path { + Some(optional_path) => { + let mut target_dir = PathBuf::from(&cmd_args.root); + target_dir.push(optional_path); + + let can_initialize = target_dir.exists() + && std::fs::metadata(&target_dir).is_ok_and(|p| p.is_dir()); + + if !can_initialize { + eprintln!("Failed to initialize aderyn.toml in non-existent directory"); + std::process::exit(1); + } + + target_dir.to_string_lossy().to_string() + } + None => cmd_args.root, + }; + + // Create aderyn.toml at the target directory + create_aderyn_toml_file_at(creation_path); + } } - return; - } - - if cmd_args.root == "init" { - create_aderyn_toml_file_at(".".to_string()); - return; - } - if cmd_args.init { - create_aderyn_toml_file_at(cmd_args.root); return; }