fix: init config file

This commit is contained in:
GyDi 2022-11-22 22:01:19 +08:00
parent 3e555ec9f1
commit 40c041031e
No known key found for this signature in database
GPG Key ID: B28E790B95193457
2 changed files with 35 additions and 10 deletions

View File

@ -31,7 +31,7 @@ impl IClashTemp {
map.insert("external-controller".into(), "127.0.0.1:9090".into()); map.insert("external-controller".into(), "127.0.0.1:9090".into());
map.insert("secret".into(), "".into()); map.insert("secret".into(), "".into());
Self(Self::guard(map)) Self(map)
} }
fn guard(mut config: Mapping) -> Mapping { fn guard(mut config: Mapping) -> Mapping {
@ -57,10 +57,6 @@ impl IClashTemp {
) )
} }
// pub fn get_info(&self) -> ClashInfo {
// self.1.clone()
// }
pub fn get_mixed_port(&self) -> u16 { pub fn get_mixed_port(&self) -> u16 {
Self::guard_mixed_port(&self.0) Self::guard_mixed_port(&self.0)
} }

View File

@ -1,4 +1,5 @@
use crate::utils::dirs; use crate::config::*;
use crate::utils::{dirs, help};
use anyhow::Result; use anyhow::Result;
use chrono::Local; use chrono::Local;
use log::LevelFilter; use log::LevelFilter;
@ -60,17 +61,38 @@ pub fn init_config() -> Result<()> {
let _ = init_log(); let _ = init_log();
let _ = dirs::app_home_dir().map(|app_dir| { crate::log_err!(dirs::app_home_dir().map(|app_dir| {
if !app_dir.exists() { if !app_dir.exists() {
let _ = fs::create_dir_all(&app_dir); let _ = fs::create_dir_all(&app_dir);
} }
}); }));
let _ = dirs::app_profiles_dir().map(|profiles_dir| { crate::log_err!(dirs::app_profiles_dir().map(|profiles_dir| {
if !profiles_dir.exists() { if !profiles_dir.exists() {
let _ = fs::create_dir_all(&profiles_dir); let _ = fs::create_dir_all(&profiles_dir);
} }
}); }));
crate::log_err!(dirs::clash_path().map(|path| {
if !path.exists() {
help::save_yaml(&path, &IClashTemp::template().0, Some("# Clash Verge"))?;
}
<Result<()>>::Ok(())
}));
crate::log_err!(dirs::verge_path().map(|path| {
if !path.exists() {
help::save_yaml(&path, &IVerge::template(), Some("# Clash Verge"))?;
}
<Result<()>>::Ok(())
}));
crate::log_err!(dirs::profiles_path().map(|path| {
if !path.exists() {
help::save_yaml(&path, &IProfiles::template(), Some("# Clash Verge"))?;
}
<Result<()>>::Ok(())
}));
Ok(()) Ok(())
} }
@ -80,6 +102,13 @@ pub fn init_resources(package_info: &PackageInfo) -> Result<()> {
let app_dir = dirs::app_home_dir()?; let app_dir = dirs::app_home_dir()?;
let res_dir = dirs::app_resources_dir(package_info)?; let res_dir = dirs::app_resources_dir(package_info)?;
if !app_dir.exists() {
let _ = fs::create_dir_all(&app_dir);
}
if !res_dir.exists() {
let _ = fs::create_dir_all(&res_dir);
}
// copy the resource file // copy the resource file
for file in ["Country.mmdb", "geoip.dat", "geosite.dat", "wintun.dll"].iter() { for file in ["Country.mmdb", "geoip.dat", "geosite.dat", "wintun.dll"].iter() {
let src_path = res_dir.join(file); let src_path = res_dir.join(file);