diff options
author | Rory Dudley | 2024-02-17 19:52:06 -0700 |
---|---|---|
committer | Rory Dudley | 2024-02-17 19:52:06 -0700 |
commit | 6cf0f1b12e880f3cd88f013a070406bfb303831a (patch) | |
tree | 3921891730623f4eb9fab25fae3a03a96d4c0cb3 | |
parent | 7fbd8e662e8f66937d647ec1f687dd8f7946de49 (diff) | |
download | dwarvish-6cf0f1b12e880f3cd88f013a070406bfb303831a.tar.gz |
Call programs from full and relative paths
Allow user to input a fullpath or relative path to a file that should be
forked to.
Notes
Notes:
Currently, this does not check whether or not that file at the path
specified is executable or not, but, if it isn't we will throw an
'Unable to fork' error, before printing the next prompt.
-rw-r--r-- | src/main.rs | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index 5f73a15..7fc8991 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use ctrlc; use std::fs; use std::io; use std::io::Write; +use std::path::Path; use std::process::Command; fn eval(paths: &[&str], prompt: &str) { @@ -42,7 +43,7 @@ fn eval(paths: &[&str], prompt: &str) { // Parse command and arguments let mut split = input.split(' '); - let cmd = match split.next() { + let mut cmd = match split.next() { Some(str) if str.trim().is_empty() => continue, Some(str) => str.trim(), None => continue, @@ -74,14 +75,19 @@ fn eval(paths: &[&str], prompt: &str) { } } - // Check if the command exists - let cmd = match bins.iter().find(|b| b.split("/").last().unwrap() == cmd) { - Some(cmd) => cmd, - None => { - println!("Command not found"); - continue; - } - }; + // Check if the file exists, if given a pull or relative path + // TODO: Check if file at the path is executable (i.e. +x) + if !Path::new(cmd).exists() { + // Check if the command exists in $PATH if a full or relative path + // was not given, or if the path does not exist + cmd = match bins.iter().find(|b| b.split("/").last().unwrap() == cmd) { + Some(cmd) => cmd, + None => { + println!("Command not found"); + continue; + } + }; + } // Run the command (and wait for it to finish) let mut child = match Command::new(cmd).args(args).spawn() { |