From 4e36fb32e681aba868007d1e33296810c973c427 Mon Sep 17 00:00:00 2001 From: rasul Date: Thu, 31 Oct 2019 17:18:00 -0500 Subject: [PATCH] get some command line options --- Cargo.lock | 16 +++++++++++ Cargo.toml | 1 + src/config.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 17 ++++++------ 4 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 80feb87..e491224 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,6 +178,14 @@ name = "fuchsia-cprng" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -346,6 +354,7 @@ dependencies = [ "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", @@ -391,6 +400,11 @@ dependencies = [ "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-width" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.2.0" @@ -450,6 +464,7 @@ dependencies = [ "checksum failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f8273f13c977665c5db7eb2b99ae520952fe5ac831ae4cd09d80c4c7042b5ed9" "checksum failure_derive 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0bc225b78e0391e4b8683440bf2e63c2deeeb2ce5189eab46e2b68c6d3725d08" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +"checksum getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" @@ -475,6 +490,7 @@ dependencies = [ "checksum synstructure 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f085a5855930c0441ca1288cf044ea4aecf4f43a91668abdb870b4ba546a203" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum toml 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c7aabe75941d914b72bf3e5d3932ed92ce0664d49d8432305a8b547c37227724" +"checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/Cargo.toml b/Cargo.toml index cff2cc8..124a0b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" chrono = "0.4" colored = "1.8" dirs = "2.0" +getopts = "0.2" log = "0.4" serde = "1.0" serde_derive = "1.0" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..3d3b3a9 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,74 @@ +use std::env; +use std::path::PathBuf; + +use dirs::config_dir; +use getopts::Options; +use log::LevelFilter; + +#[derive(Debug)] +pub struct Config { + pub config_directory: PathBuf, + pub log_level: LevelFilter, +} + +impl Config { + pub fn from_args() -> Config { + let args: Vec = env::args().collect(); + let program = args[0].clone(); + + let mut opts = Options::new(); + opts.optopt("c", "config", "config directory", "CONFIG"); + opts.optopt( + "l", + "level", + "log level (error, warn, info, debug, trace)", + "LEVEL", + ); + opts.optflag("h", "help", "print this help menu"); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(e) => { + println!("FATAL: Error parsing arguments: {:?}", e); + std::process::exit(1); + } + }; + + if matches.opt_present("h") { + let brief = format!("Usage: {} [options]", program); + print!("{}", opts.usage(&brief)); + std::process::exit(0); + } + + let config_directory = if let Some(dir) = matches.opt_str("c") { + PathBuf::from(dir) + } else if let Some(mut dir) = config_dir() { + dir.push("sup"); + dir + } else { + PathBuf::from(".") + }; + + let mut log_level: LevelFilter = LevelFilter::Info; + + if let Some(raw_level) = matches.opt_str("l") { + log_level = match raw_level.as_str() { + "error" => LevelFilter::Error, + "warn" => LevelFilter::Warn, + "info" => LevelFilter::Info, + "debug" => LevelFilter::Debug, + "trace" => LevelFilter::Trace, + _ => { + println!("FATAL: unknown log level: {}", raw_level); + println!("Options are error, warn, info, debug, or trace"); + std::process::exit(1); + } + }; + }; + + Config { + config_directory, + log_level, + } + } +} diff --git a/src/main.rs b/src/main.rs index 882a4df..6ab7190 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate chrono; extern crate colored; extern crate dirs; +extern crate getopts; #[macro_use] extern crate log; extern crate serde; @@ -10,31 +11,29 @@ extern crate toml; mod app; mod apps; +mod config; mod logger; mod proc; mod result; use std::path::PathBuf; -use dirs::config_dir; - use apps::Apps; +use config::Config; use proc::Proc; use result::Result; fn main() { + let config = Config::from_args(); + if let Err(e) = logger::init() { println!("FATAL: Error initializing logger: {:?}", e); std::process::exit(1); } + logger::set_level(config.log_level); - let apps_file: PathBuf = match config_dir() { - Some(mut d) => { - d.push("sup.toml"); - d - } - None => PathBuf::from("sup.toml"), - }; + let mut apps_file = config.config_directory.clone(); + apps_file.push("sup.toml"); match startup(apps_file) { Ok(p) => {