From 22d16ff5ba96c1d640da06709219bf21cd3143ad Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Sat, 11 Mar 2023 19:29:09 -0600 Subject: [PATCH] separate cli definition into its own file --- Cargo.toml | 3 +- src/bin/t.rs | 80 +++++++++++++++++++++++++++++++++++++++ src/{main.rs => cli.rs} | 84 +++-------------------------------------- src/lib.rs | 1 + 4 files changed, 88 insertions(+), 80 deletions(-) create mode 100644 src/bin/t.rs rename src/{main.rs => cli.rs} (79%) diff --git a/Cargo.toml b/Cargo.toml index ff839fe..b7f6e60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [[bin]] name = "t" -path = "src/main.rs" +path = "src/bin/t.rs" [package] name = "tiempo" @@ -12,6 +12,7 @@ license = "GPL-3.0" documentation = "https://tiempo.categulario.xyz" homepage = "https://gitlab.com/categulario/tiempo-rs" version = "1.6.0" +default-run = "t" [dependencies] thiserror = "1" diff --git a/src/bin/t.rs b/src/bin/t.rs new file mode 100644 index 0000000..92b7522 --- /dev/null +++ b/src/bin/t.rs @@ -0,0 +1,80 @@ +use std::convert::TryInto; +use std::process::exit; +use std::io; + +use clap::ArgMatches; +use chrono::Utc; +use regex::Regex; +use lazy_static::lazy_static; + +use tiempo::error; +use tiempo::database::SqliteDatabase; +use tiempo::env::Env; +use tiempo::config::Config; +use tiempo::commands::{ + Command, Facts, r#in::InCommand, display::DisplayCommand, + sheet::SheetCommand, today::TodayCommand, yesterday::YesterdayCommand, + week::WeekCommand, month::MonthCommand, list::ListCommand, out::OutCommand, + resume::ResumeCommand, backend::BackendCommand, kill::KillCommand, + now::NowCommand, edit::EditCommand, archive::ArchiveCommand, + configure::ConfigureCommand, +}; +use tiempo::io::Streams; +use tiempo::cli::make_cli; + +lazy_static! { + // https://regex101.com/r/V9zYQu/1/ + pub static ref NUMBER_RE: Regex = Regex::new(r"^\d+$").unwrap(); +} + +fn error_trap(matches: ArgMatches) -> error::Result<()> { + let env = Env::read(); + let facts = Facts { + config: Config::read(env.timetrap_config_file.as_deref())?, + env, + now: Utc::now(), + }; + + if let Some(_matches) = matches.subcommand_matches("backend") { + return BackendCommand::handle(&facts.config); + } + + let mut streams = Streams { + db: SqliteDatabase::from_path_or_create(&facts.config.database_file)?, + r#in: io::BufReader::new(io::stdin()), + out: io::stdout(), + err: io::stderr(), + }; + + match matches.subcommand() { + Some(("in", matches)) => InCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("out", matches)) => OutCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("resume", matches)) => ResumeCommand::handle(matches.try_into()?, &mut streams, &facts), + + Some(("display", matches)) => DisplayCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("today", matches)) => TodayCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("yesterday", matches)) => YesterdayCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("week", matches)) => WeekCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("month", matches)) => MonthCommand::handle(matches.try_into()?, &mut streams, &facts), + + Some(("sheet", matches)) => SheetCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("list", matches)) => ListCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("kill", matches)) => KillCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("now", matches)) => NowCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("edit", matches)) => EditCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("archive", matches)) => ArchiveCommand::handle(matches.try_into()?, &mut streams, &facts), + Some(("configure", matches)) => ConfigureCommand::handle(matches.try_into()?, &mut streams, &facts), + + Some((cmd, _)) => Err(error::Error::UnimplementedCommand(cmd.into())), + None => Err(error::Error::MissingSubcommand), + } +} + +fn main() { + let matches = make_cli().get_matches(); + + if let Err(e) = error_trap(matches) { + eprintln!("{}", e); + exit(1); + } +} diff --git a/src/main.rs b/src/cli.rs similarity index 79% rename from src/main.rs rename to src/cli.rs index 7bcc37a..41cbaca 100644 --- a/src/main.rs +++ b/src/cli.rs @@ -1,77 +1,8 @@ -use std::convert::TryInto; -use std::process::exit; -use std::io; - use clap::{ - Arg, SubCommand, ArgMatches, command, value_parser, + Command, Arg, SubCommand, command, value_parser, }; -use chrono::Utc; -use regex::Regex; -use lazy_static::lazy_static; -use tiempo::error; -use tiempo::database::SqliteDatabase; -use tiempo::env::Env; -use tiempo::config::Config; -use tiempo::commands::{ - Command, Facts, r#in::InCommand, display::DisplayCommand, - sheet::SheetCommand, today::TodayCommand, yesterday::YesterdayCommand, - week::WeekCommand, month::MonthCommand, list::ListCommand, out::OutCommand, - resume::ResumeCommand, backend::BackendCommand, kill::KillCommand, - now::NowCommand, edit::EditCommand, archive::ArchiveCommand, - configure::ConfigureCommand, -}; -use tiempo::io::Streams; - -lazy_static! { - // https://regex101.com/r/V9zYQu/1/ - pub static ref NUMBER_RE: Regex = Regex::new(r"^\d+$").unwrap(); -} - -fn error_trap(matches: ArgMatches) -> error::Result<()> { - let env = Env::read(); - let facts = Facts { - config: Config::read(env.timetrap_config_file.as_deref())?, - env, - now: Utc::now(), - }; - - if let Some(_matches) = matches.subcommand_matches("backend") { - return BackendCommand::handle(&facts.config); - } - - let mut streams = Streams { - db: SqliteDatabase::from_path_or_create(&facts.config.database_file)?, - r#in: io::BufReader::new(io::stdin()), - out: io::stdout(), - err: io::stderr(), - }; - - match matches.subcommand() { - Some(("in", matches)) => InCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("out", matches)) => OutCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("resume", matches)) => ResumeCommand::handle(matches.try_into()?, &mut streams, &facts), - - Some(("display", matches)) => DisplayCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("today", matches)) => TodayCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("yesterday", matches)) => YesterdayCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("week", matches)) => WeekCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("month", matches)) => MonthCommand::handle(matches.try_into()?, &mut streams, &facts), - - Some(("sheet", matches)) => SheetCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("list", matches)) => ListCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("kill", matches)) => KillCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("now", matches)) => NowCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("edit", matches)) => EditCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("archive", matches)) => ArchiveCommand::handle(matches.try_into()?, &mut streams, &facts), - Some(("configure", matches)) => ConfigureCommand::handle(matches.try_into()?, &mut streams, &facts), - - Some((cmd, _)) => Err(error::Error::UnimplementedCommand(cmd.into())), - None => Err(error::Error::MissingSubcommand), - } -} - -fn main() { +pub fn make_cli() -> Command<'static> { // Let's first declare some args that repeat here and there let start_arg = Arg::with_name("start") .long("start").short('s') @@ -127,7 +58,7 @@ fn main() { .help("Choose an entry of the (unique) last N interactively"); // Now declar this app's cli - let matches = command!() + let cli = command!() .subcommand_required(true) .subcommand(SubCommand::with_name("archive") @@ -369,12 +300,7 @@ fn main() { .value_name("NOTE") .help("The note text. It will replace the previous one unless --append is given") ) - ) + ); - .get_matches(); - - if let Err(e) = error_trap(matches) { - eprintln!("{}", e); - exit(1); - } + cli } diff --git a/src/lib.rs b/src/lib.rs index 0b9757d..c61b93c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,3 +15,4 @@ pub mod old; pub mod interactive; pub mod env; pub mod io; +pub mod cli;