-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): allow running all directory entries (#22)
- Loading branch information
Showing
7 changed files
with
130 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use async_recursion::async_recursion; | ||
use hitt_request::send_request; | ||
|
||
use crate::{ | ||
config::CliArguments, | ||
printing::{handle_response, print_error}, | ||
}; | ||
|
||
async fn get_file_content(path: std::path::PathBuf) -> Result<String, std::io::Error> { | ||
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<bool, std::io::Error> { | ||
tokio::fs::metadata(path).await.map(|m| m.is_dir()) | ||
} | ||
|
||
pub(crate) async fn handle_file( | ||
http_client: &reqwest::Client, | ||
path: std::path::PathBuf, | ||
args: &CliArguments, | ||
) -> Result<(), std::io::Error> { | ||
println!("hitt: running {path:?}"); | ||
|
||
let fcontent = get_file_content(path).await?; | ||
|
||
for req in hitt_parser::parse_requests(&fcontent).unwrap() { | ||
match send_request(http_client, &req).await { | ||
Ok(response) => handle_response(response, args), | ||
Err(request_error) => { | ||
print_error(format!( | ||
"error sending request {} {}\n{request_error:#?}", | ||
req.method, req.uri, | ||
)); | ||
std::process::exit(1); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn handle_dir_entry( | ||
http_client: &reqwest::Client, | ||
entry: tokio::fs::DirEntry, | ||
args: &CliArguments, | ||
) -> 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" { | ||
return handle_file(http_client, entry_path, args).await; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
#[async_recursion] | ||
pub(crate) async fn handle_dir( | ||
http_client: &reqwest::Client, | ||
path: std::path::PathBuf, | ||
args: &CliArguments, | ||
) -> 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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
use crate::printing::{TEXT_RESET, TEXT_YELLOW}; | ||
|
||
use super::print_error; | ||
|
||
#[inline] | ||
pub(crate) fn print_headers(headers: &reqwest::header::HeaderMap) { | ||
for (key, value) in headers { | ||
if let Ok(value) = value.to_str() { | ||
println!("{TEXT_YELLOW}{key}{TEXT_RESET}: {value}{TEXT_RESET}"); | ||
} else { | ||
eprintln!("Error printing value for header: {key}"); | ||
print_error(format!("error printing value for header: {key}")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters