Update config module to manage a directory instead of a file

This commit is contained in:
Federico Terzi 2019-09-07 11:27:08 +02:00
parent 9c680445f0
commit 5276dc262d

View File

@ -3,7 +3,7 @@ extern crate dirs;
use std::path::Path; use std::path::Path;
use std::fs; use std::fs;
use crate::matcher::Match; use crate::matcher::Match;
use std::fs::File; use std::fs::{File, create_dir_all};
use std::io::Read; use std::io::Read;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::keyboard::KeyModifier; use crate::keyboard::KeyModifier;
@ -11,12 +11,13 @@ use crate::keyboard::KeyModifier;
// TODO: add documentation link // TODO: add documentation link
const DEFAULT_CONFIG_FILE_CONTENT : &str = include_str!("res/config.yaml"); const DEFAULT_CONFIG_FILE_CONTENT : &str = include_str!("res/config.yaml");
// Default values for primitives const DEFAULT_CONFIG_FILE_NAME : &str = "default.yaml";
// Default values for primitives
fn default_toggle_interval() -> u32 { fn default_toggle_interval() -> u32 {
230 230
} }
fn default_backspace_limit() -> i32 { fn default_backspace_limit() -> i32 {
3 3
} }
@ -50,7 +51,7 @@ impl Default for BackendType {
} }
impl Configs { impl Configs {
pub fn load(path: &Path) -> Configs { fn load_config(path: &Path) -> Configs {
let file_res = File::open(path); let file_res = File::open(path);
if let Ok(mut file) = file_res { if let Ok(mut file) = file_res {
let mut contents = String::new(); let mut contents = String::new();
@ -64,21 +65,59 @@ impl Configs {
panic!("Config file not found...") panic!("Config file not found...")
} }
} }
}
pub fn load_default() -> Configs { pub struct ConfigSet {
let res = dirs::home_dir(); default: Configs,
if let Some(home_dir) = res { specific: Vec<Configs>,
let default_file = home_dir.join(".espanso"); }
// If config file does not exist, create one from template impl ConfigSet {
if !default_file.exists() { pub fn load(dir_path: &Path) -> ConfigSet {
fs::write(&default_file, DEFAULT_CONFIG_FILE_CONTENT) if !dir_path.is_dir() {
.expect("Unable to write default config file"); panic!("Invalid config directory");
} }
Configs::load(default_file.as_path()) let default_file = espanso_dir.join(DEFAULT_CONFIG_FILE_NAME);
}else{ let default = Configs::load_config(default_file);
panic!("Could not generate default position for config file");
let mut specific = Vec::new();
for entry in fs::read_dir(dir_path)? {
let entry = entry?;
let path = entry.path();
let config = Configs::load_config(path.as_path());
specific.push(config);
}
ConfigSet {
default,
specific
} }
} }
pub fn load_default() -> ConfigSet {
let res = dirs::home_dir();
if let Some(home_dir) = res {
let espanso_dir = home_dir.join(".espanso");
// Create the espanso dir if id doesn't exist
let res = create_dir_all(espanso_dir);
if let Ok(_) = res {
let default_file = espanso_dir.join(DEFAULT_CONFIG_FILE_NAME);
// If config file does not exist, create one from template
if !default_file.exists() {
fs::write(&default_file, DEFAULT_CONFIG_FILE_CONTENT)
.expect("Unable to write default config file");
}
return ConfigSet::load(espanso_dir.as_path())
}
}
panic!("Could not generate default position for config file");
}
} }