feat: 灯条配置。
This commit is contained in:
@@ -2,7 +2,8 @@ use std::{
|
||||
env::current_dir,
|
||||
fs::{self, File},
|
||||
io::Read,
|
||||
path::PathBuf, sync::Arc,
|
||||
path::PathBuf,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
@@ -43,7 +44,9 @@ impl Manager {
|
||||
}
|
||||
|
||||
pub fn new(config: Configuration) -> Self {
|
||||
Self { config: Arc::new(Mutex::new(config)) }
|
||||
Self {
|
||||
config: Arc::new(Mutex::new(config)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_config_file_path() -> PathBuf {
|
||||
@@ -70,7 +73,10 @@ impl Manager {
|
||||
.map_err(|error| anyhow::anyhow!("can not parse config file contents. {}", error))
|
||||
}
|
||||
|
||||
pub fn write_config_to_disk(config_file_path: PathBuf, config: &Configuration) -> anyhow::Result<()> {
|
||||
pub fn write_config_to_disk(
|
||||
config_file_path: PathBuf,
|
||||
config: &Configuration,
|
||||
) -> anyhow::Result<()> {
|
||||
let contents = serde_json::to_string(config)
|
||||
.map_err(|error| anyhow::anyhow!("can not serialize config. {}", error))?;
|
||||
info!("contents: {}", contents);
|
||||
@@ -86,6 +92,13 @@ impl Manager {
|
||||
let mut config = self.config.lock().await;
|
||||
*config = new_config.clone();
|
||||
}
|
||||
pub async fn reload_config(&self) -> anyhow::Result<Configuration> {
|
||||
let mut config = self.config.lock().await;
|
||||
let new_config = Self::read_config_from_disk(Self::get_config_file_path())
|
||||
.map_err(|err| anyhow::anyhow!("can not reload config. {:?}", err))?;
|
||||
*config = new_config.clone();
|
||||
return anyhow::Ok(new_config);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -103,9 +116,11 @@ mod tests {
|
||||
let temp = TestDir::temp().create("config_dir", test_dir::FileType::Dir);
|
||||
let config_file_path = temp.path("config_dir").join("picker.config.json");
|
||||
let manager = crate::picker::config::manger::Manager::default();
|
||||
crate::picker::config::manger::Manager
|
||||
::write_config_to_disk(config_file_path.clone(), &Configuration::default())
|
||||
.unwrap();
|
||||
crate::picker::config::manger::Manager::write_config_to_disk(
|
||||
config_file_path.clone(),
|
||||
&Configuration::default(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let contents = fs::read_to_string(config_file_path.clone()).unwrap();
|
||||
let _config: Configuration = serde_json::from_str(contents.as_str()).unwrap();
|
||||
|
Reference in New Issue
Block a user