mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2025-06-11 19:03:21 +02:00
Add dumping the user db
This commit is contained in:
@ -187,15 +187,15 @@ impl UserDB {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_all(&self) -> Result<Vec<(String, User)>, db::Error> {
|
||||
pub fn get_all(&self) -> Result<HashMap<String, UserData>, db::Error> {
|
||||
let txn = self.env.begin_ro_txn()?;
|
||||
let iter = self.db.get_all(&txn)?;
|
||||
let mut out = Vec::new();
|
||||
let mut out = HashMap::new();
|
||||
for (uid, user) in iter {
|
||||
let uid = unsafe { std::str::from_utf8_unchecked(uid).to_string() };
|
||||
let user: User =
|
||||
Deserialize::<User, _>::deserialize(user.as_ref(), &mut Infallible).unwrap();
|
||||
out.push((uid, user));
|
||||
out.insert(uid, user.userdata);
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
|
@ -1,8 +1,11 @@
|
||||
use std::fs;
|
||||
|
||||
use lmdb::{Environment, Transaction};
|
||||
use once_cell::sync::OnceCell;
|
||||
use rkyv::{Archive, Deserialize, Infallible, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Display, Formatter, Write};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::io::Write;
|
||||
|
||||
use clap::ArgMatches;
|
||||
use miette::{Context, Diagnostic, IntoDiagnostic, SourceOffset, SourceSpan};
|
||||
@ -166,4 +169,41 @@ impl Users {
|
||||
txn.commit().map_err(crate::db::Error::from)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn dump_file(&self, path_str: &str, force: bool) -> miette::Result<usize> {
|
||||
let path = Path::new(path_str);
|
||||
let exists = path.exists();
|
||||
if exists {
|
||||
if !force {
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error("given file already exists, refusing to clobber")]
|
||||
#[diagnostic(code(dump::clobber))]
|
||||
struct DumpFileExists {
|
||||
#[source_code]
|
||||
src: String,
|
||||
|
||||
#[label("file provided")]
|
||||
dir_path: SourceSpan,
|
||||
|
||||
#[help]
|
||||
help: &'static str,
|
||||
}
|
||||
|
||||
Err(DumpFileExists {
|
||||
src: format!("--load {}", path_str),
|
||||
dir_path: (7, path_str.as_bytes().len()).into(),
|
||||
help: "to force overwriting the file add `--force` as argument",
|
||||
})?;
|
||||
} else {
|
||||
tracing::info!("output file already exists, overwriting due to `--force`");
|
||||
}
|
||||
}
|
||||
let mut file = fs::File::create(path).into_diagnostic()?;
|
||||
|
||||
let users = self.userdb.get_all()?;
|
||||
let encoded = toml::ser::to_vec(&users).into_diagnostic()?;
|
||||
file.write_all(&encoded[..]).into_diagnostic()?;
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user