nushell/crates/nu-command/src/system/which_.rs

268 lines
6.8 KiB
Rust
Raw Normal View History

use log::trace;
use nu_engine::env;
use nu_engine::CallExt;
use nu_protocol::record;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span,
Spanned, SyntaxShape, Type, Value,
};
use std::ffi::OsStr;
use std::path::Path;
#[derive(Clone)]
pub struct Which;
impl Command for Which {
fn name(&self) -> &str {
"which"
}
fn signature(&self) -> Signature {
Signature::build("which")
.input_output_types(vec![(Type::Nothing, Type::Table(vec![]))])
.allow_variants_without_examples(true)
.required("application", SyntaxShape::String, "application")
.rest("rest", SyntaxShape::String, "additional applications")
.switch("all", "list all executables", Some('a'))
.category(Category::System)
}
fn usage(&self) -> &str {
"Finds a program file, alias or custom command."
}
fn search_terms(&self) -> Vec<&str> {
vec!["find", "path", "location", "command"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
which(engine_state, stack, call)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Find if the 'myapp' application is available",
example: "which myapp",
result: None,
}]
}
}
// Shortcut for creating an entry to the output table
change the output of `which` to be more explicit (#9646) related to - https://github.com/nushell/nushell/issues/9637#issuecomment-1629387548 # Description this PR changes the output of `which` from `table<arg: string, path: string, built-in: bool> (stream)` to `table<command: string, path: string, type: string> (stream)`. - `command`: same as `arg` but more explicit name - `path`: same as before, `null` when built-in - `type`: instead of `buil-in: bool` says if it's a `built-in` a `custom` command, an `alias` or an `external` # User-Facing Changes the output of `which` has changed ## some examples ```nushell > which open ╭───┬─────────┬──────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼──────────┤ │ 0 │ open │ │ built-in │ ╰───┴─────────┴──────┴──────────╯ ``` ```nushell > alias foo = print "foo" > which foo ╭───┬─────────┬──────┬───────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼───────┤ │ 0 │ foo │ │ alias │ ╰───┴─────────┴──────┴───────╯ ``` ```nushell > def bar [] {} > which bar ╭───┬─────────┬──────┬────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼────────┤ │ 0 │ bar │ │ custom │ ╰───┴─────────┴──────┴────────╯ ``` ```nushell > which git ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ git │ /usr/bin/git │ external │ ╰───┴─────────┴──────────────┴──────────╯ ``` ```nushell > which open git foo bar ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ open │ │ built-in │ │ 1 │ git │ /usr/bin/git │ external │ │ 2 │ foo │ │ alias │ │ 3 │ bar │ │ custom │ ╰───┴─────────┴──────────────┴──────────╯ ``` # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` # After Submitting mention that in the release note
2023-07-21 02:10:53 +02:00
fn entry(
arg: impl Into<String>,
path: impl Into<String>,
cmd_type: impl Into<String>,
span: Span,
) -> Value {
Value::record(
record! {
"command" => Value::string(arg.into(), span),
"path" => Value::string(path.into(), span),
"type" => Value::string(cmd_type.into(), span),
},
span,
)
}
fn get_entry_in_commands(engine_state: &EngineState, name: &str, span: Span) -> Option<Value> {
Overlays (#5375) * WIP: Start laying overlays * Rename Overlay->Module; Start adding overlay * Revamp adding overlay * Add overlay add tests; Disable debug print * Fix overlay add; Add overlay remove * Add overlay remove tests * Add missing overlay remove file * Add overlay list command * (WIP?) Enable overlays for env vars * Move OverlayFrames to ScopeFrames * (WIP) Move everything to overlays only ScopeFrame contains nothing but overlays now * Fix predecls * Fix wrong overlay id translation and aliases * Fix broken env lookup logic * Remove TODOs * Add overlay add + remove for environment * Add a few overlay tests; Fix overlay add name * Some cleanup; Fix overlay add/remove names * Clippy * Fmt * Remove walls of comments * List overlays from stack; Add debugging flag Currently, the engine state ordering is somehow broken. * Fix (?) overlay list test * Fix tests on Windows * Fix activated overlay ordering * Check for active overlays equality in overlay list This removes the -p flag: Either both parser and engine will have the same overlays, or the command will fail. * Add merging on overlay remove * Change help message and comment * Add some remove-merge/discard tests * (WIP) Track removed overlays properly * Clippy; Fmt * Fix getting last overlay; Fix predecls in overlays * Remove merging; Fix re-add overwriting stuff Also some error message tweaks. * Fix overlay error in the engine * Update variable_completions.rs * Adds flags and optional arguments to view-source (#5446) * added flags and optional arguments to view-source * removed redundant code * removed redundant code * fmt * fix bug in shell_integration (#5450) * fix bug in shell_integration * add some comments * enable cd to work with directory abbreviations (#5452) * enable cd to work with abbreviations * add abbreviation example * fix tests * make it configurable * make cd recornize symblic link (#5454) * implement seq char command to generate single character sequence (#5453) * add tmp code * add seq char command * Add split number flag in `split row` (#5434) Signed-off-by: Yuheng Su <gipsyh.icu@gmail.com> * Add two more overlay tests * Add ModuleId to OverlayFrame * Fix env conversion accidentally activating overlay It activated overlay from permanent state prematurely which would cause `overlay add` to misbehave. * Remove unused parameter; Add overlay list test * Remove added traces * Add overlay commands examples * Modify TODO * Fix $nu.scope iteration * Disallow removing default overlay * Refactor some parser errors * Remove last overlay if no argument * Diversify overlay examples * Make it possible to update overlay's module In case the origin module updates, the overlay add loads the new module, makes it overlay's origin and applies the changes. Before, it was impossible to update the overlay if the module changed. Co-authored-by: JT <547158+jntrnr@users.noreply.github.com> Co-authored-by: pwygab <88221256+merelymyself@users.noreply.github.com> Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> Co-authored-by: WindSoilder <WindSoilder@outlook.com> Co-authored-by: Yuheng Su <gipsyh.icu@gmail.com>
2022-05-07 21:39:22 +02:00
if let Some(decl_id) = engine_state.find_decl(name.as_bytes(), &[]) {
change the output of `which` to be more explicit (#9646) related to - https://github.com/nushell/nushell/issues/9637#issuecomment-1629387548 # Description this PR changes the output of `which` from `table<arg: string, path: string, built-in: bool> (stream)` to `table<command: string, path: string, type: string> (stream)`. - `command`: same as `arg` but more explicit name - `path`: same as before, `null` when built-in - `type`: instead of `buil-in: bool` says if it's a `built-in` a `custom` command, an `alias` or an `external` # User-Facing Changes the output of `which` has changed ## some examples ```nushell > which open ╭───┬─────────┬──────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼──────────┤ │ 0 │ open │ │ built-in │ ╰───┴─────────┴──────┴──────────╯ ``` ```nushell > alias foo = print "foo" > which foo ╭───┬─────────┬──────┬───────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼───────┤ │ 0 │ foo │ │ alias │ ╰───┴─────────┴──────┴───────╯ ``` ```nushell > def bar [] {} > which bar ╭───┬─────────┬──────┬────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼────────┤ │ 0 │ bar │ │ custom │ ╰───┴─────────┴──────┴────────╯ ``` ```nushell > which git ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ git │ /usr/bin/git │ external │ ╰───┴─────────┴──────────────┴──────────╯ ``` ```nushell > which open git foo bar ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ open │ │ built-in │ │ 1 │ git │ /usr/bin/git │ external │ │ 2 │ foo │ │ alias │ │ 3 │ bar │ │ custom │ ╰───┴─────────┴──────────────┴──────────╯ ``` # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` # After Submitting mention that in the release note
2023-07-21 02:10:53 +02:00
let cmd_type = if engine_state.get_decl(decl_id).is_custom_command() {
"custom"
fixes `which` showing aliases as built-in nushell commands (#9580) fixes #8577 # Description Currently, using `which` on an alias describes it as a nushell built-in command: ```bash > alias foo = print "foo!" > which ls foo --all ╭───┬─────┬──────────────────────────┬──────────╮ │ # │ arg │ path │ built-in │ ├───┼─────┼──────────────────────────┼──────────┤ │ 0 │ ls │ Nushell built-in command │ true │ │ 1 │ ls │ /bin/ls │ false │ │ 2 │ foo │ Nushell built-in command │ true │ ╰───┴─────┴──────────────────────────┴──────────╯ ``` This PR fixes the behaviour above to the following: ```bash > alias foo = print "foo!" > which ls foo --all ╭───┬─────┬──────────────────────────┬──────────╮ │ # │ arg │ path │ built-in │ ├───┼─────┼──────────────────────────┼──────────┤ │ 0 │ ls │ Nushell built-in command │ true │ │ 1 │ ls │ /bin/ls │ false │ │ 2 │ foo │ Nushell alias │ false │ ╰───┴─────┴──────────────────────────┴──────────╯ ``` # User-Facing Changes Passing in an alias to `which` will no longer return `Nushell built-in command`, `true` for `path` and `built-in` respectively. # Tests + Formatting # After Submitting
2023-07-08 10:48:42 +02:00
} else if engine_state.get_decl(decl_id).is_alias() {
change the output of `which` to be more explicit (#9646) related to - https://github.com/nushell/nushell/issues/9637#issuecomment-1629387548 # Description this PR changes the output of `which` from `table<arg: string, path: string, built-in: bool> (stream)` to `table<command: string, path: string, type: string> (stream)`. - `command`: same as `arg` but more explicit name - `path`: same as before, `null` when built-in - `type`: instead of `buil-in: bool` says if it's a `built-in` a `custom` command, an `alias` or an `external` # User-Facing Changes the output of `which` has changed ## some examples ```nushell > which open ╭───┬─────────┬──────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼──────────┤ │ 0 │ open │ │ built-in │ ╰───┴─────────┴──────┴──────────╯ ``` ```nushell > alias foo = print "foo" > which foo ╭───┬─────────┬──────┬───────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼───────┤ │ 0 │ foo │ │ alias │ ╰───┴─────────┴──────┴───────╯ ``` ```nushell > def bar [] {} > which bar ╭───┬─────────┬──────┬────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼────────┤ │ 0 │ bar │ │ custom │ ╰───┴─────────┴──────┴────────╯ ``` ```nushell > which git ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ git │ /usr/bin/git │ external │ ╰───┴─────────┴──────────────┴──────────╯ ``` ```nushell > which open git foo bar ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ open │ │ built-in │ │ 1 │ git │ /usr/bin/git │ external │ │ 2 │ foo │ │ alias │ │ 3 │ bar │ │ custom │ ╰───┴─────────┴──────────────┴──────────╯ ``` # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` # After Submitting mention that in the release note
2023-07-21 02:10:53 +02:00
"alias"
} else {
change the output of `which` to be more explicit (#9646) related to - https://github.com/nushell/nushell/issues/9637#issuecomment-1629387548 # Description this PR changes the output of `which` from `table<arg: string, path: string, built-in: bool> (stream)` to `table<command: string, path: string, type: string> (stream)`. - `command`: same as `arg` but more explicit name - `path`: same as before, `null` when built-in - `type`: instead of `buil-in: bool` says if it's a `built-in` a `custom` command, an `alias` or an `external` # User-Facing Changes the output of `which` has changed ## some examples ```nushell > which open ╭───┬─────────┬──────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼──────────┤ │ 0 │ open │ │ built-in │ ╰───┴─────────┴──────┴──────────╯ ``` ```nushell > alias foo = print "foo" > which foo ╭───┬─────────┬──────┬───────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼───────┤ │ 0 │ foo │ │ alias │ ╰───┴─────────┴──────┴───────╯ ``` ```nushell > def bar [] {} > which bar ╭───┬─────────┬──────┬────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼────────┤ │ 0 │ bar │ │ custom │ ╰───┴─────────┴──────┴────────╯ ``` ```nushell > which git ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ git │ /usr/bin/git │ external │ ╰───┴─────────┴──────────────┴──────────╯ ``` ```nushell > which open git foo bar ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ open │ │ built-in │ │ 1 │ git │ /usr/bin/git │ external │ │ 2 │ foo │ │ alias │ │ 3 │ bar │ │ custom │ ╰───┴─────────┴──────────────┴──────────╯ ``` # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` # After Submitting mention that in the release note
2023-07-21 02:10:53 +02:00
"built-in"
};
trace!("Found command: {}", name);
change the output of `which` to be more explicit (#9646) related to - https://github.com/nushell/nushell/issues/9637#issuecomment-1629387548 # Description this PR changes the output of `which` from `table<arg: string, path: string, built-in: bool> (stream)` to `table<command: string, path: string, type: string> (stream)`. - `command`: same as `arg` but more explicit name - `path`: same as before, `null` when built-in - `type`: instead of `buil-in: bool` says if it's a `built-in` a `custom` command, an `alias` or an `external` # User-Facing Changes the output of `which` has changed ## some examples ```nushell > which open ╭───┬─────────┬──────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼──────────┤ │ 0 │ open │ │ built-in │ ╰───┴─────────┴──────┴──────────╯ ``` ```nushell > alias foo = print "foo" > which foo ╭───┬─────────┬──────┬───────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼───────┤ │ 0 │ foo │ │ alias │ ╰───┴─────────┴──────┴───────╯ ``` ```nushell > def bar [] {} > which bar ╭───┬─────────┬──────┬────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼────────┤ │ 0 │ bar │ │ custom │ ╰───┴─────────┴──────┴────────╯ ``` ```nushell > which git ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ git │ /usr/bin/git │ external │ ╰───┴─────────┴──────────────┴──────────╯ ``` ```nushell > which open git foo bar ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ open │ │ built-in │ │ 1 │ git │ /usr/bin/git │ external │ │ 2 │ foo │ │ alias │ │ 3 │ bar │ │ custom │ ╰───┴─────────┴──────────────┴──────────╯ ``` # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` # After Submitting mention that in the release note
2023-07-21 02:10:53 +02:00
Some(entry(name, "", cmd_type, span))
} else {
None
}
}
fn get_entries_in_nu(
engine_state: &EngineState,
name: &str,
span: Span,
skip_after_first_found: bool,
) -> Vec<Value> {
let mut all_entries = vec![];
if !all_entries.is_empty() && skip_after_first_found {
return all_entries;
}
if let Some(ent) = get_entry_in_commands(engine_state, name, span) {
all_entries.push(ent);
}
all_entries
}
#[cfg(feature = "which-support")]
fn get_first_entry_in_path(
item: &str,
span: Span,
cwd: impl AsRef<Path>,
paths: impl AsRef<OsStr>,
) -> Option<Value> {
which::which_in(item, Some(paths), cwd)
change the output of `which` to be more explicit (#9646) related to - https://github.com/nushell/nushell/issues/9637#issuecomment-1629387548 # Description this PR changes the output of `which` from `table<arg: string, path: string, built-in: bool> (stream)` to `table<command: string, path: string, type: string> (stream)`. - `command`: same as `arg` but more explicit name - `path`: same as before, `null` when built-in - `type`: instead of `buil-in: bool` says if it's a `built-in` a `custom` command, an `alias` or an `external` # User-Facing Changes the output of `which` has changed ## some examples ```nushell > which open ╭───┬─────────┬──────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼──────────┤ │ 0 │ open │ │ built-in │ ╰───┴─────────┴──────┴──────────╯ ``` ```nushell > alias foo = print "foo" > which foo ╭───┬─────────┬──────┬───────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼───────┤ │ 0 │ foo │ │ alias │ ╰───┴─────────┴──────┴───────╯ ``` ```nushell > def bar [] {} > which bar ╭───┬─────────┬──────┬────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼────────┤ │ 0 │ bar │ │ custom │ ╰───┴─────────┴──────┴────────╯ ``` ```nushell > which git ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ git │ /usr/bin/git │ external │ ╰───┴─────────┴──────────────┴──────────╯ ``` ```nushell > which open git foo bar ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ open │ │ built-in │ │ 1 │ git │ /usr/bin/git │ external │ │ 2 │ foo │ │ alias │ │ 3 │ bar │ │ custom │ ╰───┴─────────┴──────────────┴──────────╯ ``` # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` # After Submitting mention that in the release note
2023-07-21 02:10:53 +02:00
.map(|path| entry(item, path.to_string_lossy().to_string(), "external", span))
.ok()
}
#[cfg(not(feature = "which-support"))]
fn get_first_entry_in_path(
_item: &str,
_span: Span,
_cwd: impl AsRef<Path>,
_paths: impl AsRef<OsStr>,
) -> Option<Value> {
None
}
#[cfg(feature = "which-support")]
fn get_all_entries_in_path(
item: &str,
span: Span,
cwd: impl AsRef<Path>,
paths: impl AsRef<OsStr>,
) -> Vec<Value> {
which::which_in_all(&item, Some(paths), cwd)
.map(|iter| {
change the output of `which` to be more explicit (#9646) related to - https://github.com/nushell/nushell/issues/9637#issuecomment-1629387548 # Description this PR changes the output of `which` from `table<arg: string, path: string, built-in: bool> (stream)` to `table<command: string, path: string, type: string> (stream)`. - `command`: same as `arg` but more explicit name - `path`: same as before, `null` when built-in - `type`: instead of `buil-in: bool` says if it's a `built-in` a `custom` command, an `alias` or an `external` # User-Facing Changes the output of `which` has changed ## some examples ```nushell > which open ╭───┬─────────┬──────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼──────────┤ │ 0 │ open │ │ built-in │ ╰───┴─────────┴──────┴──────────╯ ``` ```nushell > alias foo = print "foo" > which foo ╭───┬─────────┬──────┬───────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼───────┤ │ 0 │ foo │ │ alias │ ╰───┴─────────┴──────┴───────╯ ``` ```nushell > def bar [] {} > which bar ╭───┬─────────┬──────┬────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────┼────────┤ │ 0 │ bar │ │ custom │ ╰───┴─────────┴──────┴────────╯ ``` ```nushell > which git ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ git │ /usr/bin/git │ external │ ╰───┴─────────┴──────────────┴──────────╯ ``` ```nushell > which open git foo bar ╭───┬─────────┬──────────────┬──────────╮ │ # │ command │ path │ type │ ├───┼─────────┼──────────────┼──────────┤ │ 0 │ open │ │ built-in │ │ 1 │ git │ /usr/bin/git │ external │ │ 2 │ foo │ │ alias │ │ 3 │ bar │ │ custom │ ╰───┴─────────┴──────────────┴──────────╯ ``` # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` # After Submitting mention that in the release note
2023-07-21 02:10:53 +02:00
iter.map(|path| entry(item, path.to_string_lossy().to_string(), "external", span))
.collect()
})
.unwrap_or_default()
}
#[cfg(not(feature = "which-support"))]
fn get_all_entries_in_path(
_item: &str,
_span: Span,
_cwd: impl AsRef<Path>,
_paths: impl AsRef<OsStr>,
) -> Vec<Value> {
vec![]
}
#[derive(Debug)]
struct WhichArgs {
applications: Vec<Spanned<String>>,
all: bool,
}
fn which_single(
application: Spanned<String>,
all: bool,
engine_state: &EngineState,
cwd: impl AsRef<Path>,
paths: impl AsRef<OsStr>,
) -> Vec<Value> {
let (external, prog_name) = if application.item.starts_with('^') {
(true, application.item[1..].to_string())
} else {
(false, application.item.clone())
};
//If prog_name is an external command, don't search for nu-specific programs
//If all is false, we can save some time by only searching for the first matching
//program
//This match handles all different cases
match (all, external) {
(true, true) => get_all_entries_in_path(&prog_name, application.span, cwd, paths),
(true, false) => {
let mut output: Vec<Value> = vec![];
output.extend(get_entries_in_nu(
engine_state,
&prog_name,
application.span,
false,
));
output.extend(get_all_entries_in_path(
&prog_name,
application.span,
cwd,
paths,
));
output
}
(false, true) => {
if let Some(entry) = get_first_entry_in_path(&prog_name, application.span, cwd, paths) {
return vec![entry];
}
vec![]
}
(false, false) => {
let nu_entries = get_entries_in_nu(engine_state, &prog_name, application.span, true);
if !nu_entries.is_empty() {
return vec![nu_entries[0].clone()];
} else if let Some(entry) =
get_first_entry_in_path(&prog_name, application.span, cwd, paths)
{
return vec![entry];
}
vec![]
}
}
}
fn which(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let which_args = WhichArgs {
applications: call.rest(engine_state, stack, 0)?,
all: call.has_flag("all"),
};
let ctrlc = engine_state.ctrlc.clone();
if which_args.applications.is_empty() {
return Err(ShellError::MissingParameter {
param_name: "application".into(),
span: call.head,
});
}
let mut output = vec![];
let cwd = env::current_dir_str(engine_state, stack)?;
let paths = env::path_str(engine_state, stack, call.head)?;
for app in which_args.applications {
let values = which_single(
app,
which_args.all,
engine_state,
cwd.clone(),
paths.clone(),
);
output.extend(values);
}
Ok(output.into_iter().into_pipeline_data(ctrlc))
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
crate::test_examples(Which)
}
}