feat: started db cli tool; switched to i64 ids
This commit is contained in:
parent
b343fbccea
commit
1f4b0c35c5
10 changed files with 86 additions and 29 deletions
49
src/cli/db.rs
Normal file
49
src/cli/db.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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(())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,12 @@
|
|||
mod db;
|
||||
mod serve;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
/// Otter is a lightweight implementation of the Gpodder API, designed to be used for small
|
||||
/// personal deployments.
|
||||
#[derive(Parser)]
|
||||
pub struct Cli {
|
||||
#[arg(
|
||||
|
|
@ -21,12 +24,15 @@ pub struct Cli {
|
|||
#[derive(Subcommand)]
|
||||
pub enum Command {
|
||||
Serve(serve::ServeCommand),
|
||||
#[command(subcommand)]
|
||||
Db(db::DbCommand),
|
||||
}
|
||||
|
||||
impl Cli {
|
||||
pub fn run(&self) -> u8 {
|
||||
match &self.cmd {
|
||||
Command::Serve(cmd) => cmd.run(self),
|
||||
Command::Db(cmd) => cmd.run(self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@ use clap::Args;
|
|||
|
||||
use crate::{db, server};
|
||||
|
||||
const DB_FILENAME: &str = "otter.sqlite3";
|
||||
|
||||
/// Run the Otter web server
|
||||
#[derive(Args)]
|
||||
pub struct ServeCommand {
|
||||
#[arg(
|
||||
|
|
@ -31,7 +30,7 @@ impl ServeCommand {
|
|||
|
||||
tracing::info!("Initializing database and running migrations");
|
||||
|
||||
let pool = db::initialize_db(cli.data_dir.join(DB_FILENAME), true).unwrap();
|
||||
let pool = db::initialize_db(cli.data_dir.join(crate::DB_FILENAME), true).unwrap();
|
||||
|
||||
let ctx = server::Context { pool };
|
||||
let app = server::app().with_state(ctx);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue