Add a DB handle
This commit is contained in:
parent
667d881750
commit
2248cdeaad
@ -12,3 +12,8 @@ serde = "1.0.91"
|
||||
log = "0.4.6"
|
||||
pretty_env_logger = "0.3.0"
|
||||
rand = "0.6.5"
|
||||
dirs = "2.0.0"
|
||||
|
||||
[dependencies.rusqlite]
|
||||
version = "0.18.0"
|
||||
features = ["bundled"] # Compiles and statically links sqlite
|
||||
|
20
src/db.rs
Normal file
20
src/db.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use rusqlite::Connection;
|
||||
use std::path::PathBuf;
|
||||
|
||||
static DB_INIT_QUERY: &'static str = include_str!("init.sql");
|
||||
|
||||
pub struct DbHandle {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
impl DbHandle {
|
||||
pub fn new(db_path: &Option<String>) -> DbHandle {
|
||||
let path: PathBuf = db_path
|
||||
.as_ref()
|
||||
.map(|path: &String| PathBuf::from(path))
|
||||
.unwrap_or_else(|| dirs::home_dir().unwrap().join(".foil").to_path_buf());
|
||||
let conn: Connection = Connection::open(path).unwrap();
|
||||
conn.execute_batch(DB_INIT_QUERY).unwrap();
|
||||
DbHandle { conn: conn }
|
||||
}
|
||||
}
|
27
src/init.sql
Normal file
27
src/init.sql
Normal file
@ -0,0 +1,27 @@
|
||||
BEGIN TRANSACTION;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS encrypted_values (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
iv TEXT,
|
||||
ciphertext TEXT,
|
||||
mac TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS props(
|
||||
name TEXT NOT NULL PRIMARY KEY,
|
||||
value TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS encrypted_props(
|
||||
name TEXT NOT NULL PRIMARY KEY,
|
||||
encrypted_value INTEGER NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS accounts(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
server INTEGER NOT NULL,
|
||||
user INTEGER NOT NULL,
|
||||
password INTEGER NOT NULL
|
||||
);
|
||||
|
||||
END TRANSACTION;
|
11
src/main.rs
11
src/main.rs
@ -4,8 +4,11 @@ use rand::rngs::OsRng;
|
||||
use rand::seq::IteratorRandom;
|
||||
use rand::seq::SliceRandom;
|
||||
use serde::Deserialize;
|
||||
use std::error::Error;
|
||||
use std::iter::FromIterator;
|
||||
|
||||
pub mod db;
|
||||
|
||||
static USAGE: &'static str = "
|
||||
foil
|
||||
|
||||
@ -71,7 +74,7 @@ fn generate(spec: &str) {
|
||||
println!("{}", shuffled);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
pretty_env_logger::init();
|
||||
let args: Args = Docopt::new(USAGE)
|
||||
.and_then(|dopt| dopt.deserialize())
|
||||
@ -80,6 +83,10 @@ fn main() {
|
||||
|
||||
if args.cmd_generate {
|
||||
generate(&args.arg_spec.unwrap());
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut db_conn: db::DbHandle = db::DbHandle::new(&args.flag_db);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user