2019-07-21 09:08:05 +02:00
|
|
|
use crate::commands::to_csv::{to_string as to_csv_to_string, value_to_csv_value};
|
2019-07-16 06:03:28 +02:00
|
|
|
use crate::commands::to_json::value_to_json_value;
|
|
|
|
use crate::commands::to_toml::value_to_toml_value;
|
|
|
|
use crate::commands::to_yaml::value_to_yaml_value;
|
2019-08-15 07:02:02 +02:00
|
|
|
use crate::commands::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;
|
|
|
|
|
|
|
|
#[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-02 21:15:07 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(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-02 21:15:07 +02:00
|
|
|
pub fn save(
|
|
|
|
SaveArgs {
|
|
|
|
path,
|
|
|
|
raw: save_raw,
|
|
|
|
}: SaveArgs,
|
|
|
|
context: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let mut full_path = context.cwd();
|
|
|
|
|
2019-08-09 07:36:43 +02:00
|
|
|
if path.is_none() {
|
|
|
|
let source_map = context.source_map.clone();
|
|
|
|
let stream = async_stream_block! {
|
|
|
|
let input: Vec<Tagged<Value>> = context.input.values.collect().await;
|
|
|
|
// If there is no filename, check the metadata for the origin filename
|
|
|
|
if input.len() > 0 {
|
|
|
|
let origin = input[0].origin();
|
|
|
|
match origin.map(|x| source_map.get(&x)).flatten() {
|
|
|
|
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",
|
|
|
|
context.name,
|
|
|
|
));
|
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",
|
|
|
|
context.name,
|
|
|
|
));
|
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",
|
|
|
|
context.name,
|
|
|
|
));
|
2019-07-21 09:08:05 +02:00
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
|
|
|
|
let contents = match full_path.extension() {
|
|
|
|
Some(x) if x == "csv" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to csv requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
to_csv_to_string(&value_to_csv_value(&input[0])).unwrap()
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
Some(x) if x == "toml" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to toml requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
toml::to_string(&value_to_toml_value(&input[0])).unwrap()
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
Some(x) if x == "json" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to json requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
serde_json::to_string(&value_to_json_value(&input[0])).unwrap()
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
Some(x) if x == "yml" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to yml requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
Some(x) if x == "yaml" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to yaml requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
save_data.push_str(&i.as_string().unwrap());
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-06-07 19:13:38 +02:00
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
save_data
|
2019-06-07 19:13:38 +02:00
|
|
|
}
|
2019-08-09 07:36:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let _ = std::fs::write(full_path, contents);
|
2019-08-02 21:15:07 +02:00
|
|
|
};
|
|
|
|
|
2019-08-09 07:36:43 +02:00
|
|
|
Ok(OutputStream::new(stream))
|
|
|
|
} else {
|
|
|
|
full_path.push(path.unwrap().item());
|
|
|
|
|
|
|
|
let stream = async_stream_block! {
|
|
|
|
let input: Vec<Tagged<Value>> = context.input.values.collect().await;
|
2019-06-07 19:13:38 +02:00
|
|
|
|
2019-08-09 07:36:43 +02:00
|
|
|
let contents = match full_path.extension() {
|
|
|
|
Some(x) if x == "csv" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to csv requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
to_csv_to_string(&value_to_csv_value(&input[0])).unwrap()
|
|
|
|
}
|
|
|
|
Some(x) if x == "toml" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to toml requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
toml::to_string(&value_to_toml_value(&input[0])).unwrap()
|
|
|
|
}
|
|
|
|
Some(x) if x == "json" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to json requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
serde_json::to_string(&value_to_json_value(&input[0])).unwrap()
|
|
|
|
}
|
|
|
|
Some(x) if x == "yml" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to yml requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
|
|
|
|
}
|
|
|
|
Some(x) if x == "yaml" && !save_raw => {
|
|
|
|
if input.len() != 1 {
|
|
|
|
yield Err(ShellError::string(
|
|
|
|
"saving to yaml requires a single object (or use --raw)",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
save_data.push_str(&i.as_string().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
save_data
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = std::fs::write(full_path, contents);
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(OutputStream::new(stream))
|
|
|
|
}
|
2019-06-07 19:13:38 +02:00
|
|
|
}
|