diff --git a/Cargo.toml b/Cargo.toml index c8cabdc..be65de9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..bf0c896 --- /dev/null +++ b/src/db.rs @@ -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) -> 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 } + } +} diff --git a/src/init.sql b/src/init.sql new file mode 100644 index 0000000..21f07d9 --- /dev/null +++ b/src/init.sql @@ -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; diff --git a/src/main.rs b/src/main.rs index 0342ec1..27fc229 100644 --- a/src/main.rs +++ b/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> { 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(()) }