separate cli definition into its own file
This commit is contained in:
parent
530dd8dc15
commit
22d16ff5ba
|
@ -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"
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -15,3 +15,4 @@ pub mod old;
|
|||
pub mod interactive;
|
||||
pub mod env;
|
||||
pub mod io;
|
||||
pub mod cli;
|
||||
|
|
Loading…
Reference in New Issue