Going somewhere.

This commit is contained in:
didier amyot committed 2023-02-25 12:04:40 -05:00
1 parent 9f86d53d11
commit 0bbe716d8d
9 files changed
+160 -59

No files matched your search

+38 -31
View File
@@ -1,7 +1,7 @@
use std::path::PathBuf;
use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
use clap::parser::ValuesRef;
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use log::{debug, error, info};
use rusqlite::Connection;
use serde::Serialize;
@@ -11,7 +11,7 @@ use db::securities::list_securities;
use crate::db::users::{create_user, list_users};
use crate::migration::apply_migrations;
use crate::util::{CustomResult, Error};
use crate::util::{CustomResult, Error, SQLiteConnectionFactory};
use crate::webui::web_ui_main;
mod db;
@@ -57,7 +57,7 @@ trait CommandLineUserLogic {
fn run_webui(
&mut self,
conn: &mut Connection,
conn: SQLiteConnectionFactory,
port: u16,
enable_template_autoreload: bool,
) -> CustomResult<()>;
@@ -251,14 +251,14 @@ fn process_process_events_command_line(
}
fn process_run_wui_command_line(
conn: &mut Connection,
connFact: SQLiteConnectionFactory,
m: &ArgMatches,
logic: &mut dyn CommandLineUserLogic,
) -> CustomResult<()> {
debug!("Processing run wui command line {:?}.", m);
let port = m.get_one::<u16>("port").unwrap_or(&2222);
let enable_template_autoreload = m.get_flag("enable-template-autoreload");
logic.run_webui(conn, *port, enable_template_autoreload)
logic.run_webui(connFact, *port, enable_template_autoreload)
}
fn process_securities_command_line(
@@ -290,30 +290,31 @@ fn process_events_command_line(
}
fn process_wui_command_line(
conn: &mut Connection,
connFact: SQLiteConnectionFactory,
m: &ArgMatches,
logic: &mut dyn CommandLineUserLogic,
) -> CustomResult<()> {
debug!("Processing wui command line {:?}.", m);
match m.subcommand() {
Some(("run", run_wui_match)) => process_run_wui_command_line(conn, run_wui_match, logic),
Some(("run", run_wui_match)) => process_run_wui_command_line(connFact, run_wui_match, logic),
_ => unreachable!(),
}
}
fn process_command_line(
conn: &mut Connection,
connFact: SQLiteConnectionFactory,
m: &ArgMatches,
logic: &mut dyn CommandLineUserLogic,
) -> CustomResult<()> {
debug!("Processing command line {:?}.", m);
let mut conn = connFact.connection()?;
match m.subcommand() {
Some(("users", user_match)) => process_user_command_line(conn, user_match, logic),
Some(("users", user_match)) => process_user_command_line(&mut conn, user_match, logic),
Some(("securities", securities_match)) => {
process_securities_command_line(conn, securities_match, logic)
process_securities_command_line(&mut conn, securities_match, logic)
}
Some(("events", events_match)) => process_events_command_line(conn, events_match, logic),
Some(("wui", wui_match)) => process_wui_command_line(conn, wui_match, logic),
Some(("events", events_match)) => process_events_command_line(&mut conn, events_match, logic),
Some(("wui", wui_match)) => process_wui_command_line(connFact, wui_match, logic),
_ => {
println!("Received un parsable command. {:?}", m.subcommand());
unreachable!();
@@ -377,7 +378,7 @@ mod cli_test {
fn run_webui(
&mut self,
_conn: &mut Connection,
_conn: SQLiteConnectionFactory,
_port: u16,
_enable_template_autoreload: bool,
) -> CustomResult<()> {
@@ -419,7 +420,7 @@ mod cli_test {
}
#[test]
fn create_user() {
fn create_user() -> CustomResult<()> {
let cmd = command_line_parser();
let m = cmd.get_matches_from(vec![
"test",
@@ -432,48 +433,53 @@ mod cli_test {
]);
let mut logic = TestCommandLineLogic::new();
let mut conn = Connection::open_in_memory().unwrap();
process_command_line(&mut conn, &m, &mut logic);
process_command_line(SQLiteConnectionFactory::inMemory(), &m, &mut logic)?;
assert_eq!(logic.create_user_count, 1);
Ok(())
}
#[test]
fn list_users() {
fn list_users() -> CustomResult<()> {
let cmd = command_line_parser();
let m = cmd.get_matches_from(vec!["test", "users", "ls"]);
let mut logic = TestCommandLineLogic::new();
let mut conn = Connection::open_in_memory().unwrap();
process_command_line(&mut conn, &m, &mut logic);
process_command_line(SQLiteConnectionFactory::inMemory(), &m, &mut logic)?;
assert_eq!(logic.list_user_count, 1);
Ok(())
}
#[test]
fn list_securities() {
fn list_securities() -> CustomResult<()> {
let cmd = command_line_parser();
let m = cmd.get_matches_from(vec!["test", "securities", "ls"]);
let mut logic = TestCommandLineLogic::new();
let mut conn = Connection::open_in_memory().unwrap();
process_command_line(&mut conn, &m, &mut logic);
process_command_line(SQLiteConnectionFactory::inMemory(), &m, &mut logic)?;
assert_eq!(logic.list_securities_count, 1);
Ok(())
}
#[test]
fn process_events() {
fn process_events() -> CustomResult<()> {
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);
process_command_line(SQLiteConnectionFactory::inMemory(), &m, &mut logic)?;
assert_eq!(logic.process_events_count, 1);
Ok(())
}
#[test]
fn run_web_ui() {
fn run_web_ui() -> CustomResult<()> {
let cmd = command_line_parser();
let m = cmd.get_matches_from(vec!["test", "wui", "run"]);
let mut logic = TestCommandLineLogic::new();
let mut conn = Connection::open_in_memory().unwrap();
process_command_line(&mut conn, &m, &mut logic);
process_command_line(SQLiteConnectionFactory::inMemory(), &m, &mut logic)?;
assert_eq!(logic.run_webui_count, 1);
Ok(())
}
}
@@ -522,19 +528,20 @@ impl CommandLineUserLogic for ProCommandLineLogic {
fn run_webui(
&mut self,
conn: &mut Connection,
connFact: SQLiteConnectionFactory,
port: u16,
enable_template_autoreload: bool,
) -> CustomResult<()> {
info!("Serving web-ui on port {:?}", port);
web_ui_main(conn, port, enable_template_autoreload)
web_ui_main(connFact, port, enable_template_autoreload)
}
}
fn get_connection(m: &ArgMatches) -> Connection {
fn get_connection(m: &ArgMatches) -> SQLiteConnectionFactory {
let path = get_db_path(&m);
info!("Sqlite database location is {:?}.", path);
return Connection::open(path).unwrap();
SQLiteConnectionFactory::new(path.clone())
}
fn set_logger(m: &ArgMatches) {
@@ -544,14 +551,14 @@ fn set_logger(m: &ArgMatches) {
.unwrap();
}
fn main() {
fn main() -> CustomResult<()> {
let command = command_line_parser();
let m = command.get_matches();
set_logger(&m);
let mut conn = get_connection(&m);
let connFactory = get_connection(&m);
apply_migrations(&mut conn).unwrap();
apply_migrations(&mut connFactory.connection()?).unwrap();
let mut logic = ProCommandLineLogic::new();
process_command_line(&mut conn, &m, &mut logic);
process_command_line(connFactory, &m, &mut logic)
}