nushell/src/commands/save.rs

101 lines
3.5 KiB
Rust
Raw Normal View History

2019-06-07 19:13:38 +02:00
use crate::commands::command::SinkCommandArgs;
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-06-07 19:13:38 +02:00
use crate::errors::ShellError;
use crate::object::{Primitive, Value};
2019-06-22 05:43:37 +02:00
use crate::parser::Spanned;
2019-06-07 19:13:38 +02:00
use std::path::{Path, PathBuf};
pub fn save(args: SinkCommandArgs) -> Result<(), ShellError> {
if args.call_info.args.positional.is_none() {
2019-06-15 20:36:17 +02:00
return Err(ShellError::maybe_labeled_error(
"Save requires a filepath",
"needs path",
args.call_info.name_span,
2019-06-15 20:36:17 +02:00
));
2019-06-07 19:13:38 +02:00
}
let positional = match args.call_info.args.positional {
2019-06-22 05:43:37 +02:00
None => return Err(ShellError::string("save requires a filepath")),
Some(p) => p,
};
2019-07-16 21:10:25 +02:00
let cwd = args.ctx.env.lock().unwrap().path().to_path_buf();
2019-06-07 19:13:38 +02:00
let mut full_path = PathBuf::from(cwd);
2019-06-22 05:43:37 +02:00
match &(positional[0].item) {
2019-06-07 19:13:38 +02:00
Value::Primitive(Primitive::String(s)) => full_path.push(Path::new(s)),
_ => {}
}
2019-06-22 05:43:37 +02:00
let save_raw = match positional.get(1) {
2019-06-08 00:35:07 +02:00
Some(Spanned {
item: Value::Primitive(Primitive::String(s)),
..
}) if s == "--raw" => true,
2019-06-07 19:13:38 +02:00
_ => false,
};
let contents = match full_path.extension() {
2019-07-21 09:08:05 +02:00
Some(x) if x == "csv" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to csv requires a single object (or use --raw)",
));
}
to_csv_to_string(&value_to_csv_value(&args.input[0])).unwrap()
}
2019-06-07 19:13:38 +02:00
Some(x) if x == "toml" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to toml requires a single object (or use --raw)",
));
}
2019-07-16 06:03:28 +02:00
toml::to_string(&value_to_toml_value(&args.input[0])).unwrap()
2019-06-16 08:43:40 +02:00
}
2019-06-07 19:13:38 +02:00
Some(x) if x == "json" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to json requires a single object (or use --raw)",
));
}
2019-07-16 06:03:28 +02:00
serde_json::to_string(&value_to_json_value(&args.input[0])).unwrap()
2019-06-07 19:13:38 +02:00
}
Some(x) if x == "yml" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to yml requires a single object (or use --raw)",
));
}
2019-07-16 06:03:28 +02:00
serde_yaml::to_string(&value_to_yaml_value(&args.input[0])).unwrap()
2019-06-07 19:13:38 +02:00
}
Some(x) if x == "yaml" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to yaml requires a single object (or use --raw)",
));
}
2019-07-16 06:03:28 +02:00
serde_yaml::to_string(&value_to_yaml_value(&args.input[0])).unwrap()
2019-06-07 19:13:38 +02:00
}
_ => {
let mut save_data = String::new();
if args.input.len() > 0 {
let mut first = true;
for i in args.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(())
}