Skip to content

Commit

Permalink
added pipe support (fixed #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
QpxDesign committed May 17, 2024
1 parent 6bf6ff5 commit ebe8ff3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
47 changes: 45 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
workspace = { members = [ "tests","tests/obfuscate_access_log_ips"] }
[package]
name = "ngxav"
version = "0.6.0"
version = "0.6.1"
edition = "2021"
license = "MIT"
description = "Search through NGINX logs with advanced filters and support for displaying analytics about your selected log entries"
readme = "README.md"
repository = "https://github.com/qpxdesign/ngxav-rs/"

[dependencies]
atty = "0.2.14"
chrono = "0.4.33"
clap = { version = "4.4.18", features = ["derive"] }
fancy-regex = "0.13.0"
Expand Down
34 changes: 26 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sort_by_date::sort_by_date;
use atty::Stream;
use clap::Parser;
use rayon::prelude::*;
use std::collections::HashMap;
Expand Down Expand Up @@ -29,6 +30,14 @@ fn read_line_by_line(filename: impl AsRef<Path>) -> io::Result<io::Lines<io::Buf
Ok(io::BufReader::new(file).lines())
}

fn load_stdin() -> Vec<String> {
let stdin = io::stdin();
return stdin
.lock()
.lines()
.map(|line| line.expect("Failed to read line"))
.collect();
}
fn main() {
let args: crate::structs::Args::ArgParser = ArgParser::parse();
if args.thread_count.is_some() {
Expand All @@ -37,10 +46,12 @@ fn main() {
.build_global()
.unwrap();
}
let file_md = metadata(args.file.clone()).unwrap();
if !args.conserve_memory.is_none() && args.conserve_memory.unwrap() == true && file_md.is_file()
if args.file.is_some()
&& !args.conserve_memory.is_none()
&& args.conserve_memory.unwrap() == true
&& metadata(args.file.clone().unwrap()).unwrap().is_file()
{
if let Ok(lines) = read_line_by_line(args.file) {
if let Ok(lines) = read_line_by_line(args.file.unwrap()) {
let mut occurrences: HashMap<String, bool> = HashMap::new();
for line in lines.flatten() {
let ip: String = line.clone().split(" ").collect::<Vec<&str>>()[0].to_string();
Expand All @@ -59,15 +70,22 @@ fn main() {
return;
}
let mut lines = Vec::new();
if file_md.is_dir() {
let stdin = load_stdin();
if args.file.is_none() && stdin.len() == 0 {
panic!("error must either pipe-in log data or provide file with -f")
}
if args.file.is_some() && metadata(args.file.clone().unwrap()).unwrap().is_dir() {
if args.conserve_memory.is_some() && args.conserve_memory.unwrap() == true {
utils::read_folder_conserve_memory::read_folder_conserve_memory(args.file, args.unique);
utils::read_folder_conserve_memory::read_folder_conserve_memory(
args.file.unwrap(),
args.unique,
);
return;
} else {
lines = utils::read_folder::read_folder(args.file);
}
} else if args.file.is_none() && stdin.len() > 0 {
lines = stdin.clone();
} else {
lines = lines_from_file(args.file).expect("should read");
lines = lines_from_file(args.file.unwrap()).expect("should read");
}
let range = sort_by_date(&lines, &args.last, &args.start_date, &args.end_date);
let mut kel: Vec<crate::structs::LineParseResult::LineParseResult> = lines[range.0..range.1]
Expand Down
2 changes: 1 addition & 1 deletion src/structs/Args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::Parser;
#[command(version, about, long_about = None)]
pub struct ArgParser {
#[arg(short = 'f', long = "file")]
pub file: String,
pub file: Option<String>,

#[arg(short = 's', long = "search")]
pub search: Option<String>,
Expand Down

0 comments on commit ebe8ff3

Please sign in to comment.