2019-06-01 07:50:16 +02:00
|
|
|
use crate::parser::{Args, CommandConfig, CommandRegistry};
|
2019-05-15 18:12:38 +02:00
|
|
|
use crate::prelude::*;
|
2019-05-16 02:21:46 +02:00
|
|
|
|
2019-05-26 08:54:41 +02:00
|
|
|
use indexmap::IndexMap;
|
2019-05-15 18:12:38 +02:00
|
|
|
use std::error::Error;
|
2019-05-22 09:12:03 +02:00
|
|
|
use std::sync::Arc;
|
2019-05-15 18:12:38 +02:00
|
|
|
|
|
|
|
pub struct Context {
|
2019-05-26 08:54:41 +02:00
|
|
|
commands: IndexMap<String, Arc<dyn Command>>,
|
2019-05-24 06:34:43 +02:00
|
|
|
crate host: Arc<Mutex<dyn Host + Send>>,
|
2019-05-23 09:23:06 +02:00
|
|
|
crate env: Arc<Mutex<Environment>>,
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
crate fn basic() -> Result<Context, Box<Error>> {
|
|
|
|
Ok(Context {
|
2019-05-16 00:58:44 +02:00
|
|
|
commands: indexmap::IndexMap::new(),
|
2019-05-23 09:23:06 +02:00
|
|
|
host: Arc::new(Mutex::new(crate::env::host::BasicHost)),
|
|
|
|
env: Arc::new(Mutex::new(Environment::basic()?)),
|
2019-05-15 18:12:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-28 08:45:18 +02:00
|
|
|
pub fn add_commands(&mut self, commands: Vec<Arc<dyn Command>>) {
|
|
|
|
for command in commands {
|
|
|
|
self.commands.insert(command.name().to_string(), command);
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 08:54:41 +02:00
|
|
|
pub fn clone_commands(&self) -> indexmap::IndexMap<String, Arc<dyn Command>> {
|
|
|
|
self.commands.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn registry(&self) -> CommandMap {
|
|
|
|
CommandMap {
|
|
|
|
commands: self.clone_commands(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
crate fn has_command(&self, name: &str) -> bool {
|
2019-05-15 18:12:38 +02:00
|
|
|
self.commands.contains_key(name)
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
crate fn get_command(&self, name: &str) -> Arc<dyn Command> {
|
|
|
|
self.commands.get(name).unwrap().clone()
|
|
|
|
}
|
|
|
|
|
2019-05-16 02:21:46 +02:00
|
|
|
crate fn run_command(
|
2019-05-22 09:12:03 +02:00
|
|
|
&mut self,
|
|
|
|
command: Arc<dyn Command>,
|
2019-06-01 07:50:16 +02:00
|
|
|
args: Args,
|
2019-05-23 09:23:06 +02:00
|
|
|
input: InputStream,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-05-16 02:21:46 +02:00
|
|
|
let command_args = CommandArgs {
|
2019-05-23 09:23:06 +02:00
|
|
|
host: self.host.clone(),
|
|
|
|
env: self.env.clone(),
|
2019-06-01 07:50:16 +02:00
|
|
|
positional: args.positional,
|
|
|
|
named: args.named,
|
2019-05-16 02:21:46 +02:00
|
|
|
input,
|
|
|
|
};
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
command.run(command_args)
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-26 08:54:41 +02:00
|
|
|
|
|
|
|
pub struct CommandMap {
|
|
|
|
#[allow(unused)]
|
|
|
|
commands: IndexMap<String, Arc<dyn Command>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandRegistry for CommandMap {
|
|
|
|
fn get(&self, name: &str) -> CommandConfig {
|
|
|
|
CommandConfig {
|
|
|
|
name: name.to_string(),
|
|
|
|
mandatory_positional: vec![],
|
|
|
|
optional_positional: vec![],
|
|
|
|
rest_positional: true,
|
|
|
|
named: IndexMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|