From 89d746268e203cb60a175f9ecc6063d8044c5424 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sat, 1 Jul 2017 13:43:13 -0700 Subject: [PATCH] Moved config business out of DB struct --- src/config.rs | 93 ++++++++++++++++++++++++++++++++++++++++----------- src/db/mod.rs | 43 +++--------------------- src/index.rs | 7 ++-- src/main.rs | 4 +-- 4 files changed, 84 insertions(+), 63 deletions(-) diff --git a/src/config.rs b/src/config.rs index f960b6c..de74450 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,18 @@ +use core::ops::Deref; +use diesel; +use diesel::prelude::*; use regex::Regex; use std::fs; use std::io::Read; use std::path; use toml; -use errors::*; -use vfs::MountPoint; +use db::ConnectionSource; +use db::{misc_settings, mount_points, users}; use ddns::DDNSConfig; +use errors::*; +use user::*; +use vfs::MountPoint; #[derive(Debug, Queryable)] pub struct MiscSettings { @@ -31,28 +37,77 @@ pub struct UserConfig { pub ydns: Option, } -impl UserConfig { - pub fn parse(path: &path::Path) -> Result { - println!("Config file path: {}", path.to_string_lossy()); +pub fn parse(path: &path::Path) -> Result { + println!("Config file path: {}", path.to_string_lossy()); - // Parse user config - let mut config_file = fs::File::open(path)?; - let mut config_file_content = String::new(); - config_file.read_to_string(&mut config_file_content)?; - let mut config = toml::de::from_str::(config_file_content.as_str())?; + // Parse user config + let mut config_file = fs::File::open(path)?; + let mut config_file_content = String::new(); + config_file.read_to_string(&mut config_file_content)?; + let mut config = toml::de::from_str::(config_file_content.as_str())?; - // Clean path - if let Some(ref mut mount_dirs) = config.mount_dirs { - for mount_dir in mount_dirs { - match clean_path_string(&mount_dir.source).to_str() { - Some(p) => mount_dir.source = p.to_owned(), - _ => bail!("Bad mount directory path"), - } + // Clean path + if let Some(ref mut mount_dirs) = config.mount_dirs { + for mount_dir in mount_dirs { + match clean_path_string(&mount_dir.source).to_str() { + Some(p) => mount_dir.source = p.to_owned(), + _ => bail!("Bad mount directory path"), } } - - Ok(config) } + + Ok(config) +} + +fn reset(db: &T) -> Result<()> where T:ConnectionSource { + let connection = db.get_connection(); + let connection = connection.lock().unwrap(); + let connection = connection.deref(); + + diesel::delete(mount_points::table).execute(connection)?; + diesel::delete(users::table).execute(connection)?; + + Ok(()) +} + +pub fn overwrite(db: &T, new_config: &UserConfig) -> Result<()> where T:ConnectionSource { + reset(db)?; + ammend(db, new_config) +} + +pub fn ammend(db: &T, new_config: &UserConfig) -> Result<()> where T:ConnectionSource { + let connection = db.get_connection(); + let connection = connection.lock().unwrap(); + let connection = connection.deref(); + + if let Some(ref mount_dirs) = new_config.mount_dirs { + diesel::insert(mount_dirs) + .into(mount_points::table) + .execute(connection)?; + } + + if let Some(ref config_users) = new_config.users { + for config_user in config_users { + let new_user = NewUser::new(&config_user.name, &config_user.password); + diesel::insert(&new_user) + .into(users::table) + .execute(connection)?; + } + } + + if let Some(sleep_duration) = new_config.reindex_every_n_seconds { + diesel::update(misc_settings::table) + .set(misc_settings::index_sleep_duration_seconds.eq(sleep_duration as i32)) + .execute(connection)?; + } + + if let Some(ref album_art_pattern) = new_config.album_art_pattern { + diesel::update(misc_settings::table) + .set(misc_settings::index_album_art_pattern.eq(album_art_pattern)) + .execute(connection)?; + } + + Ok(()) } fn clean_path_string(path_string: &str) -> path::PathBuf { diff --git a/src/db/mod.rs b/src/db/mod.rs index eecc236..c73518d 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -6,9 +6,8 @@ use std::fs; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; -use config::{MiscSettings, UserConfig}; +use config::MiscSettings; use errors::*; -use user::*; mod schema; @@ -66,41 +65,6 @@ impl DB { Ok(()) } - pub fn load_config(&self, config: &UserConfig) -> Result<()> { - let connection = self.connection.lock().unwrap(); - let connection = connection.deref(); - if let Some(ref mount_dirs) = config.mount_dirs { - diesel::delete(mount_points::table).execute(connection)?; - diesel::insert(mount_dirs) - .into(mount_points::table) - .execute(connection)?; - } - - if let Some(ref config_users) = config.users { - diesel::delete(users::table).execute(connection)?; - for config_user in config_users { - let new_user = NewUser::new(&config_user.name, &config_user.password); - diesel::insert(&new_user) - .into(users::table) - .execute(connection)?; - } - } - - if let Some(sleep_duration) = config.reindex_every_n_seconds { - diesel::update(misc_settings::table) - .set(misc_settings::index_sleep_duration_seconds.eq(sleep_duration as i32)) - .execute(connection)?; - } - - if let Some(ref album_art_pattern) = config.album_art_pattern { - diesel::update(misc_settings::table) - .set(misc_settings::index_album_art_pattern.eq(album_art_pattern)) - .execute(connection)?; - } - - Ok(()) - } - pub fn get_auth_secret(&self) -> Result { let connection = self.connection.lock().unwrap(); let connection = connection.deref(); @@ -116,8 +80,9 @@ impl ConnectionSource for DB { } fn _get_test_db(name: &str) -> DB { + use config; let config_path = Path::new("test/config.toml"); - let config = UserConfig::parse(&config_path).unwrap(); + let config = config::parse(&config_path).unwrap(); let mut db_path = PathBuf::new(); db_path.push("test"); @@ -127,7 +92,7 @@ fn _get_test_db(name: &str) -> DB { } let db = DB::new(&db_path).unwrap(); - db.load_config(&config).unwrap(); + config::overwrite(&db, &config).unwrap(); db } diff --git a/src/index.rs b/src/index.rs index 734d1ca..8450655 100644 --- a/src/index.rs +++ b/src/index.rs @@ -11,7 +11,7 @@ use std::sync::Mutex; use std::thread; use std::time; -use config::{MiscSettings, UserConfig}; +use config::MiscSettings; use db::ConnectionSource; use db::DB; use db::{directories, misc_settings, songs}; @@ -544,8 +544,9 @@ pub fn get_recent_albums(db: &T, count: i64) -> Result> } fn _get_test_db(name: &str) -> DB { + use config; let config_path = Path::new("test/config.toml"); - let config = UserConfig::parse(&config_path).unwrap(); + let config = config::parse(&config_path).unwrap(); let mut db_path = PathBuf::new(); db_path.push("test"); @@ -555,7 +556,7 @@ fn _get_test_db(name: &str) -> DB { } let db = DB::new(&db_path).unwrap(); - db.load_config(&config).unwrap(); + config::overwrite(&db, &config).unwrap(); db } diff --git a/src/main.rs b/src/main.rs index c76f3c8..1dbf061 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,8 +118,8 @@ fn run() -> Result<()> { let config_file_name = matches.opt_str("c"); let config_file_path = config_file_name.map(|p| Path::new(p.as_str()).to_path_buf()); if let Some(path) = config_file_path { - let config = config::UserConfig::parse(&path)?; - db.load_config(&config)?; + let config = config::parse(&path)?; + config::overwrite(db.deref(), &config)?; } // Begin indexing