2019-05-15 18:12:38 +02:00
|
|
|
use crate::prelude::*;
|
2019-05-16 02:21:46 +02:00
|
|
|
|
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-23 06:30:43 +02:00
|
|
|
commands: indexmap::IndexMap<String, Arc<dyn Command>>,
|
|
|
|
crate host: Box<dyn Host>,
|
2019-05-15 18:12:38 +02:00
|
|
|
crate env: Environment,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-15 18:12:38 +02:00
|
|
|
host: Box::new(crate::env::host::BasicHost),
|
2019-05-23 06:30:43 +02:00
|
|
|
env: Environment::basic()?,
|
2019-05-15 18:12:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-23 06:30:43 +02:00
|
|
|
pub fn add_commands(&mut self, commands: Vec<(&str, Arc<dyn Command>)>) {
|
2019-05-15 18:12:38 +02:00
|
|
|
for (name, command) in commands {
|
|
|
|
self.commands.insert(name.to_string(), command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-15 18:12:38 +02:00
|
|
|
arg_list: Vec<Value>,
|
2019-05-16 02:21:46 +02:00
|
|
|
input: VecDeque<Value>,
|
|
|
|
) -> Result<VecDeque<ReturnValue>, ShellError> {
|
|
|
|
let command_args = CommandArgs {
|
2019-05-22 09:12:03 +02:00
|
|
|
host: &mut self.host,
|
2019-05-16 02:21:46 +02:00
|
|
|
env: &self.env,
|
|
|
|
args: arg_list,
|
|
|
|
input,
|
|
|
|
};
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
command.run(command_args)
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|
|
|
|
}
|