Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(BREAKING): move .resolve_command_path to ShellState #117

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading