ISSUE-1744-Glob support for start command (#1912)

* Possible implementation of globbing for start command

* Whoops forgot to remove Error used for debugging

* Use string lossy

* Run clippy

* Pin glob

* Better error messages

* Remove unneeded comment
This commit is contained in:
Arash Outadi 2020-05-30 10:41:25 -07:00 committed by GitHub
parent ad43ef08e5
commit ecb67fee40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 2 deletions

1
Cargo.lock generated
View file

@ -2519,6 +2519,7 @@ dependencies = [
name = "nu_plugin_start"
version = "0.1.0"
dependencies = [
"glob",
"nu-build",
"nu-errors",
"nu-plugin",

View file

@ -16,6 +16,7 @@ nu-source = { path = "../nu-source", version = "0.14.1" }
nu-errors = { path = "../nu-errors", version = "0.14.1" }
url = "2.1.1"
open = "1.4.0"
glob = "0.3.0"
[build-dependencies]
nu-build = { version = "0.14.1", path = "../nu-build" }

View file

@ -1,5 +1,5 @@
use nu_errors::ShellError;
use nu_protocol::CallInfo;
use nu_protocol::{CallInfo, Value};
use nu_source::{Tag, Tagged, TaggedItem};
use std::path::Path;
@ -42,13 +42,44 @@ impl Start {
}
}
fn glob_to_values(&self, value: &Value) -> Result<Vec<Tagged<String>>, ShellError> {
let mut result = vec![];
match glob::glob(&value.as_string()?) {
Ok(paths) => {
for path_result in paths {
match path_result {
Ok(path) => result
.push(path.to_string_lossy().to_string().tagged(value.tag.clone())),
Err(glob_error) => {
return Err(ShellError::labeled_error(
format!("{}", glob_error),
"glob error",
value.tag.clone(),
));
}
}
}
}
Err(pattern_error) => {
return Err(ShellError::labeled_error(
format!("{}", pattern_error),
"invalid pattern",
value.tag.clone(),
))
}
}
Ok(result)
}
fn parse_filenames(&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() {
result.push(value.as_string()?.tagged(value.tag.clone()));
let res = self.glob_to_values(value)?;
result.extend(res);
}
if result.is_empty() {