Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): ignore gitignored files #35

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hitt-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
14 changes: 11 additions & 3 deletions hitt-cli/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
config::RunCommandArguments,
fs::{handle_dir, handle_file, is_directory},
fs::{find_http_files, handle_file},
terminal::print_error,
};

Expand All @@ -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!(
Expand Down
70 changes: 20 additions & 50 deletions hitt-cli/src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_recursion::async_recursion;
use hitt_request::send_request;

use crate::{
Expand All @@ -7,13 +6,9 @@ use crate::{
};

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())
tokio::fs::read(path)
.await
.map(|buf| String::from_utf8_lossy(&buf).to_string())
}

pub(crate) async fn handle_file(
Expand Down Expand Up @@ -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<std::path::PathBuf> {
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()
}
3 changes: 1 addition & 2 deletions hitt-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down