2019-08-30 19:29:04 +02:00
|
|
|
use crate::commands::{UnevaluatedCallInfo, WholeStreamCommand};
|
2019-06-07 19:13:38 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-08-06 18:26:33 +02:00
|
|
|
use crate::object::Value;
|
2019-08-02 21:15:07 +02:00
|
|
|
use crate::prelude::*;
|
2019-08-09 07:36:43 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-06-07 19:13:38 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
pub struct Save;
|
|
|
|
|
2019-09-04 04:21:37 +02:00
|
|
|
macro_rules! process_string {
|
|
|
|
($input:ident, $name_span:ident) => {{
|
|
|
|
let mut result_string = String::new();
|
|
|
|
for res in $input {
|
|
|
|
match res {
|
|
|
|
Tagged {
|
|
|
|
item: Value::Primitive(Primitive::String(s)),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
result_string.push_str(&s);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
yield core::task::Poll::Ready(Err(ShellError::labeled_error(
|
|
|
|
"Save could not successfully save",
|
|
|
|
"unexpected data during save",
|
|
|
|
$name_span,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(result_string.into_bytes())
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! process_string_return_success {
|
|
|
|
($result_vec:ident, $name_span:ident) => {{
|
|
|
|
let mut result_string = String::new();
|
|
|
|
for res in $result_vec {
|
|
|
|
match res {
|
|
|
|
Ok(ReturnSuccess::Value(Tagged {
|
|
|
|
item: Value::Primitive(Primitive::String(s)),
|
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
result_string.push_str(&s);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
yield core::task::Poll::Ready(Err(ShellError::labeled_error(
|
|
|
|
"Save could not successfully save",
|
|
|
|
"unexpected data during text save",
|
|
|
|
$name_span,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(result_string.into_bytes())
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! process_binary_return_success {
|
|
|
|
($result_vec:ident, $name_span:ident) => {{
|
|
|
|
let mut result_binary: Vec<u8> = Vec::new();
|
|
|
|
for res in $result_vec {
|
|
|
|
match res {
|
|
|
|
Ok(ReturnSuccess::Value(Tagged {
|
|
|
|
item: Value::Binary(b),
|
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
for u in b.into_iter() {
|
|
|
|
result_binary.push(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
yield core::task::Poll::Ready(Err(ShellError::labeled_error(
|
|
|
|
"Save could not successfully save",
|
|
|
|
"unexpected data during binary save",
|
|
|
|
$name_span,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(result_binary)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
#[derive(Deserialize)]
|
2019-08-03 04:17:28 +02:00
|
|
|
pub struct SaveArgs {
|
2019-08-09 07:36:43 +02:00
|
|
|
path: Option<Tagged<PathBuf>>,
|
2019-08-02 21:15:07 +02:00
|
|
|
raw: bool,
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
impl WholeStreamCommand for Save {
|
2019-08-02 21:15:07 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"save"
|
2019-06-07 19:13:38 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("save")
|
2019-08-09 07:36:43 +02:00
|
|
|
.optional("path", SyntaxType::Path)
|
2019-08-02 21:15:07 +02:00
|
|
|
.switch("raw")
|
|
|
|
}
|
2019-06-22 05:43:37 +02:00
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Save the contents of the pipeline to a file."
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-31 02:59:21 +02:00
|
|
|
Ok(args.process_raw(registry, save)?.run())
|
2019-06-07 19:13:38 +02:00
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-06-07 19:13:38 +02:00
|
|
|
|
2019-08-21 14:08:23 +02:00
|
|
|
fn save(
|
2019-08-02 21:15:07 +02:00
|
|
|
SaveArgs {
|
|
|
|
path,
|
|
|
|
raw: save_raw,
|
|
|
|
}: SaveArgs,
|
2019-08-21 14:08:23 +02:00
|
|
|
RunnableContext {
|
|
|
|
input,
|
|
|
|
name,
|
|
|
|
shell_manager,
|
|
|
|
source_map,
|
2019-08-31 02:59:21 +02:00
|
|
|
host,
|
|
|
|
commands: registry,
|
2019-08-21 14:08:23 +02:00
|
|
|
..
|
|
|
|
}: RunnableContext,
|
2019-08-31 02:59:21 +02:00
|
|
|
raw_args: RawCommandArgs,
|
2019-08-02 21:15:07 +02:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-21 14:08:23 +02:00
|
|
|
let mut full_path = PathBuf::from(shell_manager.path());
|
|
|
|
let name_span = name;
|
2019-08-02 21:15:07 +02:00
|
|
|
|
2019-08-31 02:59:21 +02:00
|
|
|
let source_map = source_map.clone();
|
|
|
|
let stream = async_stream_block! {
|
|
|
|
let input: Vec<Tagged<Value>> = input.values.collect().await;
|
|
|
|
if path.is_none() {
|
2019-08-09 07:36:43 +02:00
|
|
|
// If there is no filename, check the metadata for the origin filename
|
|
|
|
if input.len() > 0 {
|
|
|
|
let origin = input[0].origin();
|
2019-08-26 21:26:10 +02:00
|
|
|
match origin.and_then(|x| source_map.get(&x)) {
|
2019-08-09 07:36:43 +02:00
|
|
|
Some(path) => match path {
|
|
|
|
SpanSource::File(file) => {
|
|
|
|
full_path.push(Path::new(file));
|
|
|
|
}
|
|
|
|
_ => {
|
2019-08-09 22:49:43 +02:00
|
|
|
yield Err(ShellError::labeled_error(
|
|
|
|
"Save requires a filepath",
|
|
|
|
"needs path",
|
2019-08-21 14:08:23 +02:00
|
|
|
name_span,
|
2019-08-09 22:49:43 +02:00
|
|
|
));
|
2019-08-09 07:36:43 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
2019-08-09 22:49:43 +02:00
|
|
|
yield Err(ShellError::labeled_error(
|
|
|
|
"Save requires a filepath",
|
|
|
|
"needs path",
|
2019-08-21 14:08:23 +02:00
|
|
|
name_span,
|
2019-08-09 22:49:43 +02:00
|
|
|
));
|
2019-08-09 07:36:43 +02:00
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
} else {
|
2019-08-09 22:49:43 +02:00
|
|
|
yield Err(ShellError::labeled_error(
|
|
|
|
"Save requires a filepath",
|
|
|
|
"needs path",
|
2019-08-21 14:08:23 +02:00
|
|
|
name_span,
|
2019-08-09 22:49:43 +02:00
|
|
|
));
|
2019-07-21 09:08:05 +02:00
|
|
|
}
|
2019-08-31 02:59:21 +02:00
|
|
|
} else {
|
|
|
|
if let Some(file) = path {
|
|
|
|
full_path.push(file.item());
|
2019-08-21 14:08:23 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
|
2019-09-04 03:50:23 +02:00
|
|
|
let content : Result<Vec<u8>, ShellError> = if !save_raw {
|
2019-08-31 02:59:21 +02:00
|
|
|
if let Some(extension) = full_path.extension() {
|
|
|
|
let command_name = format!("to-{}", extension.to_str().unwrap());
|
|
|
|
if let Some(converter) = registry.get_command(&command_name) {
|
|
|
|
let new_args = RawCommandArgs {
|
2019-09-01 23:39:59 +02:00
|
|
|
host,
|
|
|
|
shell_manager,
|
2019-08-31 02:59:21 +02:00
|
|
|
call_info: UnevaluatedCallInfo {
|
|
|
|
args: crate::parser::hir::Call {
|
|
|
|
head: raw_args.call_info.args.head,
|
|
|
|
positional: None,
|
|
|
|
named: None
|
|
|
|
},
|
|
|
|
source: raw_args.call_info.source,
|
|
|
|
source_map: raw_args.call_info.source_map,
|
|
|
|
name_span: raw_args.call_info.name_span,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mut result = converter.run(new_args.with_input(input), ®istry);
|
|
|
|
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
|
2019-09-04 03:50:23 +02:00
|
|
|
if converter.is_binary() {
|
2019-09-04 04:21:37 +02:00
|
|
|
process_binary_return_success!(result_vec, name_span)
|
2019-09-04 03:50:23 +02:00
|
|
|
} else {
|
2019-09-04 04:21:37 +02:00
|
|
|
process_string_return_success!(result_vec, name_span)
|
2019-08-31 02:59:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-04 04:21:37 +02:00
|
|
|
process_string!(input, name_span)
|
2019-08-31 02:59:21 +02:00
|
|
|
}
|
2019-08-21 14:08:23 +02:00
|
|
|
} else {
|
2019-09-04 04:21:37 +02:00
|
|
|
process_string!(input, name_span)
|
2019-08-31 02:59:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-04 03:50:23 +02:00
|
|
|
Ok(string_from(&input).into_bytes())
|
2019-08-31 02:59:21 +02:00
|
|
|
};
|
2019-08-09 07:36:43 +02:00
|
|
|
|
2019-08-31 02:59:21 +02:00
|
|
|
match content {
|
|
|
|
Ok(save_data) => match std::fs::write(full_path, save_data) {
|
|
|
|
Ok(o) => o,
|
2019-08-21 14:08:23 +02:00
|
|
|
Err(e) => yield Err(ShellError::string(e.to_string())),
|
2019-08-31 02:59:21 +02:00
|
|
|
},
|
|
|
|
Err(e) => yield Err(ShellError::string(e.to_string())),
|
|
|
|
}
|
2019-08-21 14:08:23 +02:00
|
|
|
|
2019-08-31 02:59:21 +02:00
|
|
|
};
|
2019-08-09 07:36:43 +02:00
|
|
|
|
2019-08-31 02:59:21 +02:00
|
|
|
Ok(OutputStream::new(stream))
|
2019-06-07 19:13:38 +02:00
|
|
|
}
|
2019-08-21 14:08:23 +02:00
|
|
|
|
2019-09-04 03:50:23 +02:00
|
|
|
fn string_from(input: &Vec<Tagged<Value>>) -> String {
|
2019-08-21 14:08:23 +02:00
|
|
|
let mut save_data = String::new();
|
|
|
|
|
|
|
|
if input.len() > 0 {
|
|
|
|
let mut first = true;
|
|
|
|
for i in input.iter() {
|
|
|
|
if !first {
|
|
|
|
save_data.push_str("\n");
|
|
|
|
} else {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if let Ok(data) = &i.as_string() {
|
|
|
|
save_data.push_str(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 03:50:23 +02:00
|
|
|
save_data
|
2019-08-21 14:08:23 +02:00
|
|
|
}
|