Update config module to manage a directory instead of a file
This commit is contained in:
parent
9c680445f0
commit
5276dc262d
|
@ -3,7 +3,7 @@ extern crate dirs;
|
|||
use std::path::Path;
|
||||
use std::fs;
|
||||
use crate::matcher::Match;
|
||||
use std::fs::File;
|
||||
use std::fs::{File, create_dir_all};
|
||||
use std::io::Read;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::keyboard::KeyModifier;
|
||||
|
@ -11,12 +11,13 @@ use crate::keyboard::KeyModifier;
|
|||
// TODO: add documentation link
|
||||
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 {
|
||||
230
|
||||
}
|
||||
|
||||
fn default_backspace_limit() -> i32 {
|
||||
3
|
||||
}
|
||||
|
@ -50,7 +51,7 @@ impl Default for BackendType {
|
|||
}
|
||||
|
||||
impl Configs {
|
||||
pub fn load(path: &Path) -> Configs {
|
||||
fn load_config(path: &Path) -> Configs {
|
||||
let file_res = File::open(path);
|
||||
if let Ok(mut file) = file_res {
|
||||
let mut contents = String::new();
|
||||
|
@ -64,21 +65,59 @@ impl Configs {
|
|||
panic!("Config file not found...")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_default() -> Configs {
|
||||
let res = dirs::home_dir();
|
||||
if let Some(home_dir) = res {
|
||||
let default_file = home_dir.join(".espanso");
|
||||
pub struct ConfigSet {
|
||||
default: Configs,
|
||||
specific: Vec<Configs>,
|
||||
}
|
||||
|
||||
// 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");
|
||||
}
|
||||
impl ConfigSet {
|
||||
pub fn load(dir_path: &Path) -> ConfigSet {
|
||||
if !dir_path.is_dir() {
|
||||
panic!("Invalid config directory");
|
||||
}
|
||||
|
||||
Configs::load(default_file.as_path())
|
||||
}else{
|
||||
panic!("Could not generate default position for config file");
|
||||
let default_file = espanso_dir.join(DEFAULT_CONFIG_FILE_NAME);
|
||||
let default = Configs::load_config(default_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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user