From 9c06520622ccc9662fd97a8e0457bc03b4dc4db6 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 20 Sep 2025 21:39:52 +0800 Subject: wip --- src/tasks.rs | 136 +++-------------------------------------------------------- 1 file changed, 6 insertions(+), 130 deletions(-) (limited to 'src/tasks.rs') diff --git a/src/tasks.rs b/src/tasks.rs index b979d4f..3e76ce4 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -1,145 +1,21 @@ -use std::fmt::Display; - use serde::Deserialize; pub type TaskOutput = Result; +pub mod client; +pub mod server; -pub struct Task { - pub name: Name, +pub struct Task { + pub name: N, pub exec: fn(&[&str]) -> TaskOutput, } #[derive(Deserialize)] -pub struct CompletedTask { - pub name: Name, +pub struct CompletedTask { + pub name: N, pub output: Option, } -#[derive(Debug, Deserialize, Clone, Copy)] -pub enum Name { - Nop, - Exec, - Network, -} - -impl Display for Name { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "{:?}", self) - } -} - -pub const TASKS: &[Task] = &[Task { name: Name::Nop, exec: no_op }, Task { name: Name::Exec, exec: exec }, NETWORK_T]; - -pub const NETWORK_T: Task = Task { name: Name::Network, exec: network }; - -fn no_op(_: &[&str]) -> Result { - Ok("".to_string()) -} - -fn exec(cmds: &[&str]) -> Result { - use std::process::Command; - // TODO: fmt and add exit code, and add support for both stdout and stderr, and - // support for no output - match Command::new(cmds[0]).args(&cmds[1..]).output() { - Ok(o) => { - if o.status.success() { - Ok(String::from_utf8_lossy(&o.stdout).to_string()) - } else { - Err(String::from_utf8_lossy(&o.stderr).to_string()) - } - } - Err(e) => Err(e.to_string()), - } -} - -fn network(switch: &[&str]) -> Result { - use std::process::{Command, Stdio}; - let switch: bool = switch[0].parse::().map_err(|e| e.to_string())?; - - // TODO: windows and linux - - // todo let .. = Command::new('networksetup') - #[cfg(target_os = "macos")] - { - use std::io::Read; - match Command::new("networksetup").arg("-listallhardwareports").stdout(Stdio::piped()).spawn() { - Ok(devs) => { - match Command::new("awk") - .arg(r#"/Wi-Fi|AirPort/{getline; print $2}"#) - .stdin(Stdio::from(match devs.stdout { - Some(s) => s, - None => match devs.stderr { - Some(mut e) => { - let mut reason = String::new(); - if let Err(e) = e.read_to_string(&mut reason) { - return Err(format!("networksetup failed to list hardware: {e}")); - } - return Err(format!("networksetup failed to list hardware: {reason}")); - } - None => Err("networksetup failed to list hardware".to_string())?, - }, - })) - .stdout(Stdio::piped()) - .spawn() - { - Ok(dev) => match Command::new("networksetup") - .arg("-setairportpower") - .arg(match dev.stdout { - Some(mut o) => { - let mut output = String::new(); - if let Err(e) = o.read_to_string(&mut output) { - return Err(format!("awk failed to get device from networksetup list hardware: {e}")); - }; - output - } - None => match dev.stderr { - Some(mut e) => { - let mut reason = String::new(); - if let Err(e) = e.read_to_string(&mut reason) { - return Err(format!("awk failed to get device from networksetup list hardware: {e}")); - } - return Err(format!("awk failed to get device from networksetup list hardware: {reason}")); - } - None => Err("awk failed to get device from networksetup list hardware".to_string())?, - }, - }) - .arg(if switch { "on" } else { "off" }) - .output() - { - Ok(o) => { - if o.status.success() { - Ok(String::from_utf8_lossy(&o.stdout).to_string()) - } else { - Err(String::from_utf8_lossy(&o.stderr).to_string()) - } - } - Err(e) => Err(e.to_string()), - }, - Err(e) => Err(e.to_string()), - } - } - Err(e) => Err(e.to_string()), - } - } - - #[cfg(not(target_os = "macos"))] - Err("not implemented".to_string()) -} - #[derive(Debug)] pub enum IntoTaskErr { NotFound(String), } - -impl TryFrom<&str> for &'static Task { - type Error = IntoTaskErr; - - fn try_from(value: &str) -> Result { - for task in TASKS { - if task.name.to_string() == value { - return Ok(task); - } - } - Err(IntoTaskErr::NotFound(value.to_string())) - } -} -- cgit v1.2.3