diff --git a/src/cli.rs b/src/cli.rs index e1a7c58..6e6a098 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,5 +18,6 @@ pub fn run() -> Result<()> { let foo = args; info!("{:?}", foo); + ::init()?; Ok(()) } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 4f106e9..ecb9f10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,18 +3,24 @@ extern crate structopt; #[macro_use] extern crate structopt_derive; + #[macro_use] extern crate error_chain; + #[macro_use] extern crate lazy_static; + #[macro_use] extern crate log; extern crate loggerv; -extern crate reqwest; + #[macro_use] extern crate diesel; +#[macro_use] +extern crate diesel_codegen; -// use diesel::prelude::*; +extern crate xdg; +extern crate reqwest; pub mod cli; pub mod schema; @@ -24,16 +30,55 @@ pub mod errors { use reqwest; use std::io; + use diesel::migrations::RunMigrationsError; error_chain! { foreign_links { ReqError(reqwest::Error); IoError(io::Error); Log(::log::SetLoggerError); + MigrationError(RunMigrationsError); } } } +use errors::*; +use diesel::prelude::*; +use std::path::PathBuf; + +embed_migrations!("migrations/"); + +lazy_static!{ + + static ref HAMMOND_XDG: xdg::BaseDirectories = { + xdg::BaseDirectories::with_prefix("Hammond").unwrap() + }; + + static ref HAMMOND_DATA: PathBuf = HAMMOND_XDG.create_data_directory(HAMMOND_XDG.get_data_home()).unwrap(); + static ref HAMMOND_CONFIG: PathBuf = HAMMOND_XDG.create_config_directory(HAMMOND_XDG.get_config_home()).unwrap(); + static ref HAMMOND_CACHE: PathBuf = HAMMOND_XDG.create_cache_directory(HAMMOND_XDG.get_cache_home()).unwrap(); + + static ref DB_PATH: std::path::PathBuf = HAMMOND_XDG.place_data_file("hammond.db").unwrap(); +} + +pub fn init() -> Result<()> { + &HAMMOND_DATA; + let conn = establish_connection(); + // embedded_migrations::run(&conn)?; + embedded_migrations::run_with_output(&conn, &mut std::io::stdout())?; + + Ok(()) +} + +pub fn establish_connection() -> SqliteConnection { + let database_url = DB_PATH.to_str().unwrap(); + // let database_url = &String::from(".random/foo.db"); + SqliteConnection::establish(database_url).expect(&format!( + "Error connecting to {}", + database_url + )) +} + #[cfg(test)] mod tests { #[test]