nushell/src/commands/which_.rs

150 lines
4.4 KiB
Rust
Raw Normal View History

2019-08-15 07:02:02 +02:00
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use indexmap::map::IndexMap;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
2019-08-10 09:06:08 +02:00
pub struct Which;
2019-08-15 07:02:02 +02:00
impl WholeStreamCommand for Which {
fn name(&self) -> &str {
"which"
}
fn signature(&self) -> Signature {
Signature::build("which")
.switch("all", "list all executables")
.rest(
SyntaxShape::String,
"the names of the commands to find the path to",
)
}
fn usage(&self) -> &str {
"Finds a program file."
}
2019-08-10 09:06:08 +02:00
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, which)?.run()
2019-08-10 09:06:08 +02:00
}
}
/// Shortcuts for creating an entry to the output table
fn entry(arg: impl Into<String>, path: Value, builtin: bool, tag: Tag) -> Value {
let mut map = IndexMap::new();
map.insert(
"arg".to_string(),
UntaggedValue::Primitive(Primitive::String(arg.into())).into_value(tag.clone()),
);
map.insert("path".to_string(), path);
map.insert(
"builtin".to_string(),
UntaggedValue::Primitive(Primitive::Boolean(builtin)).into_value(tag.clone()),
);
UntaggedValue::row(map).into_value(tag)
}
macro_rules! entry_builtin {
($arg:expr, $tag:expr) => {
entry(
$arg.clone(),
UntaggedValue::Primitive(Primitive::String("nushell built-in command".to_string()))
.into_value($tag.clone()),
true,
$tag,
)
};
}
macro_rules! entry_path {
($arg:expr, $path:expr, $tag:expr) => {
entry(
$arg.clone(),
UntaggedValue::Primitive(Primitive::Path($path)).into_value($tag.clone()),
false,
$tag,
)
};
}
#[derive(Deserialize, Debug)]
struct WhichArgs {
bin: Tagged<String>,
all: bool,
}
fn which(
WhichArgs { bin, all }: WhichArgs,
RunnableContext { commands, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
let external = bin.starts_with('^');
let item = if external {
bin.item[1..].to_string()
} else {
bin.item.clone()
};
if all {
let stream = async_stream! {
if external {
if let Ok(path) = ichwh::which(&item).await {
yield ReturnSuccess::value(entry_path!(item, path.into(), bin.tag.clone()));
}
}
let builtin = commands.has(&item);
if builtin {
yield ReturnSuccess::value(entry_builtin!(item, bin.tag.clone()));
}
if let Ok(paths) = ichwh::which_all(&item).await {
if !builtin && paths.len() == 0 {
yield Err(ShellError::labeled_error(
"Binary not found for argument, and argument is not a builtin",
"not found",
&bin.tag,
2019-08-10 09:06:08 +02:00
));
} else {
for path in paths {
yield ReturnSuccess::value(entry_path!(item, path.into(), bin.tag.clone()));
}
2019-08-10 09:06:08 +02:00
}
} else {
yield Err(ShellError::labeled_error(
"Error trying to find binary for argument",
"error",
&bin.tag,
));
2019-08-10 09:06:08 +02:00
}
};
Ok(stream.to_output_stream())
2019-08-10 09:06:08 +02:00
} else {
let stream = async_stream! {
if external {
if let Ok(path) = ichwh::which(&item).await {
yield ReturnSuccess::value(entry_path!(item, path.into(), bin.tag.clone()));
}
} else if commands.has(&item) {
yield ReturnSuccess::value(entry_builtin!(item, bin.tag.clone()));
} else if let Ok(path) = ichwh::which(&item).await {
yield ReturnSuccess::value(entry_path!(item, path.into(), bin.tag.clone()));
} else {
yield Err(ShellError::labeled_error(
"Binary not found for argument, and argument is not a builtin",
"not found",
&bin.tag,
));
}
};
2019-08-10 09:06:08 +02:00
Ok(stream.to_output_stream())
}
2019-08-10 09:06:08 +02:00
}