2022-01-15 20:44:20 +00:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2022-02-21 12:25:41 +00:00
|
|
|
use nu_protocol::{
|
2022-10-20 12:56:44 +00:00
|
|
|
Category, Example, PipelineData, RawStream, ShellError, Signature, Span, Spanned, SyntaxShape,
|
|
|
|
Value,
|
2022-02-21 12:25:41 +00:00
|
|
|
};
|
2022-10-20 12:56:44 +00:00
|
|
|
use std::fs::File;
|
2022-03-27 02:39:27 +00:00
|
|
|
use std::io::{BufWriter, Write};
|
2022-01-15 20:44:20 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Save;
|
|
|
|
|
|
|
|
impl Command for Save {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"save"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Save a file."
|
|
|
|
}
|
|
|
|
|
2022-04-23 13:54:03 +00:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
2022-06-03 09:35:31 +00:00
|
|
|
vec![
|
|
|
|
"write",
|
|
|
|
"write_file",
|
|
|
|
"append",
|
|
|
|
"redirection",
|
|
|
|
"file",
|
|
|
|
"io",
|
|
|
|
">",
|
|
|
|
">>",
|
|
|
|
]
|
2022-04-23 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 20:44:20 +00:00
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
|
|
Signature::build("save")
|
|
|
|
.required("filename", SyntaxShape::Filepath, "the filename to use")
|
2022-10-20 12:56:44 +00:00
|
|
|
.named(
|
|
|
|
"stderr",
|
|
|
|
SyntaxShape::Filepath,
|
|
|
|
"the filename used to save stderr, only works with `-r` flag",
|
|
|
|
Some('e'),
|
|
|
|
)
|
2022-02-21 12:25:41 +00:00
|
|
|
.switch("raw", "save file as raw binary", Some('r'))
|
2022-09-17 12:02:17 +00:00
|
|
|
.switch("append", "append input to the end of the file", Some('a'))
|
2022-12-01 12:26:17 +00:00
|
|
|
.switch("force", "overwrite the destination", Some('f'))
|
2022-01-15 20:44:20 +00:00
|
|
|
.category(Category::FileSystem)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
|
|
|
let raw = call.has_flag("raw");
|
2022-03-05 19:36:58 +00:00
|
|
|
let append = call.has_flag("append");
|
2022-12-01 12:26:17 +00:00
|
|
|
let force = call.has_flag("force");
|
2022-01-15 20:44:20 +00:00
|
|
|
|
|
|
|
let span = call.head;
|
|
|
|
|
|
|
|
let path = call.req::<Spanned<String>>(engine_state, stack, 0)?;
|
|
|
|
let arg_span = path.span;
|
|
|
|
let path = Path::new(&path.item);
|
|
|
|
|
2022-12-01 12:26:17 +00:00
|
|
|
let path_exists = path.exists();
|
|
|
|
if path_exists && !force && !append {
|
|
|
|
return Err(ShellError::GenericError(
|
|
|
|
"Destination file already exists".into(),
|
|
|
|
format!(
|
|
|
|
"Destination file '{}' already exists",
|
|
|
|
path.to_string_lossy()
|
|
|
|
),
|
|
|
|
Some(arg_span),
|
|
|
|
Some("you can use -f, --force to force overwriting the destination".into()),
|
|
|
|
Vec::new(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let file = match (append, path_exists) {
|
2022-03-05 19:36:58 +00:00
|
|
|
(true, true) => std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.append(true)
|
|
|
|
.open(path),
|
|
|
|
_ => std::fs::File::create(path),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut file = match file {
|
2022-01-15 20:44:20 +00:00
|
|
|
Ok(file) => file,
|
|
|
|
Err(err) => {
|
2022-12-07 03:48:39 +00:00
|
|
|
return Err(ShellError::GenericError(
|
|
|
|
"Permission denied".into(),
|
|
|
|
err.to_string(),
|
|
|
|
Some(arg_span),
|
2022-01-15 20:44:20 +00:00
|
|
|
None,
|
2022-12-07 03:48:39 +00:00
|
|
|
Vec::new(),
|
2022-01-15 20:44:20 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
2022-10-20 12:56:44 +00:00
|
|
|
let stderr_path = call.get_flag::<Spanned<String>>(engine_state, stack, "stderr")?;
|
|
|
|
let stderr_file = match stderr_path {
|
|
|
|
None => None,
|
|
|
|
Some(stderr_path) => {
|
|
|
|
let stderr_span = stderr_path.span;
|
|
|
|
let stderr_path = Path::new(&stderr_path.item);
|
|
|
|
if stderr_path == path {
|
|
|
|
Some(file.try_clone()?)
|
|
|
|
} else {
|
|
|
|
match std::fs::File::create(stderr_path) {
|
|
|
|
Ok(file) => Some(file),
|
|
|
|
Err(err) => {
|
2022-12-07 03:48:39 +00:00
|
|
|
return Err(ShellError::GenericError(
|
|
|
|
"Permission denied".into(),
|
|
|
|
err.to_string(),
|
|
|
|
Some(stderr_span),
|
2022-10-20 12:56:44 +00:00
|
|
|
None,
|
2022-12-07 03:48:39 +00:00
|
|
|
Vec::new(),
|
2022-10-20 12:56:44 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-01-15 20:44:20 +00:00
|
|
|
|
|
|
|
let ext = if raw {
|
|
|
|
None
|
2022-11-21 01:32:15 +00:00
|
|
|
// if is extern stream , in other words , not value
|
|
|
|
} else if let PipelineData::ExternalStream { .. } = input {
|
|
|
|
None
|
|
|
|
} else if let PipelineData::Value(Value::String { .. }, ..) = input {
|
|
|
|
None
|
2022-01-15 20:44:20 +00:00
|
|
|
} else {
|
|
|
|
path.extension()
|
|
|
|
.map(|name| name.to_string_lossy().to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(ext) = ext {
|
2022-05-07 19:39:22 +00:00
|
|
|
let output = match engine_state.find_decl(format!("to {}", ext).as_bytes(), &[]) {
|
2022-01-15 20:44:20 +00:00
|
|
|
Some(converter_id) => {
|
|
|
|
let output = engine_state.get_decl(converter_id).run(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
2022-02-04 02:01:45 +00:00
|
|
|
&Call::new(span),
|
2022-01-15 20:44:20 +00:00
|
|
|
input,
|
|
|
|
)?;
|
|
|
|
|
2022-02-11 19:26:36 +00:00
|
|
|
output.into_value(span)
|
|
|
|
}
|
|
|
|
None => input.into_value(span),
|
|
|
|
};
|
2022-01-15 20:44:20 +00:00
|
|
|
|
2022-02-11 19:26:36 +00:00
|
|
|
match output {
|
|
|
|
Value::String { val, .. } => {
|
|
|
|
if let Err(err) = file.write_all(val.as_bytes()) {
|
|
|
|
return Err(ShellError::IOError(err.to_string()));
|
2022-05-17 18:28:18 +00:00
|
|
|
} else {
|
|
|
|
file.flush()?
|
2022-01-15 20:44:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 19:26:36 +00:00
|
|
|
Ok(PipelineData::new(span))
|
|
|
|
}
|
|
|
|
Value::Binary { val, .. } => {
|
|
|
|
if let Err(err) = file.write_all(&val) {
|
2022-01-15 20:44:20 +00:00
|
|
|
return Err(ShellError::IOError(err.to_string()));
|
2022-05-17 18:28:18 +00:00
|
|
|
} else {
|
|
|
|
file.flush()?
|
2022-01-15 20:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(PipelineData::new(span))
|
|
|
|
}
|
2022-03-05 21:56:04 +00:00
|
|
|
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()));
|
2022-05-17 18:28:18 +00:00
|
|
|
} else {
|
|
|
|
file.flush()?
|
2022-03-05 21:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(PipelineData::new(span))
|
|
|
|
}
|
2022-02-28 15:14:33 +00:00
|
|
|
v => Err(ShellError::UnsupportedInput(
|
|
|
|
format!("{:?} not supported", v.get_type()),
|
|
|
|
span,
|
|
|
|
)),
|
2022-01-15 20:44:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-27 02:39:27 +00:00
|
|
|
match input {
|
|
|
|
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::new(span)),
|
|
|
|
PipelineData::ExternalStream {
|
2022-10-20 12:56:44 +00:00
|
|
|
stdout: Some(stream),
|
|
|
|
stderr,
|
2022-03-27 02:39:27 +00:00
|
|
|
..
|
|
|
|
} => {
|
2022-10-20 12:56:44 +00:00
|
|
|
// delegate a thread to redirect stderr to result.
|
|
|
|
let handler = stderr.map(|stderr_stream| match stderr_file {
|
|
|
|
Some(stderr_file) => std::thread::spawn(move || {
|
|
|
|
stream_to_file(stderr_stream, stderr_file, span)
|
|
|
|
}),
|
|
|
|
None => std::thread::spawn(move || {
|
|
|
|
let _ = stderr_stream.into_bytes();
|
|
|
|
Ok(PipelineData::new(span))
|
|
|
|
}),
|
|
|
|
});
|
2022-03-27 02:39:27 +00:00
|
|
|
|
2022-10-20 12:56:44 +00:00
|
|
|
let res = stream_to_file(stream, file, span);
|
|
|
|
if let Some(h) = handler {
|
|
|
|
match h.join() {
|
|
|
|
Err(err) => {
|
|
|
|
return Err(ShellError::ExternalCommand(
|
|
|
|
"Fail to receive external commands stderr message".to_string(),
|
|
|
|
format!("{err:?}"),
|
|
|
|
span,
|
|
|
|
))
|
2022-03-27 02:39:27 +00:00
|
|
|
}
|
2022-10-20 12:56:44 +00:00
|
|
|
Ok(res) => res,
|
|
|
|
}?;
|
|
|
|
res
|
|
|
|
} else {
|
|
|
|
res
|
|
|
|
}
|
2022-02-11 19:26:36 +00:00
|
|
|
}
|
2022-03-27 02:39:27 +00:00
|
|
|
input => match input.into_value(span) {
|
|
|
|
Value::String { val, .. } => {
|
|
|
|
if let Err(err) = file.write_all(val.as_bytes()) {
|
|
|
|
return Err(ShellError::IOError(err.to_string()));
|
2022-05-17 18:28:18 +00:00
|
|
|
} else {
|
|
|
|
file.flush()?
|
2022-03-27 02:39:27 +00:00
|
|
|
}
|
2022-03-05 21:56:04 +00:00
|
|
|
|
2022-03-27 02:39:27 +00:00
|
|
|
Ok(PipelineData::new(span))
|
2022-03-05 21:56:04 +00:00
|
|
|
}
|
2022-03-27 02:39:27 +00:00
|
|
|
Value::Binary { val, .. } => {
|
|
|
|
if let Err(err) = file.write_all(&val) {
|
|
|
|
return Err(ShellError::IOError(err.to_string()));
|
2022-05-17 18:28:18 +00:00
|
|
|
} else {
|
|
|
|
file.flush()?
|
2022-03-27 02:39:27 +00:00
|
|
|
}
|
2022-03-05 21:56:04 +00:00
|
|
|
|
2022-03-27 02:39:27 +00:00
|
|
|
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()));
|
2022-05-17 18:28:18 +00:00
|
|
|
} else {
|
|
|
|
file.flush()?
|
2022-03-27 02:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(PipelineData::new(span))
|
|
|
|
}
|
|
|
|
v => Err(ShellError::UnsupportedInput(
|
|
|
|
format!("{:?} not supported", v.get_type()),
|
|
|
|
span,
|
|
|
|
)),
|
|
|
|
},
|
2022-02-11 19:26:36 +00:00
|
|
|
}
|
2022-01-15 20:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 12:25:41 +00:00
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
2022-06-03 09:35:31 +00:00
|
|
|
description: "Save a string to foo.txt in the current directory",
|
2022-10-26 16:36:42 +00:00
|
|
|
example: r#"'save me' | save foo.txt"#,
|
2022-02-21 12:25:41 +00:00
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
2022-06-03 09:35:31 +00:00
|
|
|
description: "Append a string to the end of foo.txt",
|
2022-10-26 16:36:42 +00:00
|
|
|
example: r#"'append me' | save --append foo.txt"#,
|
2022-06-03 09:35:31 +00:00
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Save a record to foo.json in the current directory",
|
2022-10-26 16:36:42 +00:00
|
|
|
example: r#"{ a: 1, b: 2 } | save foo.json"#,
|
2022-02-21 12:25:41 +00:00
|
|
|
result: None,
|
|
|
|
},
|
2022-10-20 12:56:44 +00:00
|
|
|
Example {
|
|
|
|
description: "Save a running program's stderr to foo.txt",
|
|
|
|
example: r#"do -i {} | save foo.txt --stderr foo.txt"#,
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Save a running program's stderr to separate file",
|
|
|
|
example: r#"do -i {} | save foo.txt --stderr bar.txt"#,
|
|
|
|
result: None,
|
|
|
|
},
|
2022-02-21 12:25:41 +00:00
|
|
|
]
|
|
|
|
}
|
2022-01-15 20:44:20 +00:00
|
|
|
}
|
2022-10-20 12:56:44 +00:00
|
|
|
|
|
|
|
fn stream_to_file(
|
|
|
|
mut stream: RawStream,
|
|
|
|
file: File,
|
|
|
|
span: Span,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let mut writer = BufWriter::new(file);
|
|
|
|
|
|
|
|
stream
|
|
|
|
.try_for_each(move |result| {
|
|
|
|
let buf = match result {
|
|
|
|
Ok(v) => match v {
|
|
|
|
Value::String { val, .. } => val.into_bytes(),
|
|
|
|
Value::Binary { val, .. } => val,
|
|
|
|
_ => {
|
|
|
|
return Err(ShellError::UnsupportedInput(
|
|
|
|
format!("{:?} not supported", v.get_type()),
|
|
|
|
v.span()?,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = writer.write(&buf) {
|
|
|
|
return Err(ShellError::IOError(err.to_string()));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.map(|_| PipelineData::new(span))
|
|
|
|
}
|