use anyhow::Error;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use command::TestsType;
use fehler::throws;
mod command;
use command::ExplorerCommand;
use command::FuzzCommand;
use command::KeyPairCommand;
#[derive(Parser)]
#[clap(version, propagate_version = true)]
struct Cli {
#[clap(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Build {
#[clap(short, long)]
root: Option<String>,
},
KeyPair {
#[clap(subcommand)]
subcmd: KeyPairCommand,
},
Test {
#[clap(short, long)]
root: Option<String>,
},
Fuzz {
#[clap(short, long)]
root: Option<String>,
#[clap(subcommand)]
subcmd: FuzzCommand,
},
Localnet,
Explorer {
#[clap(subcommand)]
subcmd: ExplorerCommand,
},
Init {
#[clap(default_value = "both")]
tests_type: TestsType,
},
Clean,
}
#[throws]
pub async fn start() {
let cli = Cli::parse();
match cli.command {
Command::Build { root } => command::build(root).await?,
Command::KeyPair { subcmd } => command::keypair(subcmd)?,
Command::Test { root } => command::test(root).await?,
Command::Fuzz { root, subcmd } => command::fuzz(root, subcmd).await?,
Command::Localnet => command::localnet().await?,
Command::Explorer { subcmd } => command::explorer(subcmd).await?,
Command::Init { tests_type } => command::init(tests_type).await?,
Command::Clean => command::clean().await?,
}
}
fn _discover(target: &str) -> Result<Option<String>> {
let _cwd = std::env::current_dir()?;
let mut cwd_opt = Some(_cwd.as_path());
while let Some(cwd) = cwd_opt {
for f in std::fs::read_dir(cwd)
.with_context(|| format!("Error reading the directory with path: {}", cwd.display()))?
{
let p = f
.with_context(|| {
format!("Error reading the directory with path: {}", cwd.display())
})?
.path();
if let Some(filename) = p.file_name() {
if filename.to_str() == Some(target) {
return Ok(Some(cwd.to_string_lossy().to_string()));
}
}
}
cwd_opt = cwd.parent();
}
Ok(None)
}