From 0515e221f98566e61fe3b85d5c68e0c46bb28e79 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Tue, 16 Jan 2024 14:03:55 +0100 Subject: [PATCH] adjust bin interface with log --- members/libpt-bin/Cargo.toml | 2 +- members/libpt-bin/src/ccc/mod.rs | 28 +---------- members/libpt-bin/src/hedu/mod.rs | 84 +++++++++++++++---------------- members/libpt-bin/src/main/mod.rs | 28 +---------- 4 files changed, 46 insertions(+), 96 deletions(-) diff --git a/members/libpt-bin/Cargo.toml b/members/libpt-bin/Cargo.toml index 46700cd..8e926af 100644 --- a/members/libpt-bin/Cargo.toml +++ b/members/libpt-bin/Cargo.toml @@ -31,4 +31,4 @@ path = "src/main/mod.rs" clap = { version = "4.4.4", features = ["derive"] } clap-num = "1.0.2" clap-verbosity-flag = "2.0.1" -libpt = { version = "0.1.7", path = "../..", features = ["ccc", "math", "hedu", "net"] } +libpt = {path = "../..", features = ["default", "ccc", "math", "hedu", "net", "log"] } diff --git a/members/libpt-bin/src/ccc/mod.rs b/members/libpt-bin/src/ccc/mod.rs index 45bb35a..1ee0fe4 100644 --- a/members/libpt-bin/src/ccc/mod.rs +++ b/members/libpt-bin/src/ccc/mod.rs @@ -93,34 +93,10 @@ fn main() { } }; if cli.log_meta { - Logger::init_customized( - false, - PathBuf::from("/dev/null"), - true, - false, - true, - true, - ll, - false, - false, - false, - ) - .expect("could not initialize Logger"); + Logger::init(None, Some(ll)).expect("could not initialize Logger"); } else { // less verbose version - Logger::init_customized( - false, - PathBuf::from("/dev/null"), - true, - false, - true, - false, - ll, - false, - false, - false, - ) - .expect("could not initialize Logger"); + Logger::init_mini(Some(ll)).expect("could not initialize Logger"); } let mut expr: String = String::new(); for part in cli.expression { diff --git a/members/libpt-bin/src/hedu/mod.rs b/members/libpt-bin/src/hedu/mod.rs index a5565fa..9d17e2a 100644 --- a/members/libpt-bin/src/hedu/mod.rs +++ b/members/libpt-bin/src/hedu/mod.rs @@ -18,14 +18,18 @@ use libpt::{hedu::*, log::*}; use clap::Parser; use clap_verbosity_flag::{InfoLevel, Verbosity}; -use std::{fs::File, io::BufReader, path::PathBuf}; +use std::{ + fs::File, + io::{BufRead, BufReader, IsTerminal, Read}, + path::PathBuf, +}; //// TYPES ///////////////////////////////////////////////////////////////////////////////////////// //// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// /// short about section displayed in help const ABOUT_ROOT: &'static str = r##" -Dumps data in fancy formats +Dumps data in fancy formats. "##; /// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT) static LONG_ABOUT_ROOT: &'static str = r##" @@ -49,9 +53,13 @@ static LONG_ABOUT_ROOT: &'static str = r##" about = ABOUT_ROOT, long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT), help_template = -r#"libpt: {version}{about-section}Author: -{author-with-newline} -{usage-heading} {usage}{all-args}{tab}"# +r#"{about-section} +{usage-heading} {usage} +{all-args}{tab} + +libpt: {version} +Author: {author-with-newline} +"# )] pub struct Cli { // clap_verbosity_flag seems to make this a global option implicitly @@ -59,7 +67,7 @@ pub struct Cli { #[command(flatten)] pub verbose: Verbosity, - /// show logger meta + /// show additional logging meta data #[arg(short, long, global = true)] pub log_meta: bool, @@ -67,8 +75,10 @@ pub struct Cli { #[arg(short, long, global = true)] pub chars: bool, - /// a data source, probably a file - pub data_source: String, + /// a data source, probably a file. + /// + /// If left empty or set as "-", the program will read from stdin. + pub data_source: Option, } //// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// @@ -78,19 +88,31 @@ pub struct Cli { //// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// fn main() { let cli = cli_parse(); - debug!("Trying to open '{}'", cli.data_source); - let file = match File::open(cli.data_source.clone()) { - Ok(file) => file, - Err(err) => { - error!("Could not open file '{}': {err}", cli.data_source); - std::process::exit(1); + let source: Box; + if cli.data_source.is_some() && cli.data_source.clone().is_some_and(|val| val != "-") { + let data_source = cli.data_source.unwrap(); + debug!("Trying to open '{}'", data_source); + source = match File::open(&data_source) { + Ok(file) => Box::new(BufReader::new(file)), + Err(err) => { + error!("Could not open file '{}': {err}", data_source); + std::process::exit(1); + } + }; + } else { + debug!("Trying to open stdout"); + let stdin = std::io::stdin(); + if stdin.is_terminal() { + warn!("Refusing to dump from interactive terminal"); + std::process::exit(2) } - }; - match dump(BufReader::new(file), cli.chars) { + source = Box::new(BufReader::new(stdin)); + } + match dump(BufReader::new(source), cli.chars) { Ok(_) => (), Err(err) => { error!("Could not dump data of file: {err}"); - std::process::exit(2); + std::process::exit(3); } } } @@ -109,34 +131,10 @@ fn cli_parse() -> Cli { } }; if cli.log_meta { - Logger::init_customized( - false, - PathBuf::from("/dev/null"), - true, - false, - true, - true, - ll, - false, - false, - false, - ) - .expect("could not initialize Logger"); + Logger::init(None, Some(ll)).expect("could not initialize Logger"); } else { // less verbose version - Logger::init_customized( - false, - PathBuf::from("/dev/null"), - true, - false, - true, - false, - ll, - false, - false, - false, - ) - .expect("could not initialize Logger"); + Logger::init_mini(Some(ll)).expect("could not initialize Logger"); } return cli; } diff --git a/members/libpt-bin/src/main/mod.rs b/members/libpt-bin/src/main/mod.rs index 60618ad..9ae8ebc 100644 --- a/members/libpt-bin/src/main/mod.rs +++ b/members/libpt-bin/src/main/mod.rs @@ -51,34 +51,10 @@ pub fn main() { } }; if cli.log_meta { - Logger::init_customized( - false, - PathBuf::from("/dev/null"), - true, - false, - true, - true, - ll, - false, - false, - false, - ) - .expect("could not initialize Logger"); + Logger::init(None, Some(ll)).expect("could not initialize Logger"); } else { // less verbose version - Logger::init_customized( - false, - PathBuf::from("/dev/null"), - true, - false, - true, - false, - ll, - false, - false, - false, - ) - .expect("could not initialize Logger"); + Logger::init_mini(Some(ll)).expect("could not initialize Logger"); } trace!("started the main function");