Moved config business out of DB struct
This commit is contained in:
parent
f498956710
commit
89d746268e
4 changed files with 84 additions and 63 deletions
|
@ -1,12 +1,18 @@
|
||||||
|
use core::ops::Deref;
|
||||||
|
use diesel;
|
||||||
|
use diesel::prelude::*;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path;
|
use std::path;
|
||||||
use toml;
|
use toml;
|
||||||
|
|
||||||
use errors::*;
|
use db::ConnectionSource;
|
||||||
use vfs::MountPoint;
|
use db::{misc_settings, mount_points, users};
|
||||||
use ddns::DDNSConfig;
|
use ddns::DDNSConfig;
|
||||||
|
use errors::*;
|
||||||
|
use user::*;
|
||||||
|
use vfs::MountPoint;
|
||||||
|
|
||||||
#[derive(Debug, Queryable)]
|
#[derive(Debug, Queryable)]
|
||||||
pub struct MiscSettings {
|
pub struct MiscSettings {
|
||||||
|
@ -31,8 +37,7 @@ pub struct UserConfig {
|
||||||
pub ydns: Option<DDNSConfig>,
|
pub ydns: Option<DDNSConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserConfig {
|
pub fn parse(path: &path::Path) -> Result<UserConfig> {
|
||||||
pub fn parse(path: &path::Path) -> Result<UserConfig> {
|
|
||||||
println!("Config file path: {}", path.to_string_lossy());
|
println!("Config file path: {}", path.to_string_lossy());
|
||||||
|
|
||||||
// Parse user config
|
// Parse user config
|
||||||
|
@ -52,7 +57,57 @@ impl UserConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(config)
|
Ok(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reset<T>(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<T>(db: &T, new_config: &UserConfig) -> Result<()> where T:ConnectionSource {
|
||||||
|
reset(db)?;
|
||||||
|
ammend(db, new_config)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ammend<T>(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 {
|
fn clean_path_string(path_string: &str) -> path::PathBuf {
|
||||||
|
|
|
@ -6,9 +6,8 @@ use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use config::{MiscSettings, UserConfig};
|
use config::MiscSettings;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use user::*;
|
|
||||||
|
|
||||||
mod schema;
|
mod schema;
|
||||||
|
|
||||||
|
@ -66,41 +65,6 @@ impl DB {
|
||||||
Ok(())
|
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<String> {
|
pub fn get_auth_secret(&self) -> Result<String> {
|
||||||
let connection = self.connection.lock().unwrap();
|
let connection = self.connection.lock().unwrap();
|
||||||
let connection = connection.deref();
|
let connection = connection.deref();
|
||||||
|
@ -116,8 +80,9 @@ impl ConnectionSource for DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _get_test_db(name: &str) -> DB {
|
fn _get_test_db(name: &str) -> DB {
|
||||||
|
use config;
|
||||||
let config_path = Path::new("test/config.toml");
|
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();
|
let mut db_path = PathBuf::new();
|
||||||
db_path.push("test");
|
db_path.push("test");
|
||||||
|
@ -127,7 +92,7 @@ fn _get_test_db(name: &str) -> DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
let db = DB::new(&db_path).unwrap();
|
let db = DB::new(&db_path).unwrap();
|
||||||
db.load_config(&config).unwrap();
|
config::overwrite(&db, &config).unwrap();
|
||||||
db
|
db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ use std::sync::Mutex;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
use config::{MiscSettings, UserConfig};
|
use config::MiscSettings;
|
||||||
use db::ConnectionSource;
|
use db::ConnectionSource;
|
||||||
use db::DB;
|
use db::DB;
|
||||||
use db::{directories, misc_settings, songs};
|
use db::{directories, misc_settings, songs};
|
||||||
|
@ -544,8 +544,9 @@ pub fn get_recent_albums<T>(db: &T, count: i64) -> Result<Vec<Directory>>
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _get_test_db(name: &str) -> DB {
|
fn _get_test_db(name: &str) -> DB {
|
||||||
|
use config;
|
||||||
let config_path = Path::new("test/config.toml");
|
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();
|
let mut db_path = PathBuf::new();
|
||||||
db_path.push("test");
|
db_path.push("test");
|
||||||
|
@ -555,7 +556,7 @@ fn _get_test_db(name: &str) -> DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
let db = DB::new(&db_path).unwrap();
|
let db = DB::new(&db_path).unwrap();
|
||||||
db.load_config(&config).unwrap();
|
config::overwrite(&db, &config).unwrap();
|
||||||
db
|
db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -118,8 +118,8 @@ fn run() -> Result<()> {
|
||||||
let config_file_name = matches.opt_str("c");
|
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());
|
let config_file_path = config_file_name.map(|p| Path::new(p.as_str()).to_path_buf());
|
||||||
if let Some(path) = config_file_path {
|
if let Some(path) = config_file_path {
|
||||||
let config = config::UserConfig::parse(&path)?;
|
let config = config::parse(&path)?;
|
||||||
db.load_config(&config)?;
|
config::overwrite(db.deref(), &config)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin indexing
|
// Begin indexing
|
||||||
|
|
Loading…
Add table
Reference in a new issue