From c97578bf6e4ac54159a4215835e99593e923c51c Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 10 Aug 2019 19:06:08 +1200 Subject: [PATCH] Add which command --- src/cli.rs | 1 + src/commands.rs | 2 ++ src/commands/which_.rs | 70 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/commands/which_.rs diff --git a/src/cli.rs b/src/cli.rs index ff0deae11a..af81058723 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -191,6 +191,7 @@ pub async fn cli() -> Result<(), Box> { static_command(Save), static_command(Table), static_command(VTable), + static_command(Which), ]); } let _ = load_plugins(&mut context); diff --git a/src/commands.rs b/src/commands.rs index dfe8155528..9366257a8d 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -48,6 +48,7 @@ crate mod to_yaml; crate mod trim; crate mod vtable; crate mod where_; +crate mod which_; crate use autoview::Autoview; //crate use cd::Cd; @@ -69,3 +70,4 @@ crate use skip_while::SkipWhile; crate use table::Table; crate use vtable::VTable; crate use where_::Where; +crate use which_::Which; diff --git a/src/commands/which_.rs b/src/commands/which_.rs new file mode 100644 index 0000000000..c1db491c85 --- /dev/null +++ b/src/commands/which_.rs @@ -0,0 +1,70 @@ +use crate::errors::ShellError; +use crate::object::Value; +use crate::prelude::*; + +use crate::commands::StaticCommand; +use crate::parser::registry::Signature; + +pub struct Which; + +impl StaticCommand for Which { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + which(args, registry) + } + fn name(&self) -> &str { + "which" + } + + fn signature(&self) -> Signature { + Signature::build("which").required("name", SyntaxType::Any) + } +} + +pub fn which(args: CommandArgs, registry: &CommandRegistry) -> Result { + let args = args.evaluate_once(registry)?; + + let mut which_out = VecDeque::new(); + let span = args.call_info.name_span; + + if let Some(v) = &args.call_info.args.positional { + if v.len() > 0 { + match &v[0] { + Tagged { + item: Value::Primitive(Primitive::String(s)), + tag, + } => match which::which(&s) { + Ok(ok) => { + which_out + .push_back(Value::Primitive(Primitive::Path(ok)).tagged(tag.clone())); + } + _ => {} + }, + Tagged { tag, .. } => { + return Err(ShellError::labeled_error( + "Expected a filename to find", + "needs a filename", + tag.span, + )); + } + } + } else { + return Err(ShellError::labeled_error( + "Expected a binary to find", + "needs application name", + span, + )); + } + } else { + return Err(ShellError::labeled_error( + "Expected a binary to find", + "needs application name", + span, + )); + } + + Ok(which_out.to_output_stream()) +}