50 lines
1.1 KiB
Rust
50 lines
1.1 KiB
Rust
use clap::Subcommand;
|
|
|
|
use crate::{db::DbResult, ErrorExt};
|
|
|
|
/// Tools to view and manage the database.
|
|
#[derive(Subcommand)]
|
|
pub enum DbCommand {
|
|
#[command(subcommand)]
|
|
Add(AddCommand),
|
|
}
|
|
|
|
/// Insert a new entity into the database
|
|
#[derive(Subcommand)]
|
|
pub enum AddCommand {
|
|
User { username: String, password: String },
|
|
}
|
|
|
|
impl DbCommand {
|
|
pub fn run(&self, cli: &super::Cli) -> u8 {
|
|
match self {
|
|
DbCommand::Add(cmd) => cmd.run(cli),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AddCommand {
|
|
pub fn run(&self, cli: &super::Cli) -> u8 {
|
|
match self.run_err(cli) {
|
|
Ok(()) => 0,
|
|
Err(err) => {
|
|
eprintln!("An error occured: {}", err.stack());
|
|
|
|
1
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn run_err(&self, cli: &super::Cli) -> DbResult<()> {
|
|
let pool = crate::db::initialize_db(cli.data_dir.join(crate::DB_FILENAME), false)?;
|
|
|
|
match self {
|
|
Self::User { username, password } => {
|
|
crate::db::NewUser::new(username.clone(), password.clone()).insert(&pool)?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|