From 3a9a392a772b8e7c350e329f72fb74bb1648e6b7 Mon Sep 17 00:00:00 2001 From: GyDi Date: Fri, 18 Feb 2022 23:57:13 +0800 Subject: [PATCH] feat: inline config file template --- src-tauri/resources/config_tmp.yaml | 8 ----- src-tauri/resources/item_tmp.yaml | 16 --------- src-tauri/resources/profiles_tmp.yaml | 3 -- src-tauri/resources/verge_tmp.yaml | 6 ---- src-tauri/src/core/profiles.rs | 13 ++----- src-tauri/src/utils/init.rs | 49 ++++++++++----------------- src-tauri/src/utils/mod.rs | 1 + src-tauri/src/utils/tmpl.rs | 38 +++++++++++++++++++++ 8 files changed, 59 insertions(+), 75 deletions(-) delete mode 100644 src-tauri/resources/config_tmp.yaml delete mode 100644 src-tauri/resources/item_tmp.yaml delete mode 100644 src-tauri/resources/profiles_tmp.yaml delete mode 100644 src-tauri/resources/verge_tmp.yaml create mode 100644 src-tauri/src/utils/tmpl.rs diff --git a/src-tauri/resources/config_tmp.yaml b/src-tauri/resources/config_tmp.yaml deleted file mode 100644 index f987909..0000000 --- a/src-tauri/resources/config_tmp.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# Default Config For Clash Core - -mixed-port: 7890 -log-level: info -allow-lan: false -external-controller: 127.0.0.1:9090 -mode: rule -secret: "" diff --git a/src-tauri/resources/item_tmp.yaml b/src-tauri/resources/item_tmp.yaml deleted file mode 100644 index 72cdd93..0000000 --- a/src-tauri/resources/item_tmp.yaml +++ /dev/null @@ -1,16 +0,0 @@ -# Profile Template for clash verge - -# the profile's name -name: New Profile - -# the description of this profile(optional) -description: - -# proxies defination (optional, the same as clash) -proxies: - -# proxy-groups (optional, the same as clash) -proxy-groups: - -# rules (optional, the same as clash) -rules: diff --git a/src-tauri/resources/profiles_tmp.yaml b/src-tauri/resources/profiles_tmp.yaml deleted file mode 100644 index 4787f0e..0000000 --- a/src-tauri/resources/profiles_tmp.yaml +++ /dev/null @@ -1,3 +0,0 @@ -# Profiles Config for Clash Verge - -current: 0 diff --git a/src-tauri/resources/verge_tmp.yaml b/src-tauri/resources/verge_tmp.yaml deleted file mode 100644 index 39b754b..0000000 --- a/src-tauri/resources/verge_tmp.yaml +++ /dev/null @@ -1,6 +0,0 @@ -# Defaulf Config For Clash Verge - -theme_mode: light -enable_self_startup: false -enable_system_proxy: false -system_proxy_bypass: localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*; diff --git a/src-tauri/src/core/profiles.rs b/src-tauri/src/core/profiles.rs index f01c49d..1f7f055 100644 --- a/src-tauri/src/core/profiles.rs +++ b/src-tauri/src/core/profiles.rs @@ -1,5 +1,5 @@ use super::{Clash, ClashInfo}; -use crate::utils::{config, dirs}; +use crate::utils::{config, dirs, tmpl}; use reqwest::header::HeaderMap; use serde::{Deserialize, Serialize}; use serde_yaml::{Mapping, Value}; @@ -155,16 +155,7 @@ impl Profiles { let file = format!("{}.yaml", now); let path = dirs::app_home_dir().join("profiles").join(&file); - let file_data = b"# Profile Template for clash verge\n -# proxies defination (optional, the same as clash) -proxies:\n -# proxy-groups (optional, the same as clash) -proxy-groups:\n -# rules (optional, the same as clash) -rules:\n\n -"; - - match File::create(&path).unwrap().write(file_data) { + match File::create(&path).unwrap().write(tmpl::ITEM_CONFIG) { Ok(_) => { items.push(ProfileItem { name: Some(name), diff --git a/src-tauri/src/utils/init.rs b/src-tauri/src/utils/init.rs index 7171b1f..ce5ed6c 100644 --- a/src-tauri/src/utils/init.rs +++ b/src-tauri/src/utils/init.rs @@ -1,6 +1,6 @@ extern crate serde_yaml; -use crate::utils::dirs; +use crate::utils::{dirs, tmpl}; use chrono::Local; use log::LevelFilter; use log4rs::append::console::ConsoleAppender; @@ -41,44 +41,22 @@ fn init_log(log_dir: &PathBuf) { } /// Initialize all the files from resources -fn init_config_file(app_dir: &PathBuf, res_dir: &PathBuf) { +fn init_config(app_dir: &PathBuf) -> std::io::Result<()> { // target path let clash_path = app_dir.join("config.yaml"); let verge_path = app_dir.join("verge.yaml"); let profile_path = app_dir.join("profiles.yaml"); - let mmdb_path = app_dir.join("Country.mmdb"); - - // template path - let clash_tmpl = res_dir.join("config_tmp.yaml"); - let verge_tmpl = res_dir.join("verge_tmp.yaml"); - let profiles_tmpl = res_dir.join("profiles_tmp.yaml"); - let mmdb_tmpl = res_dir.join("Country.mmdb"); if !clash_path.exists() { - if clash_tmpl.exists() { - fs::copy(clash_tmpl, clash_path).unwrap(); - } else { - // make sure that the config.yaml not null - let content = b"\ - mixed-port: 7890\n\ - log-level: info\n\ - allow-lan: false\n\ - external-controller: 127.0.0.1:9090\n\ - secret: \"\"\n"; - File::create(clash_path).unwrap().write(content).unwrap(); - } + File::create(clash_path)?.write(tmpl::CLASH_CONFIG)?; } - - // only copy it - if !verge_path.exists() && verge_tmpl.exists() { - fs::copy(verge_tmpl, verge_path).unwrap(); + if !verge_path.exists() { + File::create(verge_path)?.write(tmpl::VERGE_CONFIG)?; } - if !profile_path.exists() && profiles_tmpl.exists() { - fs::copy(profiles_tmpl, profile_path).unwrap(); - } - if !mmdb_path.exists() && mmdb_tmpl.exists() { - fs::copy(mmdb_tmpl, mmdb_path).unwrap(); + if !profile_path.exists() { + File::create(profile_path)?.write(tmpl::PROFILES_CONFIG)?; } + Ok(()) } /// initialize app @@ -101,5 +79,14 @@ pub fn init_app(package_info: &PackageInfo) { } init_log(&log_dir); - init_config_file(&app_dir, &res_dir); + if let Err(err) = init_config(&app_dir) { + log::error!("{err}"); + } + + // copy the resource file + let mmdb_path = app_dir.join("Country.mmdb"); + let mmdb_tmpl = res_dir.join("Country.mmdb"); + if !mmdb_path.exists() && mmdb_tmpl.exists() { + fs::copy(mmdb_tmpl, mmdb_path).unwrap(); + } } diff --git a/src-tauri/src/utils/mod.rs b/src-tauri/src/utils/mod.rs index a5fe22b..be971d5 100644 --- a/src-tauri/src/utils/mod.rs +++ b/src-tauri/src/utils/mod.rs @@ -5,3 +5,4 @@ pub mod init; pub mod resolve; pub mod server; pub mod sysopt; +pub mod tmpl; diff --git a/src-tauri/src/utils/tmpl.rs b/src-tauri/src/utils/tmpl.rs new file mode 100644 index 0000000..8759c24 --- /dev/null +++ b/src-tauri/src/utils/tmpl.rs @@ -0,0 +1,38 @@ +///! Some config file template + +/// template for clash core `config.yaml` +pub const CLASH_CONFIG: &[u8] = br#"# Default Config For Clash Core + +mixed-port: 7890 +log-level: info +allow-lan: false +external-controller: 127.0.0.1:9090 +mode: rule +secret: "" +"#; + +/// template for `profiles.yaml` +pub const PROFILES_CONFIG: &[u8] = b"# Profiles Config for Clash Verge + +current: 0 +items: ~ +"; + +/// template for `verge.yaml` +pub const VERGE_CONFIG: &[u8] = b"# Defaulf Config For Clash Verge + +theme_mode: light +enable_self_startup: false +enable_system_proxy: false +system_proxy_bypass: localhost;127.*;10.*;192.168.*; +"; + +/// template for new a profile item +pub const ITEM_CONFIG: &[u8] = b"# Profile Template for clash verge\n\n +# proxies defination (optional, the same as clash) +proxies:\n +# proxy-groups (optional, the same as clash) +proxy-groups:\n +# rules (optional, the same as clash) +rules:\n\n +";