Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Usage:
terc dns <name> [--type A] [--json]
terc explain <target> [--json]
terc sysproxy on|off [--yes] [--json]
terc core start|stop [--json] start or stop the managed core
terc notify-test [--json] verify desktop notifications
terc help

Expand Down Expand Up @@ -59,6 +60,7 @@ pub fn dispatch(args: &[String]) -> Option<i32> {
"dns" => dns(rest, json),
"explain" => explain(rest, json),
"sysproxy" => sysproxy(rest, json),
"core" => core(rest, json),
"notify-test" => notify_test(json),
"help" | "?" => {
print!("{USAGE}");
Expand Down Expand Up @@ -307,8 +309,16 @@ fn mode(args: &[String], json: bool) -> i32 {
};
match mihomo::switch_routing_mode(&config.controller, normalized) {
Ok(()) => {
// A reload or a core restart reads the file's mode, not the running
// one, so record the switch the way the TUI's mode key does.
let persisted = mihomo::persist_mode(&config.controller, &normalized.to_lowercase());
if let Err(err) = &persisted {
eprintln!("terc: warning: mode {normalized} not saved to config.yaml: {err}");
}
if json {
print_json(&json!({ "ok": true, "mode": normalized }));
print_json(
&json!({ "ok": true, "mode": normalized, "persisted": persisted.is_ok() }),
);
} else {
println!("mode {normalized}");
}
Expand All @@ -321,6 +331,38 @@ fn mode(args: &[String], json: bool) -> i32 {
}
}

fn core(args: &[String], json: bool) -> i32 {
let Some(action) = args
.iter()
.find(|arg| !arg.starts_with("--"))
.map(|arg| arg.to_lowercase())
else {
return usage_error("core needs start or stop");
};
let Ok(config) = load_config() else {
return 1;
};
let result = match action.as_str() {
"start" => mihomo::start_core_service(&config.controller),
"stop" => mihomo::stop_core_service(&config.controller),
_ => return usage_error("core takes start or stop"),
};
match result {
Ok(message) => {
if json {
print_json(&json!({ "ok": true, "action": action, "message": message }));
} else {
println!("{message}");
}
0
}
Err(err) => {
eprintln!("terc: {err}");
1
}
}
}

fn test(args: &[String], json: bool) -> i32 {
let Some(group) = args.iter().find(|arg| !arg.starts_with("--")) else {
return usage_error("test needs a group name");
Expand Down
Loading