allow start to handle urls (#3351)

This commit is contained in:
Darren Schroeder 2021-04-24 09:33:17 -05:00 committed by GitHub
parent df2f3d25b0
commit 73f62266c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 38 deletions

49
Cargo.lock generated
View File

@ -183,16 +183,6 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "async-attributes"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5"
dependencies = [
"quote 1.0.9",
"syn 1.0.63",
]
[[package]]
name = "async-channel"
version = "1.6.1"
@ -290,7 +280,6 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9f06685bad74e0570f5213741bea82158279a4103d988e57bfada11ad230341"
dependencies = [
"async-attributes",
"async-channel",
"async-global-executor",
"async-io",
@ -2394,18 +2383,6 @@ dependencies = [
"thiserror",
]
[[package]]
name = "ichwh"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea685d38f1becb4f0a04e6cbff9256c6c2cd5e5905563b251401d1c13d12c654"
dependencies = [
"async-std",
"cfg-if 0.1.10",
"futures 0.3.13",
"thiserror",
]
[[package]]
name = "idna"
version = "0.2.2"
@ -3176,7 +3153,6 @@ dependencies = [
"glob",
"htmlescape",
"ical",
"ichwh",
"indexmap",
"itertools",
"lazy_static 1.4.0",
@ -3275,7 +3251,6 @@ dependencies = [
"hamcrest2",
"htmlescape",
"ical",
"ichwh",
"indexmap",
"itertools",
"lazy_static 1.4.0",
@ -3758,6 +3733,7 @@ dependencies = [
"nu-source",
"open",
"url",
"webbrowser",
]
[[package]]
@ -6787,6 +6763,17 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "webbrowser"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecad156490d6b620308ed411cfee90d280b3cbd13e189ea0d3fada8acc89158a"
dependencies = [
"web-sys",
"widestring",
"winapi 0.3.9",
]
[[package]]
name = "weezl"
version = "0.1.4"
@ -6804,14 +6791,20 @@ dependencies = [
[[package]]
name = "which"
version = "4.0.2"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87c14ef7e1b8b8ecfc75d5eca37949410046e66f15d185c01d70824f1f8111ef"
checksum = "b55551e42cbdf2ce2bedd2203d0cc08dba002c27510f86dab6d0ce304cba3dfe"
dependencies = [
"either",
"libc",
"thiserror",
]
[[package]]
name = "widestring"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c168940144dd21fd8046987c16a46a33d5fc84eec29ef9dcddc2ac9e31526b7c"
[[package]]
name = "wild"
version = "2.0.4"

View File

@ -17,6 +17,7 @@ nu-protocol = { path = "../nu-protocol", version = "0.30.1" }
nu-source = { path = "../nu-source", version = "0.30.1" }
open = "1.4.0"
url = "2.2.0"
webbrowser = "0.5.5"
[build-dependencies]
nu-errors = { version = "0.30.1", path = "../nu-errors" }

View File

@ -2,7 +2,6 @@ use nu_errors::ShellError;
use nu_protocol::{CallInfo, Value};
use nu_source::{Tag, Tagged, TaggedItem};
use std::path::Path;
#[cfg(not(target_os = "windows"))]
use std::process::{Command, Stdio};
@ -24,7 +23,7 @@ impl Start {
pub fn parse(&mut self, call_info: CallInfo) -> Result<(), ShellError> {
self.tag = call_info.name_tag.clone();
self.parse_filenames(&call_info)?;
self.parse_input_parameters(&call_info)?;
self.parse_application(&call_info);
Ok(())
}
@ -72,20 +71,46 @@ impl Start {
Ok(result)
}
fn parse_filenames(&mut self, call_info: &CallInfo) -> Result<(), ShellError> {
fn parse_input_parameters(&mut self, call_info: &CallInfo) -> Result<(), ShellError> {
let candidates = match &call_info.args.positional {
Some(values) => {
let mut result = vec![];
for value in values.iter() {
let val_str = value.as_string();
match val_str {
Ok(s) => {
if s.to_ascii_lowercase().starts_with("http")
|| s.to_ascii_lowercase().starts_with("https")
{
if webbrowser::open(&s).is_ok() {
result.push("http/web".to_string().tagged_unknown())
} else {
return Err(ShellError::labeled_error(
&format!("error opening {}", &s),
"error opening url",
self.tag.span,
));
}
} else {
let res = self.glob_to_values(value)?;
result.extend(res);
}
}
Err(e) => {
return Err(ShellError::labeled_error(
e.to_string(),
"no input given",
self.tag.span,
));
}
}
}
if result.is_empty() {
return Err(ShellError::labeled_error(
"No filename(s) given",
"no filename(s) given",
"No input given",
"no input given",
self.tag.span,
));
}
@ -93,16 +118,18 @@ impl Start {
}
None => {
return Err(ShellError::labeled_error(
"No filename(s) given",
"no filename(s) given",
"No input given",
"no input given",
self.tag.span,
))
}
};
for candidate in candidates {
if !candidate.contains("http/web") {
self.add_filename(candidate)?;
}
}
Ok(())
}