feat: yaml merge key
This commit is contained in:
parent
4213ee660f
commit
6331447dcd
@ -125,7 +125,7 @@ impl Clash {
|
|||||||
|
|
||||||
/// get clash config
|
/// get clash config
|
||||||
pub fn read_config() -> Mapping {
|
pub fn read_config() -> Mapping {
|
||||||
config::read_yaml::<Mapping>(dirs::clash_path())
|
config::read_merge_mapping(dirs::clash_path())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// save the clash config
|
/// save the clash config
|
||||||
|
@ -352,7 +352,7 @@ impl PrfItem {
|
|||||||
}),
|
}),
|
||||||
"merge" => Some(ChainItem {
|
"merge" => Some(ChainItem {
|
||||||
uid,
|
uid,
|
||||||
data: ChainType::Merge(config::read_yaml::<Mapping>(path)),
|
data: ChainType::Merge(config::read_merge_mapping(path)),
|
||||||
}),
|
}),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
@ -283,7 +283,7 @@ impl Profiles {
|
|||||||
bail!("failed to read the file \"{}\"", file_path.display());
|
bail!("failed to read the file \"{}\"", file_path.display());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(config::read_yaml::<Mapping>(file_path.clone()));
|
return Ok(config::read_merge_mapping(file_path.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bail!("failed to find current profile \"uid:{current}\"");
|
bail!("failed to find current profile \"uid:{current}\"");
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
use serde_yaml::{Mapping, Value};
|
||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
/// read data from yaml as struct T
|
/// read data from yaml as struct T
|
||||||
@ -20,6 +21,29 @@ pub fn read_yaml<T: DeserializeOwned + Default>(path: PathBuf) -> T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// read mapping from yaml fix #165
|
||||||
|
pub fn read_merge_mapping(path: PathBuf) -> Mapping {
|
||||||
|
let map = Mapping::new();
|
||||||
|
|
||||||
|
if !path.exists() {
|
||||||
|
log::error!(target: "app", "file not found \"{}\"", path.display());
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
let yaml_str = fs::read_to_string(&path).unwrap_or("".into());
|
||||||
|
|
||||||
|
match serde_yaml::from_str::<Value>(&yaml_str) {
|
||||||
|
Ok(mut val) => {
|
||||||
|
crate::log_if_err!(val.apply_merge());
|
||||||
|
val.as_mapping().unwrap_or(&map).to_owned()
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
log::error!(target: "app", "failed to read yaml file \"{}\"", path.display());
|
||||||
|
map
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// save the data to the file
|
/// save the data to the file
|
||||||
/// can set `prefix` string to add some comments
|
/// can set `prefix` string to add some comments
|
||||||
pub fn save_yaml<T: Serialize>(path: PathBuf, data: &T, prefix: Option<&str>) -> Result<()> {
|
pub fn save_yaml<T: Serialize>(path: PathBuf, data: &T, prefix: Option<&str>) -> Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user