|
|
@ -13,6 +13,7 @@ use yargs::{DEFAULT_SEP_PATTERN, stdin};
|
|
|
|
use yargs::parse::InputText;
|
|
|
|
use yargs::parse::InputText;
|
|
|
|
use anyhow::Result;
|
|
|
|
use anyhow::Result;
|
|
|
|
use std::io::{BufRead, Read, BufReader, stdin, Write};
|
|
|
|
use std::io::{BufRead, Read, BufReader, stdin, Write};
|
|
|
|
|
|
|
|
use std::os;
|
|
|
|
use std::process::{self, Command, Stdio};
|
|
|
|
use std::process::{self, Command, Stdio};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -30,7 +31,7 @@ use std::process::{self, Command, Stdio};
|
|
|
|
#[command(author="blob42")]
|
|
|
|
#[command(author="blob42")]
|
|
|
|
#[command(version="0.1")]
|
|
|
|
#[command(version="0.1")]
|
|
|
|
struct Cli {
|
|
|
|
struct Cli {
|
|
|
|
/// Regex used to to split input into columns
|
|
|
|
/// Regex used for splitting the input into columns
|
|
|
|
#[arg(default_value=DEFAULT_SEP_PATTERN)]
|
|
|
|
#[arg(default_value=DEFAULT_SEP_PATTERN)]
|
|
|
|
#[arg(short)]
|
|
|
|
#[arg(short)]
|
|
|
|
delimiter: String,
|
|
|
|
delimiter: String,
|
|
|
@ -110,7 +111,49 @@ fn main() -> Result<()> {
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Handle yargs
|
|
|
|
// Handle yargs
|
|
|
|
// For each columns of text, execute the arg command on
|
|
|
|
// For each columns of text, execute the arg command on
|
|
|
|
// the columns lines (xargs)
|
|
|
|
// the column lines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// naive implementation
|
|
|
|
|
|
|
|
// TODO: map input columns into output processed commands
|
|
|
|
|
|
|
|
for (i, yarg) in cli.yargs.into_iter().enumerate() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// let yarg_cmd = &yarg.split_whitespace().nth(0).unwrap_or(&yarg);
|
|
|
|
|
|
|
|
// dbg!(yarg_cmd);
|
|
|
|
|
|
|
|
// let yarg_args: Vec<&str> = yarg.split_whitespace().skip(1).collect();
|
|
|
|
|
|
|
|
// dbg!(yarg_args);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for each yarg split into cmd, args
|
|
|
|
|
|
|
|
let mut yarg_cmd = Command::new(&yarg.split_whitespace().nth(0).unwrap_or(&yarg))
|
|
|
|
|
|
|
|
.args(yarg.split_whitespace().skip(1))
|
|
|
|
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
|
|
|
|
.spawn()
|
|
|
|
|
|
|
|
.expect(&format!("Failed to exec {yarg}"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut yarg_stdin = yarg_cmd.stdin.take().expect("failed to open stdin");
|
|
|
|
|
|
|
|
let _columns = columns.clone();
|
|
|
|
|
|
|
|
// yarg_stdin.write_all(columns[i].join("\n").as_bytes())?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO!: use https://crates.io/crates/subprocess to spawn shell
|
|
|
|
|
|
|
|
// expl: using a long `awk ..` will fail if args are split on space the cmd needs to be
|
|
|
|
|
|
|
|
// passed as is to a subshell
|
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
|
|
|
yarg_stdin.write_all(_columns[i].join("\n").as_bytes()).expect("Failed to write to stdin");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// gather output from child
|
|
|
|
|
|
|
|
let output = yarg_cmd.wait_with_output()
|
|
|
|
|
|
|
|
.expect(format!("failed to read stdout for {}", yarg).as_str());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: gather output into column similar to input print into column format
|
|
|
|
|
|
|
|
println!("{}", String::from_utf8_lossy(&output.stdout));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process::exit(0);
|
|
|
|
|
|
|
|
|
|
|
|
// we know we have at least one elm
|
|
|
|
// we know we have at least one elm
|
|
|
|
let yarg = cli.yargs.first().unwrap();
|
|
|
|
let yarg = cli.yargs.first().unwrap();
|
|
|
|