this repo has no description
at main 1.8 kB view raw
1use std::{io::Stdin, process::{Command, Stdio}}; 2 3use crate::{ 4 runtime::nodes::RuntimeNode, 5 structs::{nodes::Node, parameter_types::ParameterType}, 6}; 7 8pub struct ShellCommand { 9 outputs: Vec<Vec<(String, isize, isize)>>, 10 inputs: Vec<Option<(String, isize, isize)>> 11} 12 13impl ShellCommand { 14 pub fn new(node: Node) -> Box<Self> { 15 Box::new(Self { 16 outputs: node.outputs.iter().map(|x| { 17 x.connections.iter() 18 .map(|x| (x.node.clone(), x.index, x.value_type)).collect()}).collect(), 19 20 inputs: node.inputs.iter().map(|x| { 21 let y = x.connections.get(0); 22 if let Some(y) = y{ 23 Some((y.node.clone(), y.index, y.value_type)) 24 } else{ 25 None 26 } 27 }).collect() 28 }) 29 } 30} 31 32impl RuntimeNode for ShellCommand { 33 fn outputs(&self) -> Vec<Vec<(String, isize, isize)>> { 34 self.outputs.clone() 35 } 36 37 fn inputs(&self) -> Vec<Option<(String, isize, isize)>> { 38 self.inputs.clone() 39 } 40 41 fn execute(&mut self, args: Vec<ParameterType>) -> Vec<ParameterType> { 42 if let Ok(cmd) = args[1].as_string(){ 43 if cmd != ""{ 44 let mut split_cmd = cmd.split(" "); 45 46 let mut cmd = Command::new(split_cmd.nth(0).unwrap()); 47 if split_cmd.clone().count() > 0{ cmd.args(split_cmd); } 48 49 cmd.stdout(Stdio::piped()); 50 cmd.stderr(Stdio::piped()); 51 52 let child = cmd.spawn().unwrap(); 53 let output = child.wait_with_output().unwrap(); 54 55 vec![ 56 ParameterType::Flow(true), 57 ParameterType::String(str::from_utf8(&output.stdout).unwrap().to_owned()) 58 ] 59 } else{ 60 vec![ ParameterType::Flow(false) ] 61 } 62 } else{ 63 vec![ ParameterType::Flow(false) ] 64 } 65 } 66 67 fn is_entrypoint(&self) -> bool { 68 false 69 } 70}