feat: added users command line.
This commit is contained in:
1 parent
fb26468175
commit
ded31da7a3
5 files changed
+239
-75
No files matched your search
+177
-71
@@ -1,15 +1,38 @@
|
||||
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 clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
|
||||
use serde::{Serialize};
|
||||
|
||||
use log::{debug, error, info};
|
||||
use rusqlite::Connection;
|
||||
use simple_logger::SimpleLogger;
|
||||
|
||||
use crate::migration::migrate;
|
||||
|
||||
mod migration;
|
||||
mod users;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct Output<T> {
|
||||
status: String,
|
||||
result: T,
|
||||
}
|
||||
|
||||
impl<T> Output<T> {
|
||||
fn success(shit: T) -> Output<T> {
|
||||
return Output{status: "success".to_string(), result: shit};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
trait CommandLineUserLogic {
|
||||
fn create_user(&mut self, conn: &Connection, name: &String, public_key: &String);
|
||||
|
||||
fn list_user(&mut self, conn: &Connection);
|
||||
|
||||
// fn list_securities(&mut self, conn: &Connection);
|
||||
}
|
||||
|
||||
fn verbosity_arg() -> Arg {
|
||||
return Arg::new("verbosity")
|
||||
@@ -18,7 +41,7 @@ fn verbosity_arg() -> Arg {
|
||||
.default_value("0");
|
||||
}
|
||||
|
||||
fn get_verbosity(m: & ArgMatches) -> log::LevelFilter {
|
||||
fn get_verbosity(m: &ArgMatches) -> log::LevelFilter {
|
||||
match m.get_one::<u8>("verbosity") {
|
||||
None => log::LevelFilter::Error,
|
||||
Some(1) => log::LevelFilter::Warn,
|
||||
@@ -41,10 +64,100 @@ fn get_db_path(m: & ArgMatches) -> & PathBuf{
|
||||
return m.get_one::<PathBuf>("db-path").unwrap();
|
||||
}
|
||||
|
||||
fn new_user_command_line_parser() -> Command {
|
||||
return Command::new("new")
|
||||
.long_flag("new")
|
||||
.about("Create new user.")
|
||||
.arg(Arg::new("name").long("name").help("name of new user.").action(ArgAction::Set))
|
||||
.arg(Arg::new("public_key").long("public-key").help("public key of users.").action(ArgAction::Set));
|
||||
}
|
||||
|
||||
fn ls_user_command_line_parser() -> Command {
|
||||
return Command::new("ls")
|
||||
.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, logic: &mut dyn CommandLineUserLogic) {
|
||||
debug!("Processing new user command line {:?}.", m);
|
||||
let name = m.get_one::<String>("name").unwrap();
|
||||
let pk = m.get_one::<String>("public_key").unwrap();
|
||||
logic.create_user(conn, name, pk);
|
||||
}
|
||||
|
||||
fn process_list_user_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
|
||||
debug!("Processing list users command line {:?}.", m);
|
||||
logic.list_user(conn);
|
||||
}
|
||||
|
||||
fn process_user_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
|
||||
debug!("Processing user command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("new", new_user_match)) => {
|
||||
process_new_user_command_line(conn, new_user_match, logic);
|
||||
}
|
||||
Some(("ls", list_user_match)) => {
|
||||
process_list_user_command_line(conn, list_user_match, logic);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn process_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
|
||||
debug!("Processing command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("users", user_match)) => {
|
||||
process_user_command_line(conn, user_match, logic);
|
||||
}
|
||||
_ => {
|
||||
println!("Received unparsable command. {:?}", m.subcommand());
|
||||
unreachable!();
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
struct TestCommandLineLogic {
|
||||
list_user_count: usize,
|
||||
create_user_count: usize,
|
||||
|
||||
}
|
||||
|
||||
impl TestCommandLineLogic {
|
||||
fn new() -> TestCommandLineLogic {
|
||||
return TestCommandLineLogic {
|
||||
list_user_count: 0,
|
||||
create_user_count: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandLineUserLogic for TestCommandLineLogic {
|
||||
fn create_user(&mut self, conn: &Connection, name: &String, public_key: &String) {
|
||||
self.create_user_count += 1;
|
||||
}
|
||||
|
||||
fn list_user(&mut self, conn: &Connection) {
|
||||
self.list_user_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verbosity() {
|
||||
{
|
||||
@@ -67,7 +180,7 @@ mod test {
|
||||
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);
|
||||
assert_eq!(get_db_path(&m), &expt);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -76,78 +189,70 @@ mod test {
|
||||
let expt = PathBuf::from("ledger.sqlite");
|
||||
assert_eq!(get_db_path(&m), &expt);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_user() {
|
||||
let cmd = command_line_parser();
|
||||
let m = cmd.get_matches_from(vec!["test", "users", "new", "--name", "paul", "--public-key", "123abc"]);
|
||||
let mut logic = TestCommandLineLogic::new();
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
process_command_line(&conn, &m, &mut logic);
|
||||
assert_eq!(logic.create_user_count, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_users() {
|
||||
let cmd = command_line_parser();
|
||||
let m = cmd.get_matches_from(vec!["test", "users", "ls"]);
|
||||
let mut logic = TestCommandLineLogic::new();
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
process_command_line(&conn, &m, &mut logic);
|
||||
assert_eq!(logic.list_user_count, 1);
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
struct ProCommandLineLogic {}
|
||||
|
||||
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!(),
|
||||
impl ProCommandLineLogic {
|
||||
fn new() -> ProCommandLineLogic {
|
||||
return ProCommandLineLogic {};
|
||||
}
|
||||
}
|
||||
|
||||
fn get_connection(m: & ArgMatches) -> Connection {
|
||||
impl CommandLineUserLogic for ProCommandLineLogic {
|
||||
fn create_user(&mut self, conn: &Connection, name: &String, public_key: &String) {
|
||||
users::create_user(conn, name, public_key);
|
||||
}
|
||||
|
||||
fn list_user(&mut self, conn: &Connection) {
|
||||
let users = users::list_users(conn);
|
||||
match users {
|
||||
Ok(u) => {
|
||||
let r: Vec<& users::User> = u.iter()
|
||||
.filter(|& x| x.is_ok())
|
||||
.map(| x| x.as_ref().unwrap())
|
||||
.collect();
|
||||
|
||||
let out = Output::success(r);
|
||||
|
||||
println!("{}", serde_json::to_string(& out).unwrap());
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
fn set_logger(m: &ArgMatches) {
|
||||
SimpleLogger::new().with_level(get_verbosity(&m)).init().unwrap();
|
||||
}
|
||||
|
||||
@@ -155,8 +260,9 @@ fn main() {
|
||||
let command = command_line_parser();
|
||||
let m = command.get_matches();
|
||||
|
||||
set_logger(& m);
|
||||
let conn = get_connection(& m);
|
||||
set_logger(&m);
|
||||
let conn = get_connection(&m);
|
||||
migrate(&conn);
|
||||
process_command_line(& conn, & m);
|
||||
let mut logic = ProCommandLineLogic::new();
|
||||
process_command_line(&conn, &m, &mut logic);
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@ const MIGRATION_SCRIPT: &str = "
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
key TEXT NOT NULL
|
||||
public_key TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS security_types (
|
||||
|
||||
+25
-2
@@ -1,5 +1,28 @@
|
||||
use rusqlite::Connection;
|
||||
use rusqlite::{Connection, Result};
|
||||
use serde::{Serialize};
|
||||
|
||||
fn create_user(conn: & Connection){
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct User {
|
||||
id: i32,
|
||||
name: String,
|
||||
public_key: String,
|
||||
}
|
||||
|
||||
pub fn create_user(conn: & Connection, name: &String, public_key: &String){
|
||||
conn.execute("INSERT INTO users (name, public_key) VALUES (?1, ?2)",
|
||||
(name, public_key)).unwrap();
|
||||
}
|
||||
|
||||
pub fn list_users(conn: & Connection) -> Result<Vec<Result<User>>>{
|
||||
|
||||
let mut stmt = conn.prepare("SELECT id, name, public_key FROM users")?;
|
||||
let ret: Vec<Result<User>> = stmt.query_map([], |row| {
|
||||
Ok(User {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
public_key: row.get(2)?,
|
||||
})
|
||||
}).unwrap().collect();
|
||||
return Ok(ret);
|
||||
}
|
||||
Reference in new issue
Block a user