2019-05-28 04:00:00 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::{Primitive, Value};
|
2019-06-07 22:35:07 +00:00
|
|
|
use crate::parser::lexer::Spanned;
|
2019-05-28 04:00:00 +00:00
|
|
|
use crate::prelude::*;
|
2019-06-03 07:41:28 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-05-28 04:00:00 +00:00
|
|
|
|
|
|
|
pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-06-03 07:41:28 +00:00
|
|
|
if args.positional.len() == 0 {
|
2019-06-08 18:09:17 +00:00
|
|
|
return Err(ShellError::string("open requires a filepath or url"));
|
2019-06-03 07:41:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 04:00:00 +00:00
|
|
|
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
|
|
|
|
let mut full_path = PathBuf::from(cwd);
|
|
|
|
|
2019-06-07 22:35:07 +00:00
|
|
|
let contents = match &args.positional[0].item {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-06-08 18:09:17 +00:00
|
|
|
if s.starts_with("http:") || s.starts_with("https:") {
|
|
|
|
let response = reqwest::get(s);
|
|
|
|
match response {
|
|
|
|
Ok(mut r) => match r.text() {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Web page contents corrupt",
|
|
|
|
"received garbled data",
|
|
|
|
args.positional[0].span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"URL could not be opened",
|
|
|
|
"url not found",
|
|
|
|
args.positional[0].span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
full_path.push(Path::new(&s));
|
|
|
|
match std::fs::read_to_string(&full_path) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"File cound not be opened",
|
|
|
|
"file not found",
|
|
|
|
args.positional[0].span,
|
|
|
|
));
|
|
|
|
}
|
2019-06-07 22:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected string value for filename",
|
|
|
|
"expected filename",
|
|
|
|
args.positional[0].span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
2019-05-28 04:00:00 +00:00
|
|
|
|
|
|
|
let mut stream = VecDeque::new();
|
2019-06-01 19:20:48 +00:00
|
|
|
|
|
|
|
let open_raw = match args.positional.get(1) {
|
2019-06-07 22:35:07 +00:00
|
|
|
Some(Spanned {
|
|
|
|
item: Value::Primitive(Primitive::String(s)),
|
|
|
|
..
|
|
|
|
}) if s == "--raw" => true,
|
|
|
|
Some(v) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Unknown flag for open",
|
|
|
|
"unknown flag",
|
|
|
|
v.span,
|
|
|
|
))
|
|
|
|
}
|
2019-06-01 19:20:48 +00:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
match full_path.extension() {
|
|
|
|
Some(x) if x == "toml" && !open_raw => {
|
2019-06-03 07:41:28 +00:00
|
|
|
stream.push_back(ReturnValue::Value(
|
|
|
|
crate::commands::from_toml::from_toml_string_to_value(contents),
|
|
|
|
));
|
2019-06-01 19:20:48 +00:00
|
|
|
}
|
|
|
|
Some(x) if x == "json" && !open_raw => {
|
2019-06-03 07:41:28 +00:00
|
|
|
stream.push_back(ReturnValue::Value(
|
|
|
|
crate::commands::from_json::from_json_string_to_value(contents),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Some(x) if x == "yml" && !open_raw => {
|
|
|
|
stream.push_back(ReturnValue::Value(
|
|
|
|
crate::commands::from_yaml::from_yaml_string_to_value(contents),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Some(x) if x == "yaml" && !open_raw => {
|
|
|
|
stream.push_back(ReturnValue::Value(
|
|
|
|
crate::commands::from_yaml::from_yaml_string_to_value(contents),
|
|
|
|
));
|
2019-06-01 19:20:48 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
stream.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
|
|
|
|
contents,
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 04:00:00 +00:00
|
|
|
|
|
|
|
Ok(stream.boxed())
|
|
|
|
}
|