From 0ab168b7f710b9bfc2b00f1647eefe3aaa5f26c6 Mon Sep 17 00:00:00 2001 From: George Kaklamanos Date: Tue, 28 Nov 2023 16:50:12 +0200 Subject: [PATCH] Create `labadoor-common` and move `run_bin()` in it --- Cargo.toml | 1 + labadoor-common/Cargo.toml | 6 ++++++ labadoor-common/src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ labadoor-open/Cargo.toml | 1 + labadoor-open/src/lib.rs | 36 +++++++----------------------------- 5 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 labadoor-common/Cargo.toml create mode 100644 labadoor-common/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index fefc551..bdb1877 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "labadoor-csv", "labadoor", "labadoor-auth", + "labadoor-common", "labadoor-open", "labadoor-acl", ] diff --git a/labadoor-common/Cargo.toml b/labadoor-common/Cargo.toml new file mode 100644 index 0000000..8175d08 --- /dev/null +++ b/labadoor-common/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "labadoor-common" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/labadoor-common/src/lib.rs b/labadoor-common/src/lib.rs new file mode 100644 index 0000000..fff1cca --- /dev/null +++ b/labadoor-common/src/lib.rs @@ -0,0 +1,38 @@ +pub type Binary = Vec; +pub type BinaryResult = Result; + +pub struct OpenBinaryArgs { + pub method: String, + pub identifier: String, + pub resource_shortcut: i8, +} + +pub fn run_bin(bin: Binary) -> BinaryResult { + use std::{ + io::Read, + process::{Command, Stdio}, + }; + + let mut ret = Err("".to_string()); + let mut iter = bin.iter(); + let mut cmd = Command::new(iter.next().unwrap()); + cmd.args(iter); + + let mut child = cmd.stdout(Stdio::piped()).spawn().unwrap(); + let mut success = child.wait().unwrap().success(); + + let mut s = String::from(""); + child.stdout.unwrap().read_to_string(&mut s).unwrap(); + s = String::from(s.trim()); + + ret = if success { Ok(s) } else { Err(s) }; + ret +} + +pub fn run_open(args: OpenBinaryArgs, bin: Binary) -> BinaryResult { + let mut auth_bin = bin.clone(); + auth_bin.push(args.method.clone()); + auth_bin.push(args.identifier.clone()); + auth_bin.push(args.resource_shortcut.to_string()); + run_bin(auth_bin) +} diff --git a/labadoor-open/Cargo.toml b/labadoor-open/Cargo.toml index 16e2704..ba02780 100644 --- a/labadoor-open/Cargo.toml +++ b/labadoor-open/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +labadoor-common = { path = "../labadoor-common"} diff --git a/labadoor-open/src/lib.rs b/labadoor-open/src/lib.rs index 13a334f..d809f87 100644 --- a/labadoor-open/src/lib.rs +++ b/labadoor-open/src/lib.rs @@ -17,37 +17,15 @@ struct AuthResult { pub resource: String, } -fn run_bin(bin: Binary) -> Result { - use std::{ - io::Read, - process::{Command, Stdio}, - }; - - let mut ret = Err("".to_string()); - let mut iter = bin.iter(); - let mut cmd = Command::new(iter.next().unwrap()); - cmd.args(iter); - - let mut child = cmd.stdout(Stdio::piped()).spawn().unwrap(); - let mut success = child.wait().unwrap().success(); - - let mut s = String::from(""); - child.stdout.unwrap().read_to_string(&mut s).unwrap(); - s = String::from(s.trim()); - - ret = if success { Ok(s) } else { Err(s) }; - ret -} - fn run_auth(args: &OpenArgs, bin: Binary) -> Result { let mut ret = Err("".to_string()); - let mut auth_bin = bin.clone(); - auth_bin.push(args.method.clone()); - auth_bin.push(args.identifier.clone()); - auth_bin.push(args.resource_shortcut.to_string()); - let output = run_bin(auth_bin); - + let a = labadoor_common::OpenBinaryArgs { + method: args.method.clone(), + identifier: args.identifier.clone(), + resource_shortcut: args.resource_shortcut, + }; + let output = labadoor_common::run_open(a, bin); if let Ok(out) = output { let s: Vec = out.split(',').map(|s| String::from(s)).collect(); ret = Ok(AuthResult { @@ -66,7 +44,7 @@ pub fn open(args: OpenArgs) -> Result<(), ()> { let output = run_auth(&args, binary.to_vec()); if let Ok(user) = output { let resource_bin = args.hardware.get(&user.resource).unwrap(); - if let Ok(_) = run_bin(resource_bin.to_vec()) { + if let Ok(_) = labadoor_common::run_bin(resource_bin.to_vec()) { msg = "Open sesame!"; ret = Ok(()); } else {