forked from dathere/qsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeach.rs
198 lines (158 loc) · 6.8 KB
/
foreach.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![cfg(target_family = "unix")]
static USAGE: &str = r#"
Execute a shell command once per line in given CSV file. Only works in
Linux, macOS and other Unix-like environments (i.e. not Windows).
Deleting all files whose filenames are listed in a column:
$ qsv foreach filename 'rm {}' assets.csv
Executing a command that outputs CSV once per line without repeating headers:
$ qsv foreach query 'search --year 2020 {}' queries.csv > results.csv
Same as above but with an additional column containing the current value:
$ qsv foreach query -c from_query 'search {}' queries.csv > results.csv
For more examples, see https://github.com/jqnatividad/qsv/blob/master/tests/test_foreach.rs.
Usage:
qsv foreach [options] <column> <command> [<input>]
qsv foreach --help
foreach arguments:
column The column to use as input for the command.
command The command to execute. Use "{}" to substitute the value
of the current input file line.
input The CSV file to read. If not provided, will read from stdin.
foreach options:
-u, --unify If the output of execute command is CSV, will
unify the result by skipping headers on each
subsequent command.
-c, --new-column <name> If unifying, add a new column with given name
and copying the value of the current input file line.
Common options:
-h, --help Display this message
-n, --no-headers When set, the file will be considered to have no
headers.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
-p, --progressbar Show progress bars. Not valid for stdin.
"#;
use std::{
ffi::OsStr,
io::BufReader,
os::unix::ffi::OsStrExt,
process::{Command, Stdio},
};
use indicatif::{ProgressBar, ProgressDrawTarget};
use regex::bytes::{NoExpand, Regex};
use serde::Deserialize;
use crate::{
config::{Config, Delimiter},
select::SelectColumns,
util, CliResult,
};
#[derive(Deserialize)]
struct Args {
arg_column: SelectColumns,
arg_command: String,
arg_input: Option<String>,
flag_unify: bool,
flag_new_column: Option<String>,
flag_no_headers: bool,
flag_delimiter: Option<Delimiter>,
flag_progressbar: bool,
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let rconfig = Config::new(&args.arg_input)
.delimiter(args.flag_delimiter)
.no_headers(args.flag_no_headers)
.select(args.arg_column);
let mut rdr = rconfig.reader()?;
let mut wtr = Config::new(&None).writer()?;
#[allow(clippy::trivial_regex)]
// template_pattern matches pairs of curly braces, e.g. "{}".
let template_pattern = Regex::new(r"\{\}")?;
// splitter_pattern gets all the arguments to the command as tokens.
// The regular expression matches any sequence of characters that consists of one or more word
// characters (`a-z`, `A-Z`, `0-9`, `_`, `.`, `+`, `-`), or any of the following three types of
// quoted strings: double-quoted strings ("..."), single-quoted strings ('...'), or
// backtick-quoted strings (`...`).
let splitter_pattern = Regex::new(r#"(?:[a-zA-Z0-9_.+-]+|"[^"]*"|'[^']*'|`[^`]*`)"#)?;
// cleaner_pattern removes the quotes or backticks from the quoted strings matched by
// splitter_pattern.
let cleaner_pattern = Regex::new(r#"(?:^["'`]|["'`]$)"#)?;
let headers = rdr.byte_headers()?.clone();
let sel = rconfig.selection(&headers)?;
let column_index = *sel.iter().next().unwrap();
let mut record = csv::ByteRecord::new();
let mut output_headers_written = false;
// prep progress bar
let show_progress =
(args.flag_progressbar || util::get_envvar_flag("QSV_PROGRESSBAR")) && !rconfig.is_stdin();
let progress = ProgressBar::with_draw_target(None, ProgressDrawTarget::stderr_with_hz(5));
if show_progress {
util::prep_progress(&progress, util::count_rows(&rconfig)?);
} else {
progress.set_draw_target(ProgressDrawTarget::hidden());
}
while rdr.read_byte_record(&mut record)? {
if show_progress {
progress.inc(1);
}
let current_value = &record[column_index];
let templated_command = template_pattern
.replace_all(args.arg_command.as_bytes(), current_value)
.to_vec();
#[allow(unused_mut)]
let mut command_pieces = splitter_pattern.find_iter(&templated_command);
let prog = OsStr::from_bytes(command_pieces.next().unwrap().as_bytes());
let cmd_args: Vec<String> = command_pieces
.map(|piece| {
let clean_piece = cleaner_pattern.replace_all(piece.as_bytes(), NoExpand(b""));
simdutf8::basic::from_utf8(&clean_piece)
.unwrap_or_default()
.to_string()
})
.collect();
if args.flag_unify {
let mut cmd = Command::new(prog)
.args(cmd_args)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()?;
{
let stdout = cmd.stdout.as_mut().unwrap();
let stdout_reader = BufReader::new(stdout);
let mut stdout_rdr = csv::ReaderBuilder::new()
.delimiter(match &args.flag_delimiter {
Some(delimiter) => delimiter.as_byte(),
None => b',',
})
.has_headers(true)
.from_reader(stdout_reader);
let mut output_record = csv::ByteRecord::new();
if !output_headers_written {
let mut headers = stdout_rdr.byte_headers()?.clone();
if let Some(name) = &args.flag_new_column {
headers.push_field(name.as_bytes());
}
wtr.write_byte_record(&headers)?;
output_headers_written = true;
}
while stdout_rdr.read_byte_record(&mut output_record)? {
if args.flag_new_column.is_some() {
output_record.push_field(current_value);
}
wtr.write_byte_record(&output_record)?;
}
}
cmd.wait()?;
} else {
let mut cmd = Command::new(prog)
.args(cmd_args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?;
cmd.wait()?;
}
}
if show_progress {
util::finish_progress(&progress);
}
Ok(())
}