refactor(config): 统一共享配置模板来源

This commit is contained in:
2026-09-19 08:00:55 +08:00
parent 56ad014199
commit e6622b32c7
7 changed files with 141 additions and 186 deletions
+11 -92
View File
@@ -13,94 +13,8 @@ use std::time::Duration;
pub(crate) const CONFIG_FILE_NAME: &str = "config.toml";
pub(crate) const CONFIG_EXAMPLE_FILE_NAME: &str = "config.toml.example";
const CONFIG_TEMPLATE: &str = r#"# BlueArchive Toolkit 配置文件(bat 首次启动自动生成)
#
# `config.toml` 位于 bat 二进制所在目录。存在时会被读取并生效。
# 优先级:CLI > 进程环境变量 > config.toml > 内置默认值。
# `config.toml.example` 只是模板,程序不会自动读取它作为实际配置。
#
# 字符串建议使用单引号,路径/URL 更容易直接复制。
[runtime]
state_dir = '/tmp/bat-pid'
interval_seconds = 3600
error_retry_seconds = 60
quiet_up_to_date = false
output_format = 'human'
banner = true
progress = true
tail_lines = 200
[resource]
output_root = './bat-resources'
auto_discover = true
app_version = ''
connection_group = ''
launcher_version = '1.7.2'
platforms = ['windows', 'android']
snapshot_path = ''
dry_run = false
plan = false
force = false
audit_local = true
repair = true
[resource.server_info]
kind = 'none'
value = ''
[localized]
output_root = './bat-localized'
[repository]
import_repository = false
import_cas_root = ''
import_resource_repository_path = ''
[network]
curl_command = 'curl'
proxy = 'auto'
unzip_command = 'unzip'
zip_command = 'zip'
download_concurrency = 8
[translation.worker]
provider = 'mock'
fixture = ''
translation_memory_path = ''
glossary_path = ''
concurrency = 8
max_attempts = 3
lease_seconds = 300
retry_backoff_seconds = 5
max_tasks = ''
worker_id = ''
# Go bat-api consumes this shared section. Rust bat ignores it.
[api]
listen = ':18080'
public_base_url = 'http://127.0.0.1:18080'
state_dir = '/tmp/bat-pid'
socket_path = ''
resource_root = ''
server_info_file = ''
require_indexed = true
verify_size = true
rpc_timeout = '30s'
refresh_interval = '1m'
auth_token = ''
auth_query_param = 'bat_token'
auth_exempt_paths = []
trust_proxy_headers = false
access_log = false
rate_limit_rps = 0
rate_limit_burst = 0
max_resource_page_limit = 1000
database_url = ''
database_password = ''
redis_url = ''
redis_password = ''
"#;
const CONFIG_TEMPLATE: &str =
include_str!("../../../../internal/configtemplate/config.toml.example");
#[derive(Debug, Clone, Default)]
pub(crate) struct BatConfigFile {
@@ -1068,6 +982,14 @@ mod tests {
assert!(options.config.server_info_source.is_none());
}
#[test]
fn shared_template_matches_checked_in_bat_api_example() {
assert_eq!(
CONFIG_TEMPLATE,
include_str!("../../../../cmd/bat-api/config.toml.example")
);
}
#[test]
fn config_file_is_written_when_missing() {
let temp = TempDir::new().unwrap();
@@ -1076,10 +998,7 @@ mod tests {
let example = temp.path().join(CONFIG_EXAMPLE_FILE_NAME);
assert!(example.exists());
let content = fs::read_to_string(example).unwrap();
let parsed = parse_config_document(&content).unwrap();
let mut options = CliOptions::default();
parsed.apply_to_options(&mut options).unwrap();
assert_eq!(options.config.output_root, PathBuf::from("./bat-resources"));
assert_eq!(content, CONFIG_TEMPLATE);
}
#[test]