finds run in path

master
Avril 5 years ago
parent 385d191f27
commit a9cf444d39
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,6 +1,9 @@
use std::{ use std::{
error, error,
fmt, fmt,
path::{
PathBuf,
},
}; };
#[cfg(feature = "broken-pipe")] #[cfg(feature = "broken-pipe")]
@ -14,6 +17,8 @@ pub struct ProcessArgs
{ {
pub name: String, pub name: String,
pub args: Vec<String>, pub args: Vec<String>,
name_real: Option<PathBuf>, //get => `.name_path()`
} }
mod proc_args; mod proc_args;
@ -74,17 +79,17 @@ impl Operation
&self.outputs &self.outputs
} }
pub fn validate(&self) -> Result<(), String> pub fn validate(&mut self) -> Result<(), String>
{ {
if !self.output_to_tty && self.outputs.len()<1 { if !self.output_to_tty && self.outputs.len()<1 {
return Err("Need at least one output.".to_owned()); return Err("Need at least one output.".to_owned());
} }
self.input.verify().or_else(|err| Err(format!("bad input <{}>: {}", &self.input, err)))?; self.input.name_real = Some(self.input.verify().or_else(|err| Err(format!("bad input <{}>: {}", &self.input, err)))?);
for x in self.outputs.iter() for x in self.outputs.iter_mut()
{ {
x.verify().or_else(|err| Err(format!("bad output <{}>: {}", &x, err)))?; x.name_real = Some(x.verify().or_else(|err| Err(format!("bad output <{}>: {}", &x, err)))?);
} }
Ok(()) Ok(())

@ -1,4 +1,7 @@
use super::*; use super::*;
use std::{
path::{Path,PathBuf,},
};
impl ProcessArgs impl ProcessArgs
{ {
@ -23,14 +26,97 @@ impl ProcessArgs
Ok(Self{ Ok(Self{
name, name,
args, args,
name_real: None,
}) })
} }
pub fn verify(&self) -> Result<(), &'static str> pub fn name_path(&self) -> Result<&Path, &'static str>
{ {
//TODO: Check file path of executable if let Some(buf) = &self.name_real {
Ok(()) Ok(buf.as_ref())
} else {
Err("No name_path specified, have you called `verify()`?")
}
}
pub fn verify(&self) -> Result<PathBuf, &'static str>
{
if let Ok(spath) = std::env::var("PATH") {
if let Some(path) = find_somewhere(&self.name, spath.split(':')) {
Ok(path)
} else {
Err("File not found")
}
} else if Path::new(&self.name).exists() {
Ok(Path::new(&self.name).to_owned())
} else {
Err("File not found and $PATH not found.")
}
} }
} }
//TODO: impl fmt::Display for ProcessArgs fn find_somewhere<T,U,V>(name: T, places: U) -> Option<PathBuf>
where T: AsRef<str>,
U: Iterator<Item=V>,
V: AsRef<str>
{
let name = name.as_ref();
let pbuf = Path::new(&name);
if pbuf.exists() {
return Some(pbuf.to_owned())
}
for place in places
{
let place = place.as_ref();
let pbuf = Path::new(&place).join(&name);
if pbuf.exists() {
return Some(pbuf);
}
}
None
}
fn escape<T>(s: &T) -> String
where T: AsRef<str> + ?Sized
{
let s = s.as_ref();
let mut o = String::with_capacity(s.len());
for c in s.chars() {
match c {
'\\' => {
o.push('\\');
o.push('\\');
},
'\'' => {
o.push('\\');
o.push('\'');
},
other => o.push(other),
};
}
o
}
use std::fmt;
impl fmt::Display for ProcessArgs
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "{} ", self.name)?;
let len = self.args.len();
for (i,x) in (0..len).zip(self.args.iter()) {
write!(f, "{}", &x)?;
if i < len-1 {
write!(f, " ")?;
}
}
Ok(())
}
}

@ -10,7 +10,8 @@ mod arg;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let op = arg::parse()?; let op = arg::parse()?;
//println!("{:?}", op); yay! //TODO: Open input
println!("{:?}", op);
Ok(()) Ok(())
} }

Loading…
Cancel
Save