Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new rename command to change the headers #5

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod index;
pub mod input;
pub mod join;
pub mod partition;
pub mod rename;
pub mod reverse;
pub mod sample;
pub mod search;
Expand Down
65 changes: 65 additions & 0 deletions src/cmd/rename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use csv;

use CliResult;
use config::{Config, Delimiter};
use util;

static USAGE: &'static str = "
Rename the columns of CSV data efficiently.

This command lets you rename the columns in CSV data. You must specify
all of the headers, and separate them by a comma.

Change the name of the columns:
$ xsv rename id,name,title

Use column names that contains commas and conflict with the separator:
$ xsv rename '\"Date - Opening\",\"Date - Actual Closing\"'

Usage:
xsv rename [options] [--] <headers> [<input>]
xsv rename --help

Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
";

#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
arg_headers: String,
flag_output: Option<String>,
flag_delimiter: Option<Delimiter>,
}

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);

let mut rdr = rconfig.reader()?;
let mut wtr = Config::new(&args.flag_output).writer()?;
let headers = rdr.byte_headers()?;

let mut new_rdr = csv::Reader::from_reader(args.arg_headers.as_bytes());
let new_headers = new_rdr.byte_headers()?;

if headers.len() != new_headers.len() {
return fail!("The length of the CSV headers is different from the provided one.");
}

if !rconfig.no_headers {
wtr.write_record(new_headers)?;
}

let mut record = csv::ByteRecord::new();
while rdr.read_byte_record(&mut record)? {
wtr.write_record(&record)?;
}
wtr.flush()?;
Ok(())
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ enum Command {
Input,
Join,
Partition,
Rename,
Reverse,
Sample,
Search,
Expand All @@ -171,7 +172,7 @@ impl Command {

if !argv[1].chars().all(char::is_lowercase) {
return Err(CliError::Other(format!(
"xsv expects commands in lowercase. Did you mean '{}'?",
"xsv expects commands in lowercase. Did you mean '{}'?",
argv[1].to_lowercase()).to_string()));
}
match self {
Expand All @@ -187,6 +188,7 @@ impl Command {
Command::Input => cmd::input::run(argv),
Command::Join => cmd::join::run(argv),
Command::Partition => cmd::partition::run(argv),
Command::Rename => cmd::rename::run(argv),
Command::Reverse => cmd::reverse::run(argv),
Command::Sample => cmd::sample::run(argv),
Command::Search => cmd::search::run(argv),
Expand Down