fop(1): fixed trimming and handled unwraps (closes #58)
This commit is contained in:
parent
d3470233ea
commit
f8e3013563
36
src/fop.rs
36
src/fop.rs
@ -26,7 +26,7 @@ extern crate sysexits;
|
||||
extern crate getopt;
|
||||
|
||||
use getopt::{ Opt, Parser };
|
||||
use sysexits::{ EX_DATAERR, EX_USAGE };
|
||||
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
|
||||
|
||||
fn main() {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
@ -60,17 +60,24 @@ fn main() {
|
||||
});
|
||||
|
||||
let mut buf = String::new();
|
||||
stdin().read_to_string(&mut buf).unwrap();
|
||||
let _ = stdin().read_to_string(&mut buf);
|
||||
let mut fields = buf.split(d).collect::<Vec<&str>>();
|
||||
|
||||
let opts = argv.iter().clone().skip(command_arg + 1).collect::<Vec<&String>>();
|
||||
let opts = argv
|
||||
.iter()
|
||||
.clone()
|
||||
.skip(command_arg + 1)
|
||||
.collect::<Vec<&String>>();
|
||||
|
||||
let mut spawned = Command::new(argv.get(command_arg).unwrap())
|
||||
.args(opts)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
.unwrap_or_else( |_| {
|
||||
eprintln!("{}: {}: Command not found.", argv[0], argv[command_arg]);
|
||||
exit(EX_UNAVAILABLE);
|
||||
});
|
||||
|
||||
let field = fields.get(index).unwrap_or_else(|| {
|
||||
eprintln!(
|
||||
@ -82,15 +89,28 @@ fn main() {
|
||||
});
|
||||
|
||||
if let Some(mut child_stdin) = spawned.stdin.take() {
|
||||
child_stdin.write_all(field.as_bytes()).unwrap();
|
||||
let _ = child_stdin.write_all(field.as_bytes());
|
||||
drop(child_stdin);
|
||||
}
|
||||
|
||||
let output = spawned.wait_with_output().unwrap();
|
||||
let output = spawned.wait_with_output().unwrap_or_else(|| {
|
||||
eprintln!("{}: {}: Command has no output.",
|
||||
argv[0],
|
||||
argv[command_arg]
|
||||
);
|
||||
exit(EX_IOERR);
|
||||
});
|
||||
|
||||
let new_field = String::from_utf8(output.stdout).unwrap();
|
||||
let new_field = String::from_utf8(output.stdout).unwrap_or_else(|_| {
|
||||
eprintln!(
|
||||
"{}: {}: Command output is invalid UTF-8.",
|
||||
argv[0],
|
||||
argv[command_arg]
|
||||
);
|
||||
exit(EX_IOERR);
|
||||
});
|
||||
|
||||
fields[index] = &new_field;
|
||||
fields[index] = &new_field.trim_end();
|
||||
|
||||
print!("{}", fields.join(&d.to_string()));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user