mirror of
https://github.com/nushell/nushell.git
synced 2024-11-23 17:03:45 +01:00
Allow save to use origin span in Tagged<Value>
This commit is contained in:
parent
37767b8882
commit
acf8a66a8f
@ -5,37 +5,69 @@ use crate::commands::to_toml::value_to_toml_value;
|
||||
use crate::commands::to_yaml::value_to_yaml_value;
|
||||
use crate::errors::ShellError;
|
||||
use crate::object::{Primitive, Value};
|
||||
use crate::Tagged;
|
||||
use crate::SpanSource;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn save(args: SinkCommandArgs) -> Result<(), ShellError> {
|
||||
let cwd = args.ctx.env.lock().unwrap().path().to_path_buf();
|
||||
let mut full_path = PathBuf::from(cwd);
|
||||
|
||||
let save_raw = if args.call_info.args.has("raw") {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if args.call_info.args.positional.is_none() {
|
||||
// If there is no filename, check the metadata for the origin filename
|
||||
if args.input.len() > 0 {
|
||||
let span = args.input[0].span();
|
||||
match span
|
||||
.source
|
||||
.map(|x| args.call_info.source_map.get(&x))
|
||||
.flatten()
|
||||
{
|
||||
Some(path) => match path {
|
||||
SpanSource::File(file) => {
|
||||
full_path.push(Path::new(file));
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::maybe_labeled_error(
|
||||
"Save requires a filepath",
|
||||
"needs path",
|
||||
args.call_info.name_span,
|
||||
));
|
||||
}
|
||||
|
||||
let positional = match args.call_info.args.positional {
|
||||
None => return Err(ShellError::string("save requires a filepath")),
|
||||
Some(p) => p,
|
||||
};
|
||||
|
||||
let cwd = args.ctx.env.lock().unwrap().path().to_path_buf();
|
||||
let mut full_path = PathBuf::from(cwd);
|
||||
match &(positional[0].item) {
|
||||
Value::Primitive(Primitive::String(s)) => full_path.push(Path::new(s)),
|
||||
_ => {}
|
||||
},
|
||||
None => {
|
||||
return Err(ShellError::maybe_labeled_error(
|
||||
"Save requires a filepath",
|
||||
"needs path",
|
||||
args.call_info.name_span,
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::maybe_labeled_error(
|
||||
"Save requires a filepath",
|
||||
"needs path",
|
||||
args.call_info.name_span,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
let arg = &args.call_info.args.positional.unwrap()[0];
|
||||
let arg_span = arg.span();
|
||||
match arg.item {
|
||||
Value::Primitive(Primitive::String(ref s)) => full_path.push(Path::new(s)),
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Save requires a string as a filepath",
|
||||
"needs path",
|
||||
arg_span.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let save_raw = match positional.get(1) {
|
||||
Some(Tagged {
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) if s == "--raw" => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
let contents = match full_path.extension() {
|
||||
Some(x) if x == "csv" && !save_raw => {
|
||||
|
Loading…
Reference in New Issue
Block a user