feat: first commit.
This commit is contained in:
commit
fb26468175
12 files changed
+865
No files matched your search
+162
@@ -0,0 +1,162 @@
|
||||
mod migration;
|
||||
mod users;
|
||||
|
||||
use std::any::Any;
|
||||
use clap::{Command, Arg, ArgAction, ArgMatches, value_parser};
|
||||
use log::{debug, info};
|
||||
use simple_logger::SimpleLogger;
|
||||
use std::path::PathBuf;
|
||||
use rusqlite::{Connection};
|
||||
|
||||
use crate::migration::migrate;
|
||||
|
||||
|
||||
fn verbosity_arg() -> Arg {
|
||||
return Arg::new("verbosity")
|
||||
.short('v')
|
||||
.action(ArgAction::Count)
|
||||
.default_value("0");
|
||||
}
|
||||
|
||||
fn get_verbosity(m: & ArgMatches) -> log::LevelFilter {
|
||||
match m.get_one::<u8>("verbosity") {
|
||||
None => log::LevelFilter::Error,
|
||||
Some(1) => log::LevelFilter::Warn,
|
||||
Some(2) => log::LevelFilter::Info,
|
||||
Some(3) => log::LevelFilter::Debug,
|
||||
Some(4) => log::LevelFilter::Trace,
|
||||
_ => log::LevelFilter::Error,
|
||||
}
|
||||
}
|
||||
|
||||
fn db_path_arg() -> Arg {
|
||||
return Arg::new("db-path")
|
||||
.long("db-path")
|
||||
.value_name("FILE")
|
||||
.value_parser(value_parser!(PathBuf))
|
||||
.default_value( "ledger.sqlite");
|
||||
}
|
||||
|
||||
fn get_db_path(m: & ArgMatches) -> & PathBuf{
|
||||
return m.get_one::<PathBuf>("db-path").unwrap();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn verbosity() {
|
||||
{
|
||||
let cmd = Command::new("test").arg(verbosity_arg());
|
||||
let m = cmd.get_matches_from(vec!["test", "-vv"]);
|
||||
assert_eq!(get_verbosity(&m), log::LevelFilter::Info);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
let cmd = Command::new("test").arg(verbosity_arg());
|
||||
let m = cmd.get_matches_from(vec!["test"]);
|
||||
assert_eq!(get_verbosity(&m), log::LevelFilter::Error);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn db_path() {
|
||||
{
|
||||
let cmd = Command::new("test").arg(db_path_arg());
|
||||
let m = cmd.get_matches_from(vec!["test", "--db-path", "/foo/bar"]);
|
||||
let expt = PathBuf::from("/foo/bar");
|
||||
assert_eq!(get_db_path(&m), & expt);
|
||||
}
|
||||
|
||||
{
|
||||
let cmd = Command::new("test").arg(db_path_arg());
|
||||
let m = cmd.get_matches_from(vec!["test"]);
|
||||
let expt = PathBuf::from("ledger.sqlite");
|
||||
assert_eq!(get_db_path(&m), &expt);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn new_user_command_line_parser() -> Command {
|
||||
return Command::new("new")
|
||||
.long_flag("new")
|
||||
.about("Create new user.")
|
||||
.arg(Arg::new("name").help("name of new user.").action(ArgAction::Set));
|
||||
}
|
||||
|
||||
fn ls_user_command_line_parser() -> Command {
|
||||
return Command::new("list")
|
||||
.long_flag("ls")
|
||||
.about("List all users.");
|
||||
}
|
||||
|
||||
fn user_command_line_parser() -> Command {
|
||||
return Command::new("users")
|
||||
.subcommand(new_user_command_line_parser())
|
||||
.subcommand(ls_user_command_line_parser());
|
||||
}
|
||||
|
||||
|
||||
fn command_line_parser() -> Command {
|
||||
return Command::new("prog")
|
||||
.arg(verbosity_arg())
|
||||
.arg(db_path_arg())
|
||||
.subcommand(user_command_line_parser());
|
||||
}
|
||||
|
||||
fn process_new_user_command_line(conn: &Connection, m: &ArgMatches){
|
||||
debug!("Processing new user command line {:?}.", m);
|
||||
|
||||
}
|
||||
|
||||
fn process_list_user_command_line(conn: &Connection, m: &ArgMatches){
|
||||
debug!("Processing list users command line {:?}.", m);
|
||||
}
|
||||
|
||||
fn process_user_command_line(conn: &Connection, m: &ArgMatches){
|
||||
debug!("Processing user command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("new", new_user_match)) => {
|
||||
process_new_user_command_line(conn, new_user_match);
|
||||
}
|
||||
Some(("list", list_user_match)) => {
|
||||
process_list_user_command_line(conn, list_user_match);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
fn process_command_line(conn: & Connection, m: & ArgMatches) {
|
||||
debug!("Processing command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("users", user_match)) => {
|
||||
process_user_command_line(conn, user_match);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_connection(m: & ArgMatches) -> Connection {
|
||||
let path = get_db_path(&m);
|
||||
info!("Sqlite database location is {:?}.", path);
|
||||
return Connection::open(path).unwrap();
|
||||
}
|
||||
|
||||
fn set_logger(m: & ArgMatches) {
|
||||
SimpleLogger::new().with_level(get_verbosity(&m)).init().unwrap();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let command = command_line_parser();
|
||||
let m = command.get_matches();
|
||||
|
||||
set_logger(& m);
|
||||
let conn = get_connection(& m);
|
||||
migrate(&conn);
|
||||
process_command_line(& conn, & m);
|
||||
}
|
||||
Reference in new issue
Block a user