Skip to content

Commit

Permalink
headers: now also accepts process_input - dir input; infile-list;…
Browse files Browse the repository at this point in the history
… multiple input; auto decompress snappy
  • Loading branch information
jqnatividad committed Dec 26, 2023
1 parent 54977d8 commit fc56590
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/cmd/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ Usage:
qsv headers [options] [<input>...]
qsv headers --help
headers arguments:
<input>... The CSV file(s) to read. Use '-' for standard input.
If input is a directory, all files in the directory will
be read as input.
If the input is a file with a '.infile-list' extension,
the file will be read as a list of input files.
If the input are snappy-compressed files(s), it will be
decompressed automatically.
headers options:
-j, --just-names Only show the header names (hide column index).
This is automatically enabled if more than one
Expand All @@ -27,7 +36,7 @@ Common options:
Must be a single character. (default: ,)
"#;

use std::io;
use std::{io, path::PathBuf};

use serde::Deserialize;
use tabwriter::TabWriter;
Expand All @@ -36,15 +45,17 @@ use crate::{config::Delimiter, util, CliResult};

#[derive(Deserialize)]
struct Args {
arg_input: Vec<String>,
arg_input: Vec<PathBuf>,
flag_just_names: bool,
flag_intersect: bool,
flag_trim: bool,
flag_delimiter: Option<Delimiter>,
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let mut args: Args = util::get_args(USAGE, argv)?;
let tmpdir = tempfile::tempdir()?;
args.arg_input = util::process_input(args.arg_input, &tmpdir, "")?;
let configs = util::many_configs(&args.arg_input, args.flag_delimiter, true, false)?;

let num_inputs = configs.len();
Expand Down
55 changes: 55 additions & 0 deletions tests/test_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,58 @@ h2
h3";
assert_eq!(got, expected.to_string());
}

#[test]
fn headers_infile() {
let wrk = Workdir::new("headers_infile");
wrk.create("in1.csv", vec![svec!["a", "b", "c"], svec!["1", "2", "3"]]);

wrk.create("in2.csv", vec![svec!["c", "d", "e"], svec!["3", "1", "2"]]);

wrk.create(
"in3.csv",
vec![svec!["a", "b", "f", "g"], svec!["1", "2", "4", "3"]],
);

wrk.create_from_string("testdata.infile-list", "in1.csv\nin2.csv\nin3.csv\n");

let mut cmd = wrk.command("headers");
cmd.arg("testdata.infile-list");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
["a"],
["b"],
["c"],
["c"],
["d"],
["e"],
["a"],
["b"],
["f"],
["g"],
];
assert_eq!(got, expected);
}

#[test]
fn headers_intersect_infile() {
let wrk = Workdir::new("headers_intersect_infile");
wrk.create("in1.csv", vec![svec!["a", "b", "c"], svec!["1", "2", "3"]]);

wrk.create("in2.csv", vec![svec!["c", "d", "e"], svec!["3", "1", "2"]]);

wrk.create(
"in3.csv",
vec![svec!["a", "b", "f", "g"], svec!["1", "2", "4", "3"]],
);

wrk.create_from_string("testdata.infile-list", "in1.csv\nin2.csv\nin3.csv\n");

let mut cmd = wrk.command("headers");
cmd.arg("--intersect").arg("testdata.infile-list");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"]];
assert_eq!(got, expected);
}

0 comments on commit fc56590

Please sign in to comment.