mirror of
https://github.com/agersant/polaris
synced 2024-12-02 17:49:10 +00:00
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 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<DDNSConfig>,
|
||||
}
|
||||
|
||||
impl UserConfig {
|
||||
pub fn parse(path: &path::Path) -> Result<UserConfig> {
|
||||
println!("Config file path: {}", path.to_string_lossy());
|
||||
pub fn parse(path: &path::Path) -> Result<UserConfig> {
|
||||
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::<UserConfig>(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::<UserConfig>(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<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 {
|
||||
|
|
|
@ -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<String> {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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<T>(db: &T, count: i64) -> Result<Vec<Directory>>
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue