Slightly improve new which command (#1145)

This commit is contained in:
Jonathan Turner 2020-01-01 20:47:25 +13:00 committed by GitHub
parent b304de8199
commit 78016446dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 20 deletions

View file

@ -3,11 +3,14 @@
Finds a program file. Finds a program file.
Usage: Usage:
> which ...args{flags} > which <application> {flags}
## Parameters ## Parameters
- ...args: the names of the commands to find the path to - application: the name of the command to find the path to
## Flags
- --all: list all executables - --all: list all executables
## Examples ## Examples

View file

@ -14,11 +14,8 @@ impl WholeStreamCommand for Which {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("which") Signature::build("which")
.required("application", SyntaxShape::String, "application")
.switch("all", "list all executables") .switch("all", "list all executables")
.rest(
SyntaxShape::String,
"the names of the commands to find the path to",
)
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -75,32 +72,32 @@ macro_rules! entry_path {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct WhichArgs { struct WhichArgs {
bin: Tagged<String>, application: Tagged<String>,
all: bool, all: bool,
} }
fn which( fn which(
WhichArgs { bin, all }: WhichArgs, WhichArgs { application, all }: WhichArgs,
RunnableContext { commands, .. }: RunnableContext, RunnableContext { commands, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let external = bin.starts_with('^'); let external = application.starts_with('^');
let item = if external { let item = if external {
bin.item[1..].to_string() application.item[1..].to_string()
} else { } else {
bin.item.clone() application.item.clone()
}; };
if all { if all {
let stream = async_stream! { let stream = async_stream! {
if external { if external {
if let Ok(path) = ichwh::which(&item).await { if let Ok(path) = ichwh::which(&item).await {
yield ReturnSuccess::value(entry_path!(item, path.into(), bin.tag.clone())); yield ReturnSuccess::value(entry_path!(item, path.into(), application.tag.clone()));
} }
} }
let builtin = commands.has(&item); let builtin = commands.has(&item);
if builtin { if builtin {
yield ReturnSuccess::value(entry_builtin!(item, bin.tag.clone())); yield ReturnSuccess::value(entry_builtin!(item, application.tag.clone()));
} }
if let Ok(paths) = ichwh::which_all(&item).await { if let Ok(paths) = ichwh::which_all(&item).await {
@ -108,18 +105,18 @@ fn which(
yield Err(ShellError::labeled_error( yield Err(ShellError::labeled_error(
"Binary not found for argument, and argument is not a builtin", "Binary not found for argument, and argument is not a builtin",
"not found", "not found",
&bin.tag, &application.tag,
)); ));
} else { } else {
for path in paths { for path in paths {
yield ReturnSuccess::value(entry_path!(item, path.into(), bin.tag.clone())); yield ReturnSuccess::value(entry_path!(item, path.into(), application.tag.clone()));
} }
} }
} else { } else {
yield Err(ShellError::labeled_error( yield Err(ShellError::labeled_error(
"Error trying to find binary for argument", "Error trying to find binary for argument",
"error", "error",
&bin.tag, &application.tag,
)); ));
} }
}; };
@ -129,17 +126,17 @@ fn which(
let stream = async_stream! { let stream = async_stream! {
if external { if external {
if let Ok(path) = ichwh::which(&item).await { if let Ok(path) = ichwh::which(&item).await {
yield ReturnSuccess::value(entry_path!(item, path.into(), bin.tag.clone())); yield ReturnSuccess::value(entry_path!(item, path.into(), application.tag.clone()));
} }
} else if commands.has(&item) { } else if commands.has(&item) {
yield ReturnSuccess::value(entry_builtin!(item, bin.tag.clone())); yield ReturnSuccess::value(entry_builtin!(item, application.tag.clone()));
} else if let Ok(path) = ichwh::which(&item).await { } else if let Ok(path) = ichwh::which(&item).await {
yield ReturnSuccess::value(entry_path!(item, path.into(), bin.tag.clone())); yield ReturnSuccess::value(entry_path!(item, path.into(), application.tag.clone()));
} else { } else {
yield Err(ShellError::labeled_error( yield Err(ShellError::labeled_error(
"Binary not found for argument, and argument is not a builtin", "Binary not found for argument, and argument is not a builtin",
"not found", "not found",
&bin.tag, &application.tag,
)); ));
} }
}; };