Merge pull request #95 from jonathandturner/fetch_url

Add opening urls
This commit is contained in:
Jonathan Turner 2019-06-09 06:09:58 +12:00 committed by GitHub
commit c65ca858cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 685 additions and 14 deletions

650
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -58,6 +58,7 @@ dirs = "2.0.1"
ctrlc = "3.1.3"
ptree = "0.2"
clipboard = "0.5"
reqwest = "0.9"
[dependencies.pancurses]
version = "0.16"

View File

@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
if args.positional.len() == 0 {
return Err(ShellError::string("open requires a filepath"));
return Err(ShellError::string("open requires a filepath or url"));
}
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
@ -14,15 +14,38 @@ pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
let contents = match &args.positional[0].item {
Value::Primitive(Primitive::String(s)) => {
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,
));
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,
));
}
}
}
}

View File

@ -4,7 +4,6 @@ use crate::prelude::*;
use derive_new::new;
use language_reporting::{Diagnostic, Label, Severity};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Serialize, Deserialize)]
pub enum ShellError {

View File

@ -12,7 +12,6 @@ use ordered_float::OrderedFloat;
use std::time::SystemTime;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, new)]
pub struct OF64 {

View File

@ -1,7 +1,6 @@
use crate::object::types::Type;
use derive_new::new;
use serde::{Serialize, Serializer};
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, Serializer};
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
pub enum DescriptorName {