feat: inline config file template

This commit is contained in:
GyDi 2022-02-18 23:57:13 +08:00
parent a13d4698be
commit 3a9a392a77
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
8 changed files with 59 additions and 75 deletions

View File

@ -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: ""

View File

@ -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:

View File

@ -1,3 +0,0 @@
# Profiles Config for Clash Verge
current: 0

View File

@ -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.*;<local>

View File

@ -1,5 +1,5 @@
use super::{Clash, ClashInfo}; use super::{Clash, ClashInfo};
use crate::utils::{config, dirs}; use crate::utils::{config, dirs, tmpl};
use reqwest::header::HeaderMap; use reqwest::header::HeaderMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_yaml::{Mapping, Value}; use serde_yaml::{Mapping, Value};
@ -155,16 +155,7 @@ impl Profiles {
let file = format!("{}.yaml", now); let file = format!("{}.yaml", now);
let path = dirs::app_home_dir().join("profiles").join(&file); let path = dirs::app_home_dir().join("profiles").join(&file);
let file_data = b"# Profile Template for clash verge\n match File::create(&path).unwrap().write(tmpl::ITEM_CONFIG) {
# 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) {
Ok(_) => { Ok(_) => {
items.push(ProfileItem { items.push(ProfileItem {
name: Some(name), name: Some(name),

View File

@ -1,6 +1,6 @@
extern crate serde_yaml; extern crate serde_yaml;
use crate::utils::dirs; use crate::utils::{dirs, tmpl};
use chrono::Local; use chrono::Local;
use log::LevelFilter; use log::LevelFilter;
use log4rs::append::console::ConsoleAppender; use log4rs::append::console::ConsoleAppender;
@ -41,44 +41,22 @@ fn init_log(log_dir: &PathBuf) {
} }
/// Initialize all the files from resources /// 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 // target path
let clash_path = app_dir.join("config.yaml"); let clash_path = app_dir.join("config.yaml");
let verge_path = app_dir.join("verge.yaml"); let verge_path = app_dir.join("verge.yaml");
let profile_path = app_dir.join("profiles.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_path.exists() {
if clash_tmpl.exists() { File::create(clash_path)?.write(tmpl::CLASH_CONFIG)?;
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();
} }
if !verge_path.exists() {
File::create(verge_path)?.write(tmpl::VERGE_CONFIG)?;
} }
if !profile_path.exists() {
// only copy it File::create(profile_path)?.write(tmpl::PROFILES_CONFIG)?;
if !verge_path.exists() && verge_tmpl.exists() {
fs::copy(verge_tmpl, verge_path).unwrap();
}
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();
} }
Ok(())
} }
/// initialize app /// initialize app
@ -101,5 +79,14 @@ pub fn init_app(package_info: &PackageInfo) {
} }
init_log(&log_dir); 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();
}
} }

View File

@ -5,3 +5,4 @@ pub mod init;
pub mod resolve; pub mod resolve;
pub mod server; pub mod server;
pub mod sysopt; pub mod sysopt;
pub mod tmpl;

View File

@ -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.*;<local>
";
/// 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
";