Create labadoor-common and move run_bin() in it

This commit is contained in:
George Kaklamanos 2023-11-28 16:50:12 +02:00
parent 924224c7fa
commit 0ab168b7f7
No known key found for this signature in database
GPG key ID: C0CAB8A6BDC9399D
5 changed files with 53 additions and 29 deletions

View file

@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"
[dependencies]
labadoor-common = { path = "../labadoor-common"}

View file

@ -17,37 +17,15 @@ struct AuthResult {
pub resource: String,
}
fn run_bin(bin: Binary) -> Result<String, String> {
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<AuthResult, String> {
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<String> = 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 {