Skip to content

Commit dffbf25

Browse files
author
messica
committed
feat: support export clash config
1 parent cd54cae commit dffbf25

File tree

5 files changed

+565
-105
lines changed

5 files changed

+565
-105
lines changed

src/commands/cli.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ pub enum Commands {
3838
#[arg(short, long, value_name = "PATH")]
3939
output: Option<PathBuf>,
4040

41-
/// Output format
42-
#[arg(short, long, value_name = "FORMAT", value_enum, default_value_t = OutputFormat::Json)]
43-
format: OutputFormat,
44-
45-
/// Target output protocol (sing-box, clash, v2ray). Currently only sing-box is supported.
46-
/// The output format and default filename will be automatically determined based on the protocol.
41+
/// Target output protocol (sing-box, clash, v2ray).
42+
/// The output format is determined by the protocol:
43+
/// - sing-box: JSON only
44+
/// - clash: YAML only
45+
/// - v2ray: JSON only
4746
#[arg(long = "output-protocol", value_name = "PROTOCOL")]
4847
output_protocol: Option<String>,
4948

src/commands/convert.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl ConvertCommand {
122122
std::fs::read_to_string(template_path).map_err(|e| ConvertError::IoError(e))?;
123123
template_engine.process_template(&template_content)?
124124
} else {
125-
Self::generate_default_config(&template_engine)?
125+
Self::generate_default_config(&template_engine, output_protocol)?
126126
};
127127

128128
// Get output format and filename based on protocol
@@ -222,12 +222,17 @@ impl ConvertCommand {
222222
}
223223
}
224224

225-
/// generate default config template (sing-box format)
225+
/// generate default config template based on output protocol
226226
fn generate_default_config(
227227
template_engine: &template_engine::TemplateEngine,
228+
output_protocol: &OutputProtocol,
228229
) -> Result<String> {
229-
// Get default sing-box template from the protocol module
230-
let template_str = singbox::generate_default_template();
230+
// Get default template from the protocol module based on output protocol
231+
let template_str = match output_protocol {
232+
OutputProtocol::SingBox => singbox::generate_default_template(),
233+
OutputProtocol::Clash => protocols::clash::generate_default_template(),
234+
OutputProtocol::V2Ray => protocols::v2ray::generate_default_template(),
235+
};
231236

232237
// Process template to replace interpolation rules like {{ALL-TAG}}
233238
template_engine.process_template(&template_str)
@@ -292,24 +297,11 @@ pub async fn handle_convert(
292297

293298
let output_protocol = OutputProtocol::from_str(output_protocol_str).ok_or_else(|| {
294299
ConvertError::ConfigValidationError(format!(
295-
"不支持的输出协议: {},支持的协议: sing-box(singbox), clash, v2ray。当前仅支持 sing-box",
300+
"Unsupported output protocol: {}, supported protocols: sing-box(singbox), clash, v2ray",
296301
output_protocol_str
297302
))
298303
})?;
299304

300-
// 验证当前仅支持 sing-box
301-
match output_protocol {
302-
OutputProtocol::SingBox => {
303-
// 允许
304-
}
305-
OutputProtocol::Clash | OutputProtocol::V2Ray => {
306-
return Err(ConvertError::ConfigValidationError(format!(
307-
"输出协议 {} 暂不支持,当前仅支持 sing-box",
308-
output_protocol_str
309-
)));
310-
}
311-
}
312-
313305
// 合并 output:CLI > 配置文件 > 默认值
314306
let final_output: Option<String> = output
315307
.as_ref()

0 commit comments

Comments
 (0)