From e5e897b6ad3f9b3df6403b9bb15182d2b1b6f2b4 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Thu, 5 Aug 2021 07:34:55 -0600
Subject: [PATCH] Add output parameter to exec

---
 Cargo.lock   |  2 +-
 Cargo.toml   |  2 +-
 src/cargo.rs | 17 +++++++++++++++--
 src/exec.rs  | 22 ++++++++++++++++++----
 4 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 539c17b..96820b0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -993,7 +993,7 @@ dependencies = [
 
 [[package]]
 name = "redoxer"
-version = "0.2.20"
+version = "0.2.21"
 dependencies = [
  "dirs",
  "proc-mounts",
diff --git a/Cargo.toml b/Cargo.toml
index 8b24511..8238ecb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "redoxer"
-version = "0.2.20"
+version = "0.2.21"
 description = "Method for quickly running programs inside of Redox from a KVM capable OS."
 license = "MIT"
 authors = ["Jeremy Soller <jackpot51@gmail.com>"]
diff --git a/src/cargo.rs b/src/cargo.rs
index fab8e48..59590ef 100644
--- a/src/cargo.rs
+++ b/src/cargo.rs
@@ -30,11 +30,20 @@ fn inner<I: Iterator<Item=String>>(mut args: I) -> io::Result<()> {
     let mut arguments = Vec::new();
     let mut matching = true;
     let mut gui = false;
+    let mut output_opt = None;
     while let Some(arg) = args.next() {
         match arg.as_str() {
             "-g" | "--gui" if matching => {
                 gui = true;
             },
+            "-o" | "--output" if matching => match args.next() {
+                Some(output) => {
+                    output_opt = Some(output);
+                },
+                None => {
+                    //TODO: usage();
+                },
+            },
             "--" if matching => {
                 matching = false;
             },
@@ -46,9 +55,13 @@ fn inner<I: Iterator<Item=String>>(mut args: I) -> io::Result<()> {
 
     // TODO: Ensure no spaces in command
     let runner = format!(
-        "{} exec --folder .{}",
+        "{} exec --folder .{}{}",
         command,
-        if gui { " --gui" } else { "" }
+        if gui { " --gui" } else { "" },
+        match output_opt {
+            Some(output) => format!(" --output {}", output),
+            None => String::new(),
+        }
     );
 
     crate::env::command("cargo")?
diff --git a/src/exec.rs b/src/exec.rs
index 682eff0..0ff7877 100644
--- a/src/exec.rs
+++ b/src/exec.rs
@@ -145,7 +145,7 @@ fn archive_free_space(disk_path: &Path, folder_path: &Path, bootloader_path: &Pa
     Ok(())
 }
 
-fn inner(arguments: &[String], folder_opt: Option<String>, gui: bool) -> io::Result<i32> {
+fn inner(arguments: &[String], folder_opt: Option<String>, gui: bool, output_opt: Option<String>) -> io::Result<i32> {
     let kvm = Path::new("/dev/kvm").exists();
     if ! installed("qemu-system-x86_64")? {
         eprintln!("redoxer: qemu-system-x86 not found, please install before continuing");
@@ -340,7 +340,11 @@ fn inner(arguments: &[String], folder_opt: Option<String>, gui: bool) -> io::Res
             }
         };
 
-        print!("{}", fs::read_to_string(&redoxer_log)?);
+        if let Some(output) = output_opt {
+            fs::copy(&redoxer_log, output)?;
+        } else {
+            print!("{}", fs::read_to_string(&redoxer_log)?);
+        }
 
         code
     };
@@ -351,7 +355,7 @@ fn inner(arguments: &[String], folder_opt: Option<String>, gui: bool) -> io::Res
 }
 
 fn usage() {
-    eprintln!("redoxer exec [-f|--folder folder] [-g|--gui] [-h|--help] [--] <command> [arguments]...");
+    eprintln!("redoxer exec [-f|--folder folder] [-g|--gui] [-h|--help] [-o|--output file] [--] <command> [arguments]...");
     process::exit(1);
 }
 
@@ -362,6 +366,8 @@ pub fn main(args: &[String]) {
     let mut folder_opt = None;
     // Run with GUI
     let mut gui = false;
+    // File to put command output into
+    let mut output_opt = None;
     // Arguments to pass to command
     let mut arguments = Vec::new();
 
@@ -383,6 +389,14 @@ pub fn main(args: &[String]) {
             "-h" | "--help" if matching => {
                 usage();
             },
+            "-o" | "--output" if matching => match args.next() {
+                Some(output) => {
+                    output_opt = Some(output);
+                },
+                None => {
+                    usage();
+                },
+            },
             // TODO: "-p" | "--package"
             "--" if matching => {
                 matching = false;
@@ -398,7 +412,7 @@ pub fn main(args: &[String]) {
         usage();
     }
 
-    match inner(&arguments, folder_opt, gui) {
+    match inner(&arguments, folder_opt, gui, output_opt) {
         Ok(code) => {
             process::exit(code);
         },
-- 
GitLab