Add --ephemeral cli flag

This commit is contained in:
Joscha 2022-08-22 20:58:11 +02:00
parent f76c6a557d
commit 68bd6042c5
4 changed files with 48 additions and 19 deletions

View file

@ -20,9 +20,14 @@ enum Request {
#[derive(Debug, Clone)]
pub struct Vault {
tx: mpsc::UnboundedSender<Request>,
ephemeral: bool,
}
impl Vault {
pub fn ephemeral(&self) -> bool {
self.ephemeral
}
pub async fn close(&self) {
let (tx, rx) = oneshot::channel();
let _ = self.tx.send(Request::Close(tx));
@ -64,12 +69,26 @@ fn run(mut conn: Connection, mut rx: mpsc::UnboundedReceiver<Request>) {
}
}
fn launch_from_connection(mut conn: Connection, ephemeral: bool) -> rusqlite::Result<Vault> {
conn.pragma_update(None, "foreign_keys", true)?;
conn.pragma_update(None, "trusted_schema", false)?;
println!("Opening vault");
migrate::migrate(&mut conn)?;
prepare::prepare(&mut conn)?;
let (tx, rx) = mpsc::unbounded_channel();
thread::spawn(move || run(conn, rx));
Ok(Vault { tx, ephemeral })
}
pub fn launch(path: &Path) -> rusqlite::Result<Vault> {
// If this fails, rusqlite will complain about not being able to open the db
// file, which saves me from adding a separate vault error type.
let _ = fs::create_dir_all(path.parent().expect("path to file"));
let mut conn = Connection::open(path)?;
let conn = Connection::open(path)?;
// Setting locking mode before journal mode so no shared memory files
// (*-shm) need to be created by sqlite. Apparently, setting the journal
@ -78,13 +97,11 @@ pub fn launch(path: &Path) -> rusqlite::Result<Vault> {
// https://sqlite.org/pragma.html#pragma_locking_mode
conn.pragma_update(None, "locking_mode", "exclusive")?;
conn.pragma_update(None, "journal_mode", "wal")?;
conn.pragma_update(None, "foreign_keys", true)?;
conn.pragma_update(None, "trusted_schema", false)?;
migrate::migrate(&mut conn)?;
prepare::prepare(&mut conn)?;
let (tx, rx) = mpsc::unbounded_channel();
thread::spawn(move || run(conn, rx));
Ok(Vault { tx })
launch_from_connection(conn, false)
}
pub fn launch_in_memory() -> rusqlite::Result<Vault> {
let conn = Connection::open_in_memory()?;
launch_from_connection(conn, true)
}