Skip to content

Commit

Permalink
fix(BREAKING): move .resolve_command_path to ShellState
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed May 8, 2024
1 parent 5fb9d68 commit 37ec6df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
17 changes: 0 additions & 17 deletions src/shell/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ mod unset;
mod xargs;

use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;

use futures::future::LocalBoxFuture;
Expand Down Expand Up @@ -117,22 +116,6 @@ pub struct ShellCommandContext {
Box<dyn FnOnce(ExecuteCommandArgsContext) -> FutureExecuteResult>,
}

impl ShellCommandContext {
/// Resolves the path to a command from the current working directory.
///
/// Does not take injected custom commands into account.
pub fn resolve_command_path(
&self,
command_name: &str,
) -> Result<PathBuf, crate::ResolveCommandPathError> {
super::command::resolve_command_path(
command_name,
self.state.cwd(),
&self.state,
)
}
}

pub trait ShellCommand {
fn execute(
&self,
Expand Down
2 changes: 1 addition & 1 deletion src/shell/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ fn execute_command_args(
)
}),
};
match command_context.state.resolve_command(&command_name) {
match command_context.state.resolve_custom_command(&command_name) {
Some(command) => command.execute(command_context),
None => execute_unresolved_command_name(
UnresolvedCommandName {
Expand Down
5 changes: 4 additions & 1 deletion src/shell/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,10 @@ async fn custom_command_resolve_command_path() {
"custom_which",
Box::new(|mut context| {
async move {
let path = context.resolve_command_path(&context.args[0]).unwrap();
let path = context
.state
.resolve_command_path(&context.args[0])
.unwrap();
let _ = context.stdout.write_line(&path.to_string_lossy());
ExecuteResult::from_exit_code(0)
}
Expand Down
16 changes: 15 additions & 1 deletion src/shell/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,25 @@ impl ShellState {
&self.token
}

pub fn resolve_command(&self, name: &str) -> Option<Rc<dyn ShellCommand>> {
/// Resolves a custom command that was injected.
pub fn resolve_custom_command(
&self,
name: &str,
) -> Option<Rc<dyn ShellCommand>> {
// uses an Rc to allow resolving a command without borrowing from self
self.commands.get(name).cloned()
}

/// Resolves the path to a command from the current working directory.
///
/// Does not take injected custom commands into account.
pub fn resolve_command_path(
&self,
command_name: &str,
) -> Result<PathBuf, crate::ResolveCommandPathError> {
super::command::resolve_command_path(command_name, self.cwd(), self)
}

pub fn with_child_token(&self) -> ShellState {
let mut state = self.clone();
state.token = self.token.child_token();
Expand Down

0 comments on commit 37ec6df

Please sign in to comment.