Skip to content

Commit

Permalink
Implements init subcommand with optional path for aderyn.toml (#762)
Browse files Browse the repository at this point in the history
Co-authored-by: mgiagante <[email protected]>
Co-authored-by: TilakMaddy <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2024
1 parent f00f678 commit 47792bf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
7 changes: 3 additions & 4 deletions aderyn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand All @@ -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()) {
Expand Down
56 changes: 36 additions & 20 deletions aderyn/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<RegistryCommand>,
/// Commands to initialize a config file for aderyn [BETA] and other utils
#[clap(subcommand)]
subcommand: Option<Command>,

/// 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:
Expand Down Expand Up @@ -77,7 +75,7 @@ pub struct CommandLineArgs {
}

#[derive(Debug, Subcommand)]
enum RegistryCommand {
enum Command {
/// Browse detector registry
Registry {
/// all - View all available detectors
Expand All @@ -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<String>,
},
}

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;
}

Expand Down

0 comments on commit 47792bf

Please sign in to comment.