feat: read clash config

This commit is contained in:
GyDi 2021-12-14 15:55:29 +08:00
parent b5d0c2b78b
commit 2a986a3d2f
3 changed files with 70 additions and 16 deletions

View File

@ -0,0 +1,27 @@
use serde::{Deserialize, Serialize};
/// ### `config.yaml` schema
/// here should contain all configuration options.
/// See: https://github.com/Dreamacro/clash/wiki/configuration for details
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct ClashConfig {
pub port: Option<u32>,
/// alias to `mixed-port`
pub mixed_port: Option<u32>,
/// alias to `allow-lan`
pub allow_lan: Option<bool>,
/// alias to `external-controller`
pub external_ctrl: Option<String>,
pub secret: Option<String>,
}
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct ClashController {
/// same as `external-controller`
pub server: Option<String>,
pub secret: Option<String>,
}

View File

@ -1,7 +1,8 @@
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};
use super::profiles::ProfilesConfig; use super::{profiles::ProfilesConfig, ClashController};
use crate::init::app_home_dir; use crate::init::app_home_dir;
/// read data from yaml as struct T /// read data from yaml as struct T
@ -34,23 +35,37 @@ pub fn save_yaml<T: Serialize>(
} }
} }
// /// Get Clash Core Config /// Get Clash Core Config
// pub fn read_clash() -> Mapping { pub fn read_clash() -> Mapping {
// read_yaml::<Mapping>(app_home_dir().join("config.yaml")) read_yaml::<Mapping>(app_home_dir().join("config.yaml"))
// } }
// /// Get Verge App Config /// Get infomation of the clash's `external-controller` and `secret`
// pub fn read_verge() -> ProfilesConfig { pub fn read_clash_controller() -> ClashController {
// read_from_yaml::<ProfilesConfig>(app_home_dir().join("verge.yaml")) let config = read_clash();
// }
// /// Save Verge App Config let key_server = Value::String("external-controller".to_string());
// pub fn save_verge(verge_config: &ProfilesConfig) { let key_secret = Value::String("secret".to_string());
// let yaml_path = app_home_dir().join("verge.yaml");
// let yaml_str = serde_yaml::to_string(&verge_config).unwrap(); let server = match config.get(&key_server) {
// let yaml_str = String::from("# Config File for Clash Verge\n\n") + &yaml_str; Some(value) => match value {
// fs::write(yaml_path, yaml_str.as_bytes()).unwrap(); Value::String(val_str) => Some(val_str.clone()),
// } _ => None,
},
_ => None,
};
let secret = match config.get(&key_secret) {
Some(value) => match value {
Value::String(val_str) => Some(val_str.clone()),
Value::Bool(val_bool) => Some(val_bool.to_string()),
Value::Number(val_num) => Some(val_num.to_string()),
_ => None,
},
_ => None,
};
ClashController { server, secret }
}
/// Get Profiles Config /// Get Profiles Config
pub fn read_profiles() -> ProfilesConfig { pub fn read_profiles() -> ProfilesConfig {
@ -66,3 +81,8 @@ pub fn save_profiles(profiles: &ProfilesConfig) {
) )
.unwrap(); .unwrap();
} }
#[test]
fn test_print_config() {
println!("{:?}", read_clash_controller());
}

View File

@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
/// ### `verge.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct VergeConfig {
pub something: Option<String>,
}