-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
247 lines (228 loc) · 7.75 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use anyhow::{anyhow, Context, Result};
use clap::{App, Arg};
use regex::{Captures, Regex};
use std::{fs, io, process};
include!(concat!(env!("OUT_DIR"), "/buildinfo.rs"));
struct Config {
pub input_path: Option<String>,
pub output_path: Option<String>,
pub exec: String,
pub no_headers: bool,
pub delimiter: String,
pub out_delimiter: Option<String>,
pub quote: String,
pub arg_regex: String,
pub new_column_name: String,
}
fn main() -> Result<()> {
let matches = App::new("csv-exec")
.version(BUILDINFO_VERSION)
.author("niladic <git@nil.choron.cc>")
.about("Execute a command on each record of a CSV.")
.arg(
Arg::with_name("input")
.short("i")
.long("input")
.value_name("FILE")
.help("Input CSV file [stdin by default]")
.takes_value(true),
)
.arg(
Arg::with_name("output")
.short("o")
.long("output")
.value_name("FILE")
.help("Output CSV [stdout by default]")
.takes_value(true),
)
.arg(
Arg::with_name("exec")
.index(1)
.value_name("COMMAND")
.required(true)
.help("The command to execute")
.takes_value(true),
)
.arg(
Arg::with_name("no-headers")
.short("n")
.long("no-headers")
.help("Do not read the first line as a header line")
.takes_value(false),
)
.arg(
Arg::with_name("delimiter")
.short("d")
.long("delimiter")
.value_name("CHAR")
.default_value(",")
.help("CSV delimiter (\\t for tabs)")
.takes_value(true),
)
.arg(
Arg::with_name("out-delimiter")
.long("out-delimiter")
.value_name("CHAR")
.help("Output CSV delimiter, if different from delimiter (\\t for tabs)")
.takes_value(true),
)
.arg(
Arg::with_name("quote")
.long("quote")
.value_name("CHAR")
.default_value("\"")
.help("CSV quote")
.takes_value(true),
)
.arg(
Arg::with_name("arg-regex")
.long("arg-regex")
.value_name("REGEX")
.default_value(r"\$([0-9]+)")
.help(
"
Regex used to parse the column position in the command args.
Position begins at 1.
Only the first capturing group is used.
Syntax: https://docs.rs/regex/1.3.4/regex/index.html#syntax
"
.trim_start(),
)
.takes_value(true),
)
.arg(
Arg::with_name("new-column-name")
.long("new-column-name")
.value_name("STRING")
.default_value("Result")
.help("Name of the new column which contains the results")
.takes_value(true),
)
.get_matches();
let config = Config {
input_path: matches.value_of("input").map(String::from),
output_path: matches.value_of("output").map(String::from),
// Note: required using clap
exec: matches
.value_of("exec")
.map(String::from)
.unwrap_or_else(String::new),
no_headers: matches.is_present("no-headers"),
delimiter: matches
.value_of("delimiter")
.map(String::from)
.unwrap_or_else(String::new),
out_delimiter: matches.value_of("out-delimiter").map(String::from),
quote: matches
.value_of("quote")
.map(String::from)
.unwrap_or_else(String::new),
arg_regex: matches
.value_of("arg-regex")
.map(String::from)
.unwrap_or_else(String::new),
new_column_name: matches
.value_of("new-column-name")
.map(String::from)
.unwrap_or_else(String::new),
};
run(config)
}
fn run(config: Config) -> Result<()> {
let reader: Box<dyn io::Read> = match config.input_path {
None => Box::new(io::stdin()),
Some(path) => Box::new(fs::File::open(&path).context(format!("Failed to open {}", path))?),
};
let writer: Box<dyn io::Write> = match config.output_path {
None => Box::new(io::stdout()),
Some(path) => {
Box::new(fs::File::create(&path).context(format!("Failed to create {}", path))?)
}
};
let read_one_ascii_char = |value: &str| -> Result<u8> {
if value.bytes().count() > 1 {
return Err(anyhow!("Value {} must be 1 ASCII character", value));
}
match value.chars().next() {
None => Err(anyhow!("Missing value")),
Some(c) => {
if c.is_ascii() {
Ok(c as u8)
} else {
Err(anyhow!("Value {} must be 1 ASCII character", value))
}
}
}
};
let read_delimiter = |value: &str| -> Result<u8> {
if value == r"\t" {
Ok(b'\t')
} else {
read_one_ascii_char(value)
}
};
let delimiter: u8 = read_delimiter(&config.delimiter)?;
let out_delimiter: u8 = config
.out_delimiter
.map(|d| read_delimiter(&d))
.transpose()?
.unwrap_or(delimiter);
let quote: u8 = read_one_ascii_char(&config.quote)?;
let variable_regex = Regex::new(&config.arg_regex)?;
let cmd_and_args: Vec<String> = shell_words::split(&config.exec)?;
let mut csv_reader = csv::ReaderBuilder::new()
.has_headers(!config.no_headers)
.delimiter(delimiter)
.quote(quote)
.from_reader(reader);
let mut csv_writer = csv::WriterBuilder::new()
.delimiter(out_delimiter)
.quote(quote)
.from_writer(writer);
if !config.no_headers {
let new_headers = csv_reader.headers()?.clone();
csv_writer.write_record(
new_headers
.iter()
.chain(vec![&*config.new_column_name].into_iter()),
)?;
}
for record in csv_reader.records() {
let mut record = record?;
let mut args_iter = cmd_and_args.iter();
let command = match args_iter.next() {
None => return Err(anyhow!("No command to execute")),
Some(command) => command,
};
let args = args_iter
.map(|arg| {
variable_regex
.replace_all(arg, |caps: &Captures| {
let record_value = caps
.get(1)
.and_then(|position| position.as_str().parse::<usize>().ok())
// Column position begins at 1
.and_then(|position| position.checked_sub(1))
.and_then(|position| record.get(position));
match record_value {
None => "",
Some(value) => value,
}
})
.to_string()
})
.collect::<Vec<_>>();
let output = process::Command::new(command)
.args(&args)
.output()
.context(format!(
"Failed to execute command {} with args {:?}",
command, args
))?;
let out = std::str::from_utf8(&output.stdout)?.trim();
record.push_field(&out);
csv_writer.write_record(record.iter())?;
}
csv_writer.flush()?;
Ok(())
}