2022-09-07 14:07:42 +00:00
|
|
|
use crate::EvaluatedCall;
|
2021-12-12 11:50:35 +00:00
|
|
|
|
2022-09-07 14:07:42 +00:00
|
|
|
use super::{call_plugin, create_command, get_plugin_encoding};
|
2022-07-25 16:32:56 +00:00
|
|
|
use crate::protocol::{
|
|
|
|
CallInfo, CallInput, PluginCall, PluginCustomValue, PluginData, PluginResponse,
|
|
|
|
};
|
2021-12-12 11:50:35 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2022-01-28 18:32:33 +00:00
|
|
|
use nu_protocol::{ast::Call, Signature};
|
2022-07-25 16:32:56 +00:00
|
|
|
use nu_protocol::{PipelineData, ShellError, Value};
|
2021-12-12 11:50:35 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PluginDeclaration {
|
|
|
|
name: String,
|
|
|
|
signature: Signature,
|
|
|
|
filename: PathBuf,
|
2021-12-18 18:13:56 +00:00
|
|
|
shell: Option<PathBuf>,
|
2021-12-12 11:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginDeclaration {
|
2022-09-07 14:07:42 +00:00
|
|
|
pub fn new(filename: PathBuf, signature: Signature, shell: Option<PathBuf>) -> Self {
|
2021-12-12 11:50:35 +00:00
|
|
|
Self {
|
|
|
|
name: signature.name.clone(),
|
|
|
|
signature,
|
|
|
|
filename,
|
2021-12-18 18:13:56 +00:00
|
|
|
shell,
|
2021-12-12 11:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Command for PluginDeclaration {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
self.signature.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
self.signature.usage.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
// Call the command with self path
|
|
|
|
// Decode information from plugin
|
|
|
|
// Create PipelineData
|
|
|
|
let source_file = Path::new(&self.filename);
|
2021-12-18 18:13:56 +00:00
|
|
|
let mut plugin_cmd = create_command(source_file, &self.shell);
|
2022-08-08 12:26:49 +00:00
|
|
|
// We need the current environment variables for `python` based plugins
|
|
|
|
// Or we'll likely have a problem when a plugin is implemented in a virtual Python environment.
|
|
|
|
let current_envs = nu_engine::env::env_to_strings(engine_state, stack).unwrap_or_default();
|
|
|
|
plugin_cmd.envs(current_envs);
|
2021-12-12 11:50:35 +00:00
|
|
|
|
|
|
|
let mut child = plugin_cmd.spawn().map_err(|err| {
|
|
|
|
let decl = engine_state.get_decl(call.decl_id);
|
2022-04-18 12:34:10 +00:00
|
|
|
ShellError::GenericError(
|
2021-12-12 11:50:35 +00:00
|
|
|
format!("Unable to spawn plugin for {}", decl.name()),
|
|
|
|
format!("{}", err),
|
2022-04-18 12:34:10 +00:00
|
|
|
Some(call.head),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
2021-12-12 11:50:35 +00:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
2022-01-28 18:32:33 +00:00
|
|
|
let input = input.into_value(call.head);
|
2022-07-25 16:32:56 +00:00
|
|
|
let input = match input {
|
|
|
|
Value::CustomValue { val, span } => {
|
|
|
|
match val.as_any().downcast_ref::<PluginCustomValue>() {
|
|
|
|
Some(plugin_data) if plugin_data.filename == self.filename => {
|
|
|
|
CallInput::Data(PluginData {
|
|
|
|
data: plugin_data.data.clone(),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let custom_value_name = val.value_string();
|
|
|
|
return Err(ShellError::GenericError(
|
|
|
|
format!(
|
|
|
|
"Plugin {} can not handle the custom value {}",
|
|
|
|
self.name, custom_value_name
|
|
|
|
),
|
|
|
|
format!("custom value {}", custom_value_name),
|
|
|
|
Some(span),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value => CallInput::Value(value),
|
|
|
|
};
|
2021-12-12 11:50:35 +00:00
|
|
|
|
2022-07-25 16:32:56 +00:00
|
|
|
let plugin_call = PluginCall::CallInfo(CallInfo {
|
|
|
|
name: self.name.clone(),
|
|
|
|
call: EvaluatedCall::try_from_call(call, engine_state, stack)?,
|
|
|
|
input,
|
|
|
|
});
|
2021-12-12 11:50:35 +00:00
|
|
|
|
2022-09-07 14:07:42 +00:00
|
|
|
let encoding = {
|
|
|
|
let stdout_reader = match &mut child.stdout {
|
|
|
|
Some(out) => out,
|
|
|
|
None => {
|
|
|
|
return Err(ShellError::PluginFailedToLoad(
|
|
|
|
"Plugin missing stdout reader".into(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
get_plugin_encoding(stdout_reader)?
|
|
|
|
};
|
|
|
|
let response = call_plugin(&mut child, plugin_call, &encoding, call.head).map_err(|err| {
|
|
|
|
let decl = engine_state.get_decl(call.decl_id);
|
|
|
|
ShellError::GenericError(
|
|
|
|
format!("Unable to decode call for {}", decl.name()),
|
|
|
|
err.to_string(),
|
|
|
|
Some(call.head),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
)
|
|
|
|
});
|
2021-12-12 11:50:35 +00:00
|
|
|
|
2022-07-25 16:32:56 +00:00
|
|
|
let pipeline_data = match response {
|
|
|
|
Ok(PluginResponse::Value(value)) => {
|
|
|
|
Ok(PipelineData::Value(value.as_ref().clone(), None))
|
2021-12-12 11:50:35 +00:00
|
|
|
}
|
2022-07-25 16:32:56 +00:00
|
|
|
Ok(PluginResponse::PluginData(name, plugin_data)) => Ok(PipelineData::Value(
|
|
|
|
Value::CustomValue {
|
|
|
|
val: Box::new(PluginCustomValue {
|
|
|
|
name,
|
|
|
|
data: plugin_data.data,
|
|
|
|
filename: self.filename.clone(),
|
|
|
|
shell: self.shell.clone(),
|
|
|
|
source: engine_state.get_decl(call.decl_id).name().to_owned(),
|
|
|
|
}),
|
|
|
|
span: plugin_data.span,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
Ok(PluginResponse::Error(err)) => Err(err.into()),
|
|
|
|
Ok(PluginResponse::Signature(..)) => Err(ShellError::GenericError(
|
|
|
|
"Plugin missing value".into(),
|
|
|
|
"Received a signature from plugin instead of value".into(),
|
2022-04-18 12:34:10 +00:00
|
|
|
Some(call.head),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
2022-07-25 16:32:56 +00:00
|
|
|
)),
|
|
|
|
Err(err) => Err(err),
|
2022-01-31 15:20:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// We need to call .wait() on the child, or we'll risk summoning the zombie horde
|
|
|
|
let _ = child.wait();
|
2021-12-12 11:50:35 +00:00
|
|
|
|
2022-01-31 15:20:11 +00:00
|
|
|
pipeline_data
|
2021-12-12 11:50:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 14:07:42 +00:00
|
|
|
fn is_plugin(&self) -> Option<(&PathBuf, &Option<PathBuf>)> {
|
|
|
|
Some((&self.filename, &self.shell))
|
2021-12-12 11:50:35 +00:00
|
|
|
}
|
|
|
|
}
|