mirror of
https://github.com/nushell/nushell.git
synced 2025-01-27 08:38:44 +01:00
Allow save to accept a list of strings (#4743)
This commit is contained in:
parent
1527b34d9c
commit
4aa9a18c63
@ -105,6 +105,20 @@ impl Command for Save {
|
|||||||
|
|
||||||
Ok(PipelineData::new(span))
|
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(
|
v => Err(ShellError::UnsupportedInput(
|
||||||
format!("{:?} not supported", v.get_type()),
|
format!("{:?} not supported", v.get_type()),
|
||||||
span,
|
span,
|
||||||
@ -126,6 +140,20 @@ impl Command for Save {
|
|||||||
|
|
||||||
Ok(PipelineData::new(span))
|
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(
|
v => Err(ShellError::UnsupportedInput(
|
||||||
format!("{:?} not supported", v.get_type()),
|
format!("{:?} not supported", v.get_type()),
|
||||||
span,
|
span,
|
||||||
|
@ -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]
|
#[test]
|
||||||
fn save_append_will_create_file_if_not_exists() {
|
fn save_append_will_create_file_if_not_exists() {
|
||||||
Playground::setup("save_test_3", |dirs, sandbox| {
|
Playground::setup("save_test_3", |dirs, sandbox| {
|
||||||
|
Loading…
Reference in New Issue
Block a user