Allow save to accept a list of strings (#4743)

This commit is contained in:
Genna Wingert 2022-03-05 22:56:04 +01:00 committed by GitHub
parent 1527b34d9c
commit 4aa9a18c63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -105,6 +105,20 @@ impl Command for Save {
Ok(PipelineData::new(span))
}
Value::List { vals, .. } => {
let val = vals
.into_iter()
.map(|it| it.as_string())
.collect::<Result<Vec<String>, ShellError>>()?
.join("\n")
+ "\n";
if let Err(err) = file.write_all(val.as_bytes()) {
return Err(ShellError::IOError(err.to_string()));
}
Ok(PipelineData::new(span))
}
v => Err(ShellError::UnsupportedInput(
format!("{:?} not supported", v.get_type()),
span,
@ -126,6 +140,20 @@ impl Command for Save {
Ok(PipelineData::new(span))
}
Value::List { vals, .. } => {
let val = vals
.into_iter()
.map(|it| it.as_string())
.collect::<Result<Vec<String>, ShellError>>()?
.join("\n")
+ "\n";
if let Err(err) = file.write_all(val.as_bytes()) {
return Err(ShellError::IOError(err.to_string()));
}
Ok(PipelineData::new(span))
}
v => Err(ShellError::UnsupportedInput(
format!("{:?} not supported", v.get_type()),
span,

View File

@ -49,6 +49,24 @@ fn writes_out_csv() {
})
}
#[test]
fn writes_out_list() {
Playground::setup("save_test_3", |dirs, sandbox| {
sandbox.with_files(vec![]);
let expected_file = dirs.test().join("list_sample.txt");
nu!(
cwd: dirs.root(),
r#"echo [a b c d] | save save_test_3/list_sample.txt"#,
);
let actual = file_contents(expected_file);
println!("{actual}");
assert_eq!(actual, "a\nb\nc\nd\n")
})
}
#[test]
fn save_append_will_create_file_if_not_exists() {
Playground::setup("save_test_3", |dirs, sandbox| {