clash-verge/src-tauri/src/utils/clash.rs

64 lines
1.6 KiB
Rust
Raw Normal View History

2021-12-08 18:40:52 +03:00
extern crate log;
2021-12-04 09:31:26 +03:00
use crate::{
config::read_clash_controller,
events::emit::{clash_start, ClashInfoPayload},
utils::app_home_dir,
};
use tauri::{
api::process::{Command, CommandEvent},
AppHandle,
};
2021-12-04 09:31:26 +03:00
/// Run the clash bin
pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
2021-12-08 18:40:52 +03:00
let app_dir = app_home_dir();
let mut payload = ClashInfoPayload {
status: "success".to_string(),
controller: None,
message: None,
};
2021-12-08 18:40:52 +03:00
match Command::new_sidecar("clash") {
Ok(cmd) => match cmd
.args(["-d", &app_dir.as_os_str().to_str().unwrap()])
.spawn()
{
Ok((mut rx, _)) => {
log::info!("Successfully execute clash sidecar");
payload.controller = Some(read_clash_controller());
2021-12-04 09:31:26 +03:00
tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await {
match event {
CommandEvent::Stdout(line) => log::info!("{}", line),
CommandEvent::Stderr(err) => log::error!("{}", err),
_ => {}
}
}
});
2021-12-04 09:31:26 +03:00
}
Err(err) => {
log::error!(
"Failed to execute clash sidecar for \"{:?}\"",
err.to_string()
);
payload.status = "error".to_string();
payload.message = Some(err.to_string());
}
},
Err(err) => {
log::error!(
"Failed to execute clash sidecar for \"{:?}\"",
err.to_string()
);
payload.status = "error".to_string();
payload.message = Some(err.to_string());
2021-12-04 09:31:26 +03:00
}
};
clash_start(app_handle, &payload);
payload
2021-12-04 09:31:26 +03:00
}