1use super::super::{finder, finder::FindResult, script, types::CommandType};
2use super::{builtins::BUILTIN_COMMANDS, help};
3
4pub fn execute(
5 command_directory_path: &str,
6 shell: &str,
7 args: &[String],
8) -> Result<String, String> {
9 let mut args_peekable = args.iter().peekable();
10 if args_peekable.peek().is_none() {
11 return help::help(command_directory_path, &[]);
12 }
13 if let Some(&command_name) = args_peekable.peek() {
14 if let Some(command) = BUILTIN_COMMANDS.get(command_name) {
15 return (command.func)(command_directory_path, shell, args);
16 }
17 }
18 match finder::find_script(command_directory_path, args) {
19 Ok(FindResult::File(invocation)) => {
20 match script::Script::load(invocation.target.to_str().unwrap_or_default()) {
21 Ok(script) => {
22 script.get_execution_body(CommandType::Execute, shell, &invocation.args)
23 }
24 Err(error) => Err(format!("IOError loading file: {:?}", error)),
25 }
26 }
27 Ok(FindResult::Directory(_path)) => help::help(command_directory_path, args),
28 Err(err) => Err(err),
29 }
30}
31