fuck off, I call that a day.

This commit is contained in:
didier amyot committed 2022-12-23 17:58:30 -05:00
1 parent 04d4d1b7a9
commit cb5805a829
12 files changed
+229 -86

No files matched your search

+85 -13
View File
@@ -1,19 +1,23 @@
use std::path::PathBuf;
use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
use log::{debug, info};
use clap::parser::ValuesRef;
use log::{debug, error, info};
use rusqlite::Connection;
use serde::Serialize;
use simple_logger::SimpleLogger;
use crate::migration::apply_migrations;
use db::securities::list_securities;
use crate::db::users::{create_user, list_users};
use crate::event_processor::process_event_files;
use crate::migration::apply_migrations;
mod migration;
mod db;
mod services;
mod util;
mod event_processor;
#[derive(Debug, Serialize)]
struct Output<T> {
@@ -33,6 +37,8 @@ trait CommandLineUserLogic {
fn list_user(&mut self, conn: &Connection);
fn list_securities(&mut self, conn: &Connection);
fn process_events(&mut self, conn: &mut Connection, files: ValuesRef<PathBuf>);
}
fn verbosity_arg() -> Arg {
@@ -58,10 +64,10 @@ fn db_path_arg() -> Arg {
.long("db-path")
.value_name("FILE")
.value_parser(value_parser!(PathBuf))
.default_value( "ledger.sqlite");
.default_value("ledger.sqlite");
}
fn get_db_path(m: & ArgMatches) -> & PathBuf{
fn get_db_path(m: &ArgMatches) -> &PathBuf {
return m.get_one::<PathBuf>("db-path").unwrap();
}
@@ -79,10 +85,28 @@ fn ls_user_command_line_parser() -> Command {
.about("List all users.");
}
fn process_events_command_line_parser() -> Command {
Command::new("process")
.about("Process events one after the other. In the order received")
.arg_required_else_help(true)
.arg(clap::arg!(<PATH> ... "Stuff to add")
.value_parser(value_parser!(PathBuf)))
}
fn user_command_line_parser() -> Command {
return Command::new("users")
.about("Handy command about users.")
.subcommand(new_user_command_line_parser())
.subcommand(ls_user_command_line_parser());
.subcommand(ls_user_command_line_parser())
.arg_required_else_help(true);
}
fn event_command_line_parser() -> Command {
return Command::new("events")
.about("Process and query events files")
.subcommand(process_events_command_line_parser())
.arg_required_else_help(true);
}
fn ls_securities_command_line_parser() -> Command {
@@ -93,15 +117,18 @@ fn ls_securities_command_line_parser() -> Command {
fn securities_command_line_parser() -> Command {
return Command::new("securities")
.subcommand(ls_securities_command_line_parser());
.subcommand(ls_securities_command_line_parser())
.arg_required_else_help(true);
}
fn command_line_parser() -> Command {
return Command::new("prog")
.arg(verbosity_arg())
.arg(db_path_arg())
.subcommand(event_command_line_parser())
.subcommand(user_command_line_parser())
.subcommand(securities_command_line_parser());
.subcommand(securities_command_line_parser())
.arg_required_else_help(true);
}
fn process_new_user_command_line(conn: &mut Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
@@ -134,6 +161,17 @@ fn process_list_securities_command_line(conn: &Connection, m: &ArgMatches, logic
logic.list_securities(conn);
}
fn process_process_events_command_line(conn: &mut Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
debug!("Processing process event command line {:?}.", m);
let mut files = m.get_many::<PathBuf>("PATH").unwrap();
if files.any(|x| !x.exists()) {
error!("Some passed files do not exists. Existing right now.");
return;
}
logic.process_events(conn, files);
}
fn process_securities_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
debug!("Processing securities command line {:?}.", m);
match m.subcommand() {
@@ -144,6 +182,16 @@ fn process_securities_command_line(conn: &Connection, m: &ArgMatches, logic: &mu
}
}
fn process_events_command_line(conn: &mut Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
debug!("Processing events command line {:?}.", m);
match m.subcommand() {
Some(("process", process_events_match)) => {
process_process_events_command_line(conn, process_events_match, logic);
}
_ => unreachable!(),
}
}
fn process_command_line(conn: &mut Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
debug!("Processing command line {:?}.", m);
match m.subcommand() {
@@ -154,10 +202,13 @@ fn process_command_line(conn: &mut Connection, m: &ArgMatches, logic: &mut dyn C
Some(("securities", securities_match)) => {
process_securities_command_line(conn, securities_match, logic);
}
Some(("events", events_match)) => {
process_events_command_line(conn, events_match, logic);
}
_ => {
println!("Received unparsable command. {:?}", m.subcommand());
println!("Received un parsable command. {:?}", m.subcommand());
unreachable!();
},
}
}
}
@@ -169,6 +220,7 @@ mod cli_test {
list_user_count: usize,
create_user_count: usize,
list_securities_count: usize,
process_events_count: usize,
}
impl TestCommandLineLogic {
@@ -177,6 +229,7 @@ mod cli_test {
list_user_count: 0,
create_user_count: 0,
list_securities_count: 0,
process_events_count: 0,
};
}
}
@@ -193,6 +246,10 @@ mod cli_test {
fn list_securities(&mut self, _conn: &Connection) {
self.list_securities_count += 1;
}
fn process_events(&mut self, conn: &mut Connection, _files: ValuesRef<PathBuf>) {
self.process_events_count += 1;
}
}
#[test]
@@ -257,6 +314,16 @@ mod cli_test {
process_command_line(&mut conn, &m, &mut logic);
assert_eq!(logic.list_securities_count, 1);
}
#[test]
fn process_events() {
let cmd = command_line_parser();
let m = cmd.get_matches_from(vec!["test", "events", "process", "/dev/null"]);
let mut logic = TestCommandLineLogic::new();
let mut conn = Connection::open_in_memory().unwrap();
process_command_line(&mut conn, &m, &mut logic);
assert_eq!(logic.process_events_count, 1);
}
}
struct ProCommandLineLogic {}
@@ -270,7 +337,7 @@ impl ProCommandLineLogic {
impl CommandLineUserLogic for ProCommandLineLogic {
fn create_user(&mut self, conn: &mut Connection, name: &String, public_key: &String) {
let tx = conn.transaction().unwrap();
create_user(& tx, name, public_key).unwrap();
create_user(&tx, name, public_key).unwrap();
tx.commit().unwrap();
}
@@ -287,20 +354,25 @@ impl CommandLineUserLogic for ProCommandLineLogic {
}
}
fn list_securities(&mut self, conn: & Connection) {
fn list_securities(&mut self, conn: &Connection) {
let securities = list_securities(conn);
match securities {
Ok(s) => {
let out = Output::success(s);
println!("{}", serde_json::to_string(&out).unwrap());
},
}
Err(e) => {
println!("{:?}", e);
}
};
}
}
fn process_events(&mut self, conn: &mut Connection, files: ValuesRef<PathBuf>) {
process_event_files(conn, files);
println!("fuck me");
}
}
fn get_connection(m: &ArgMatches) -> Connection {
let path = get_db_path(&m);