2019-06-13 23:47:25 +02:00
|
|
|
use crate::commands::command::CommandAction;
|
2019-06-26 09:40:43 +02:00
|
|
|
use crate::commands::open::{fetch, parse_as_value};
|
2019-06-13 23:47:25 +02:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::{Primitive, Value};
|
|
|
|
use crate::prelude::*;
|
2019-06-26 09:40:43 +02:00
|
|
|
use std::path::PathBuf;
|
2019-06-13 23:47:25 +02:00
|
|
|
|
|
|
|
pub fn enter(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-06-26 09:40:43 +02:00
|
|
|
if args.len() == 0 {
|
|
|
|
return Err(ShellError::maybe_labeled_error(
|
|
|
|
"open requires a path or url",
|
|
|
|
"missing path",
|
|
|
|
args.name_span,
|
|
|
|
));
|
|
|
|
}
|
2019-06-13 23:47:25 +02:00
|
|
|
|
2019-06-16 01:03:49 +02:00
|
|
|
let span = args.name_span;
|
|
|
|
|
2019-06-13 23:47:25 +02:00
|
|
|
let cwd = args
|
2019-06-22 05:43:37 +02:00
|
|
|
.env()
|
2019-06-13 23:47:25 +02:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
2019-06-15 19:52:55 +02:00
|
|
|
.front()
|
2019-06-13 23:47:25 +02:00
|
|
|
.unwrap()
|
|
|
|
.path()
|
|
|
|
.to_path_buf();
|
2019-06-22 05:43:37 +02:00
|
|
|
|
2019-06-26 09:40:43 +02:00
|
|
|
let full_path = PathBuf::from(cwd);
|
2019-06-13 23:47:25 +02:00
|
|
|
|
2019-06-26 09:40:43 +02:00
|
|
|
let (file_extension, contents) = match &args.expect_nth(0)?.item {
|
|
|
|
Value::Primitive(Primitive::String(s)) => fetch(&full_path, s, args.expect_nth(0)?.span)?,
|
2019-06-13 23:47:25 +02:00
|
|
|
_ => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected string value for filename",
|
|
|
|
"expected filename",
|
2019-06-22 05:43:37 +02:00
|
|
|
args.expect_nth(0)?.span,
|
2019-06-13 23:47:25 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut stream = VecDeque::new();
|
|
|
|
|
2019-06-22 20:02:23 +02:00
|
|
|
let file_extension = if args.has("raw") {
|
|
|
|
None
|
|
|
|
} else if args.has("json") {
|
|
|
|
Some("json".to_string())
|
|
|
|
} else if args.has("xml") {
|
|
|
|
Some("xml".to_string())
|
|
|
|
} else if args.has("ini") {
|
|
|
|
Some("ini".to_string())
|
|
|
|
} else if args.has("yaml") {
|
|
|
|
Some("yaml".to_string())
|
|
|
|
} else if args.has("toml") {
|
|
|
|
Some("toml".to_string())
|
|
|
|
} else {
|
|
|
|
if let Some(ref named_args) = args.args.named {
|
|
|
|
for named in named_args.iter() {
|
2019-06-15 04:24:13 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
2019-06-22 20:02:23 +02:00
|
|
|
"Unknown flag for enter",
|
2019-06-15 04:24:13 +02:00
|
|
|
"unknown flag",
|
2019-06-22 20:02:23 +02:00
|
|
|
named.1.span.clone(),
|
2019-06-15 04:24:13 +02:00
|
|
|
));
|
|
|
|
}
|
2019-06-22 20:02:23 +02:00
|
|
|
file_extension
|
|
|
|
} else {
|
|
|
|
file_extension
|
2019-06-13 23:47:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-26 09:40:43 +02:00
|
|
|
stream.push_back(ReturnValue::Action(CommandAction::Enter(parse_as_value(
|
|
|
|
file_extension,
|
|
|
|
contents,
|
|
|
|
span,
|
|
|
|
)?)));
|
2019-06-13 23:47:25 +02:00
|
|
|
|
|
|
|
Ok(stream.boxed())
|
|
|
|
}
|