From abe8f306373a69275194125a2b499cc0cd89eccb Mon Sep 17 00:00:00 2001 From: Gerhard Dutka Date: Wed, 18 May 2022 06:34:17 +0200 Subject: [PATCH 1/2] Add Clap crate and a binary Date: Wed May 18 06:34:17 2022 +0200 --- Cargo.toml | 1 + README.md | 8 ++++++++ src/bin/wake.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/bin/wake.rs diff --git a/Cargo.toml b/Cargo.toml index 8a0dc8c..c68bcc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ categories = ["network-programming"] [dependencies] hex = "~0.3" +clap = { version = "3.1.18", features = ["derive"] } diff --git a/README.md b/README.md index 6b2bf78..dfd04ec 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,11 @@ let dst = SocketAddr::from(([255,255,255,255], 9)); wol.send_magic_to(src, dst); ``` + +## Included binary + +The binary `bin/wake` may be directly used in scripts: + +``` +wake -m 00:11:22:33:44:55 +``` diff --git a/src/bin/wake.rs b/src/bin/wake.rs new file mode 100644 index 0000000..93c07bf --- /dev/null +++ b/src/bin/wake.rs @@ -0,0 +1,28 @@ +use clap::Parser; + +#[derive(Parser)] +#[clap(about = "WakeOnLan a device. https://github.com/LesnyRumcajs/wakey.git", long_about = None)] +struct CmdArgs { + /// mac address to send packet to + #[clap(short, long)] + mac: Option, +} +fn main() { + let args = CmdArgs::parse(); + if let Some(m) = args.mac { + let mut sep: char = '/'; + [":", "-"].iter().for_each(|s| { + if m.contains(s) { + sep = s.chars().next().unwrap(); + } + }); + let wol = wakey::WolPacket::from_string(&m, sep); + if wol.send_magic().is_ok() { + println!("sent the magic packet."); + } else { + println!("failed to send the magic packet."); + } + } else { + println!("give mac address to wake up"); + } +} From c71ef11df22887fcded7c9b242ac23cdaf22677d Mon Sep 17 00:00:00 2001 From: Gerhard Dutka Date: Tue, 24 May 2022 12:10:01 +0200 Subject: [PATCH 2/2] wake: simplify separator check as suggested --- src/bin/wake.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/bin/wake.rs b/src/bin/wake.rs index 93c07bf..0c08148 100644 --- a/src/bin/wake.rs +++ b/src/bin/wake.rs @@ -10,12 +10,7 @@ struct CmdArgs { fn main() { let args = CmdArgs::parse(); if let Some(m) = args.mac { - let mut sep: char = '/'; - [":", "-"].iter().for_each(|s| { - if m.contains(s) { - sep = s.chars().next().unwrap(); - } - }); + let sep = m.chars().find(|ch| *ch == ':' || *ch == '-').unwrap_or('/'); let wol = wakey::WolPacket::from_string(&m, sep); if wol.send_magic().is_ok() { println!("sent the magic packet.");