feat(cli): implemented an arg to list all acounts
This commit is contained in:
parent
8c388e6329
commit
e1c90a0484
|
@ -82,7 +82,7 @@ impl Backend {
|
||||||
))?;
|
))?;
|
||||||
statement.next()?;
|
statement.next()?;
|
||||||
|
|
||||||
let raw_hash = &statement.read::<String, _>("password").unwrap();
|
let raw_hash = &statement.read::<String, _>("password")?;
|
||||||
|
|
||||||
let hash = PasswordHash::new(raw_hash)
|
let hash = PasswordHash::new(raw_hash)
|
||||||
.map_err(|_| anyhow::Error::msg("Failed to parse the password hash"))?;
|
.map_err(|_| anyhow::Error::msg("Failed to parse the password hash"))?;
|
||||||
|
@ -114,10 +114,23 @@ impl Backend {
|
||||||
username
|
username
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
info!("Deleted `{}`)", username);
|
info!("Deleted `{}`", username);
|
||||||
Ok(Response::Delete(ResponseDelete::Success))
|
Ok(Response::Delete(ResponseDelete::Success))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn list(&mut self) -> Result<Response> {
|
||||||
|
let mut list = Vec::new();
|
||||||
|
for row in self.db.prepare(format!(
|
||||||
|
"SELECT username FROM Accounts;",
|
||||||
|
))?
|
||||||
|
.iter()
|
||||||
|
.map(|row| row.unwrap()) {
|
||||||
|
list.push(row.read::<&str, _>("username").to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Response::List(list))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(database: &std::path::Path) -> Result<Self> {
|
pub fn new(database: &std::path::Path) -> Result<Self> {
|
||||||
let conn = sqlite::open(database)?;
|
let conn = sqlite::open(database)?;
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ pub enum Call {
|
||||||
Register(String, String),
|
Register(String, String),
|
||||||
Authenticate(String, String),
|
Authenticate(String, String),
|
||||||
Delete(String),
|
Delete(String),
|
||||||
|
List,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -38,6 +39,7 @@ pub enum Response {
|
||||||
Register(ResponseRegister),
|
Register(ResponseRegister),
|
||||||
Authenticate(ResponseAuthenticate),
|
Authenticate(ResponseAuthenticate),
|
||||||
Delete(ResponseDelete),
|
Delete(ResponseDelete),
|
||||||
|
List(Vec<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Call {
|
impl Call {
|
||||||
|
@ -46,6 +48,7 @@ impl Call {
|
||||||
Self::Register(username, password) => backend.register(username, password),
|
Self::Register(username, password) => backend.register(username, password),
|
||||||
Self::Authenticate(username, password) => backend.authenticate(username, password),
|
Self::Authenticate(username, password) => backend.authenticate(username, password),
|
||||||
Self::Delete(username) => backend.delete(username),
|
Self::Delete(username) => backend.delete(username),
|
||||||
|
Self::List => backend.list(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
30
src/main.rs
30
src/main.rs
|
@ -3,7 +3,7 @@ mod backend;
|
||||||
mod call;
|
mod call;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use call::Call;
|
use call::{Call, Response};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use log::info;
|
use log::info;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
@ -20,6 +20,10 @@ struct Args {
|
||||||
#[arg(short = 'D', long)]
|
#[arg(short = 'D', long)]
|
||||||
daemon: bool,
|
daemon: bool,
|
||||||
|
|
||||||
|
/// List all accounts
|
||||||
|
#[arg(short, long)]
|
||||||
|
list: bool,
|
||||||
|
|
||||||
/// Delete one or multiple account(s)
|
/// Delete one or multiple account(s)
|
||||||
#[arg(short, long, use_value_delimiter = true, value_delimiter = ',')]
|
#[arg(short, long, use_value_delimiter = true, value_delimiter = ',')]
|
||||||
deletions: Option<Vec<String>>,
|
deletions: Option<Vec<String>>,
|
||||||
|
@ -30,6 +34,12 @@ async fn main() -> Result<()> {
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
|
if args.deletions == None &&
|
||||||
|
!args.daemon &&
|
||||||
|
!args.list {
|
||||||
|
return Err(anyhow::Error::msg("Nothing to do. run `baseauth --help` for a list of commands"));
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(dir) = args.dir {
|
if let Some(dir) = args.dir {
|
||||||
std::env::set_current_dir(dir)?;
|
std::env::set_current_dir(dir)?;
|
||||||
}
|
}
|
||||||
|
@ -39,6 +49,22 @@ async fn main() -> Result<()> {
|
||||||
let backend = backend::Backend::new(&Path::new("db.sqlite"))?;
|
let backend = backend::Backend::new(&Path::new("db.sqlite"))?;
|
||||||
let tx = call::start_worker(backend).await?;
|
let tx = call::start_worker(backend).await?;
|
||||||
|
|
||||||
|
if args.list {
|
||||||
|
let (oneshot_tx, oneshot_rx) = oneshot::channel();
|
||||||
|
tx.send((Call::List, oneshot_tx)).await?;
|
||||||
|
|
||||||
|
if let Response::List(list) = oneshot_rx.await?? {
|
||||||
|
if list.len() > 0 {
|
||||||
|
println!("Registered accounts:");
|
||||||
|
for user in list.iter() {
|
||||||
|
println!(" * {}", user);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("There are no accounts registered");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(d) = &args.deletions {
|
if let Some(d) = &args.deletions {
|
||||||
for username in d.iter() {
|
for username in d.iter() {
|
||||||
let (oneshot_tx, oneshot_rx) = oneshot::channel();
|
let (oneshot_tx, oneshot_rx) = oneshot::channel();
|
||||||
|
@ -52,8 +78,6 @@ async fn main() -> Result<()> {
|
||||||
if args.daemon {
|
if args.daemon {
|
||||||
api::start_worker(8080, tx).await?;
|
api::start_worker(8080, tx).await?;
|
||||||
loop {}
|
loop {}
|
||||||
} else if args.deletions == None {
|
|
||||||
println!("Nothing to do. run `baseauth --help` for a list of commands");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in New Issue