From cfe0f2f06858737e22ff4e514d7a0fb2da9e5bd6 Mon Sep 17 00:00:00 2001
From: Michael Gattozzi <mgattozzi@gmail.com>
Date: Tue, 13 Oct 2015 19:27:54 -0400
Subject: [PATCH] Command Execution

Ion now can execute single commands passed to it, including multiple flags and arguments. The result is an struct from which various things can be derived from, such as stderr and stdout. This can be expanded as more things are needed.

On branch master
Changes to be committed:
	new file:   src/command.rs
	modified:   src/lib.rs
---
 src/command.rs | 28 ++++++++++++++++++++++++++++
 src/lib.rs     | 16 +++++++++++++++-
 2 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 src/command.rs

diff --git a/src/command.rs b/src/command.rs
new file mode 100644
index 00000000..871cdb34
--- /dev/null
+++ b/src/command.rs
@@ -0,0 +1,28 @@
+use std::process::{Command,Output};
+
+pub struct InstructionOut {
+    pub stdout: String,
+    pub stderr: String,
+}
+
+pub fn run(input_command: Vec<&str>) -> Option<InstructionOut> {
+    let args = input_command.as_slice();
+    let length = args.len();
+    let output: Option<Output>;
+    if length ==0 {
+        output = Command::new("").output().ok();
+    } else if length ==  1 {
+        output = Command::new(&args[0]).output().ok();
+    } else {
+        output = Command::new(&args[0]).args(&args[1..]).output().ok();
+    };
+    if output.is_some() {
+        let output = output.unwrap();
+        Some(InstructionOut {
+            stdout: String::from_utf8(output.stdout).ok().expect("No stdout"),
+            stderr: String::from_utf8(output.stderr).ok().expect("No stderr"),
+        })
+    } else {
+        None
+    }
+}
diff --git a/src/lib.rs b/src/lib.rs
index cd3c8c0e..c59145e5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,7 +1,21 @@
+#![feature(convert)]
+pub mod command;
+
 use std::io;
+use command::*;
 
 pub fn repl() {
     let mut input = String::new();
     let _unused = io::stdin().read_line(&mut input);
-    println!("You typed: {}", input.trim());
+    let out_wrap = run(input.trim().split_whitespace().collect::<Vec<&str>>());
+    if out_wrap.is_some() {
+        let out = out_wrap.unwrap();
+        if out.stdout.is_empty() {
+            println!("{}",out.stderr.trim());
+        } else {
+            println!("{}",out.stdout.trim());
+        }
+    } else {
+        println!("{} is not a valid command", input.trim());
+    }
 }
-- 
GitLab