mirror of
https://github.com/nushell/nushell.git
synced 2025-01-18 12:22:21 +01:00
Add opening urls
This commit is contained in:
parent
0e47e4c07d
commit
4e6c3d255c
650
Cargo.lock
generated
650
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -58,6 +58,7 @@ dirs = "2.0.1"
|
|||||||
ctrlc = "3.1.3"
|
ctrlc = "3.1.3"
|
||||||
ptree = "0.2"
|
ptree = "0.2"
|
||||||
clipboard = "0.5"
|
clipboard = "0.5"
|
||||||
|
reqwest = "0.9"
|
||||||
|
|
||||||
[dependencies.pancurses]
|
[dependencies.pancurses]
|
||||||
version = "0.16"
|
version = "0.16"
|
||||||
|
@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
|
|||||||
|
|
||||||
pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
pub fn open(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
if args.positional.len() == 0 {
|
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();
|
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 {
|
let contents = match &args.positional[0].item {
|
||||||
Value::Primitive(Primitive::String(s)) => {
|
Value::Primitive(Primitive::String(s)) => {
|
||||||
full_path.push(Path::new(&s));
|
if s.starts_with("http:") || s.starts_with("https:") {
|
||||||
match std::fs::read_to_string(&full_path) {
|
let response = reqwest::get(s);
|
||||||
Ok(s) => s,
|
match response {
|
||||||
Err(_) => {
|
Ok(mut r) => match r.text() {
|
||||||
return Err(ShellError::labeled_error(
|
Ok(s) => s,
|
||||||
"File cound not be opened",
|
Err(_) => {
|
||||||
"file not found",
|
return Err(ShellError::labeled_error(
|
||||||
args.positional[0].span,
|
"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,
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ use crate::prelude::*;
|
|||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
use language_reporting::{Diagnostic, Label, Severity};
|
use language_reporting::{Diagnostic, Label, Severity};
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
use serde_derive::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Serialize, Deserialize)]
|
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Serialize, Deserialize)]
|
||||||
pub enum ShellError {
|
pub enum ShellError {
|
||||||
|
@ -12,7 +12,6 @@ use ordered_float::OrderedFloat;
|
|||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
use serde_derive::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, new)]
|
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, new)]
|
||||||
pub struct OF64 {
|
pub struct OF64 {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use crate::object::types::Type;
|
use crate::object::types::Type;
|
||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
use serde_derive::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
|
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
|
||||||
pub enum DescriptorName {
|
pub enum DescriptorName {
|
||||||
|
Loading…
Reference in New Issue
Block a user