diff --git a/hitt-cli/Cargo.toml b/hitt-cli/Cargo.toml index fefce4c..6ad70d5 100644 --- a/hitt-cli/Cargo.toml +++ b/hitt-cli/Cargo.toml @@ -9,9 +9,9 @@ name = "hitt" path = "src/main.rs" [dependencies] -async-recursion = "1.0.5" clap = { version = "4.3.24", features = ["derive"] } console = "0.15.7" +ignore = "0.4.20" hitt-formatter = { path = "../hitt-formatter" } hitt-parser = { path = "../hitt-parser" } hitt-request = { path = "../hitt-request" } diff --git a/hitt-cli/src/commands/run.rs b/hitt-cli/src/commands/run.rs index 5e1a8d5..043086c 100644 --- a/hitt-cli/src/commands/run.rs +++ b/hitt-cli/src/commands/run.rs @@ -1,6 +1,6 @@ use crate::{ config::RunCommandArguments, - fs::{handle_dir, handle_file, is_directory}, + fs::{find_http_files, handle_file}, terminal::print_error, }; @@ -10,8 +10,16 @@ pub(crate) async fn run_command(args: &RunCommandArguments) -> Result<(), std::i // TODO: figure out a way to remove this clone let cloned_path = args.path.clone(); - match is_directory(&args.path).await { - Ok(true) => handle_dir(&http_client, cloned_path, args).await, + match std::fs::metadata(&args.path).map(|metadata| metadata.is_dir()) { + Ok(true) => { + let http_files = find_http_files(&args.path); + + for http_file in http_files { + handle_file(&http_client, http_file, args).await?; + } + + Ok(()) + } Ok(false) => handle_file(&http_client, cloned_path, args).await, Err(io_error) => { print_error(format!( diff --git a/hitt-cli/src/fs/mod.rs b/hitt-cli/src/fs/mod.rs index 84b55fd..d819cbc 100644 --- a/hitt-cli/src/fs/mod.rs +++ b/hitt-cli/src/fs/mod.rs @@ -1,4 +1,3 @@ -use async_recursion::async_recursion; use hitt_request::send_request; use crate::{ @@ -7,13 +6,9 @@ use crate::{ }; async fn get_file_content(path: std::path::PathBuf) -> Result { - let buffr = tokio::fs::read(path).await?; - - Ok(String::from_utf8_lossy(&buffr).to_string()) -} - -pub(crate) async fn is_directory(path: &std::path::Path) -> Result { - tokio::fs::metadata(path).await.map(|m| m.is_dir()) + tokio::fs::read(path) + .await + .map(|buf| String::from_utf8_lossy(&buf).to_string()) } pub(crate) async fn handle_file( @@ -41,48 +36,23 @@ pub(crate) async fn handle_file( Ok(()) } -async fn handle_dir_entry( - http_client: &reqwest::Client, - entry: tokio::fs::DirEntry, - args: &RunCommandArguments, -) -> Result<(), std::io::Error> { - let metadata = entry.metadata().await?; - - match metadata.is_dir() { - true => handle_dir(http_client, entry.path(), args).await, - false => { - let entry_path = entry.path(); - - if let Some(ext) = entry_path.extension() { - if ext == "http" || ext == "rest" { - return handle_file(http_client, entry_path, args).await; +pub(crate) fn find_http_files(path: &std::path::Path) -> Vec { + ignore::WalkBuilder::new(path) + .build() + .filter_map(|orginal_entry| { + if let Ok(entry) = orginal_entry { + let path = entry.path(); + + if let Some(ext) = path.extension() { + if ext == "http" { + return Some(path.to_path_buf()); + } } - } - - Ok(()) - } - } -} -#[async_recursion] -pub(crate) async fn handle_dir( - http_client: &reqwest::Client, - path: std::path::PathBuf, - args: &RunCommandArguments, -) -> Result<(), std::io::Error> { - if !args.recursive { - print_error(format!( - "{path:?} is a directory, but the recursive argument was not supplied" - )); - - std::process::exit(1); - } - - let mut read_dir_result = tokio::fs::read_dir(&path).await?; - - while let Some(entry) = read_dir_result.next_entry().await? { - handle_dir_entry(http_client, entry, args).await?; - } - - Ok(()) + None + } else { + None + } + }) + .collect() } diff --git a/hitt-cli/src/main.rs b/hitt-cli/src/main.rs index afe4fb8..8cfa5f1 100644 --- a/hitt-cli/src/main.rs +++ b/hitt-cli/src/main.rs @@ -1,6 +1,5 @@ use clap::Parser; -use commands::new::new_command; -use commands::run::run_command; +use commands::{new::new_command, run::run_command}; use config::{Cli, Commands}; mod commands;