rhit/cli/
mod.rs

1pub mod args;
2mod help;
3
4use {
5    crate::*,
6    args::Args,
7    clap::Parser,
8    cli_log::*,
9    std::path::PathBuf,
10};
11
12const DEFAULT_NGINX_LOCATION: &str = "/var/log/nginx";
13static MISSING_DEFAULT_MESSAGE: &str = "\
14No nginx log found at default location, do you have nginx set up?
15If necessary, provide the path to the log file(s) as argument.
16More information with 'rhit --help'.";
17
18fn print_analysis(paths: &[PathBuf], args: &args::Args) -> Result<(), RhitError> {
19    let mut log_base = time!("LogBase::new", LogBase::new(paths, args))?;
20    let printer = md::Printer::new(args, &log_base);
21    let base = &mut log_base;
22    let trend_computer = time!("Trend computer initialization", TrendComputer::new(base, args))?;
23    md::summary::print_summary(base, &printer);
24    time!("Analysis & Printing", md::print_analysis(&log_base, &printer, trend_computer.as_ref()));
25    Ok(())
26}
27
28pub fn run() -> Result<(), RhitError> {
29    let args = Args::parse();
30    debug!("args: {:#?}", &args);
31    if args.version {
32        println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
33        return Ok(());
34    }
35    if args.help {
36        help::print();
37        return Ok(());
38    }
39    let mut paths = args.files.clone();
40    if paths.is_empty() {
41        paths.push(PathBuf::from(DEFAULT_NGINX_LOCATION));
42    }
43    let result = match args.output {
44        Output::Raw => print_raw_lines(&paths, &args),
45        Output::Tables => print_analysis(&paths, &args),
46        Output::Csv => print_csv_lines(&paths, &args),
47        Output::Json => print_json_lines(&paths, &args),
48    };
49    if let Err(RhitError::PathNotFound(ref path)) = result {
50        if path == &PathBuf::from(DEFAULT_NGINX_LOCATION) {
51            eprintln!("{}", MISSING_DEFAULT_MESSAGE);
52        }
53    }
54    log_mem(Level::Info);
55    result
56}
57