Diesel and XDG yak-shaving.

This commit is contained in:
Jordan Petridis 2017-09-15 04:45:25 +03:00
parent 44eb261b95
commit 600415ff5d
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 48 additions and 2 deletions

View File

@ -18,5 +18,6 @@ pub fn run() -> Result<()> {
let foo = args;
info!("{:?}", foo);
::init()?;
Ok(())
}

View File

@ -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]