mirror of
https://github.com/nushell/nushell.git
synced 2025-08-17 15:31:14 +02:00
Make plugins able to find and call other commands (#13407)
# Description Adds functionality to the plugin interface to support calling internal commands from plugins. For example, using `view ir --json`: ```rust let closure: Value = call.req(0)?; let Some(decl_id) = engine.find_decl("view ir")? else { return Err(LabeledError::new("`view ir` not found")); }; let ir_json = engine.call_decl( decl_id, EvaluatedCall::new(call.head) .with_named("json".into_spanned(call.head), Value::bool(true, call.head)) .with_positional(closure), PipelineData::Empty, true, false, )?.into_value()?.into_string()?; let ir = serde_json::from_value(&ir_json); // ... ``` # User-Facing Changes Plugin developers can now use `EngineInterface::find_decl()` and `call_decl()` to call internal commands, which could be handy for formatters like `to csv` or `to nuon`, or for reflection commands that help gain insight into the engine. # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting - [ ] release notes - [ ] update plugin protocol documentation: `FindDecl`, `CallDecl` engine calls; `Identifier` engine call response
This commit is contained in:
@ -22,7 +22,7 @@ mod tests;
|
||||
pub mod test_util;
|
||||
|
||||
use nu_protocol::{
|
||||
ast::Operator, engine::Closure, ByteStreamType, Config, LabeledError, PipelineData,
|
||||
ast::Operator, engine::Closure, ByteStreamType, Config, DeclId, LabeledError, PipelineData,
|
||||
PluginMetadata, PluginSignature, ShellError, Span, Spanned, Value,
|
||||
};
|
||||
use nu_utils::SharedCow;
|
||||
@ -494,6 +494,21 @@ pub enum EngineCall<D> {
|
||||
/// Whether to redirect stderr from external commands
|
||||
redirect_stderr: bool,
|
||||
},
|
||||
/// Find a declaration by name
|
||||
FindDecl(String),
|
||||
/// Call a declaration with args
|
||||
CallDecl {
|
||||
/// The id of the declaration to be called (can be found with `FindDecl`)
|
||||
decl_id: DeclId,
|
||||
/// Information about the call (head span, arguments, etc.)
|
||||
call: EvaluatedCall,
|
||||
/// Pipeline input to the call
|
||||
input: D,
|
||||
/// Whether to redirect stdout from external commands
|
||||
redirect_stdout: bool,
|
||||
/// Whether to redirect stderr from external commands
|
||||
redirect_stderr: bool,
|
||||
},
|
||||
}
|
||||
|
||||
impl<D> EngineCall<D> {
|
||||
@ -511,6 +526,8 @@ impl<D> EngineCall<D> {
|
||||
EngineCall::LeaveForeground => "LeaveForeground",
|
||||
EngineCall::GetSpanContents(_) => "GetSpanContents",
|
||||
EngineCall::EvalClosure { .. } => "EvalClosure",
|
||||
EngineCall::FindDecl(_) => "FindDecl",
|
||||
EngineCall::CallDecl { .. } => "CallDecl",
|
||||
}
|
||||
}
|
||||
|
||||
@ -544,6 +561,20 @@ impl<D> EngineCall<D> {
|
||||
redirect_stdout,
|
||||
redirect_stderr,
|
||||
},
|
||||
EngineCall::FindDecl(name) => EngineCall::FindDecl(name),
|
||||
EngineCall::CallDecl {
|
||||
decl_id,
|
||||
call,
|
||||
input,
|
||||
redirect_stdout,
|
||||
redirect_stderr,
|
||||
} => EngineCall::CallDecl {
|
||||
decl_id,
|
||||
call,
|
||||
input: f(input)?,
|
||||
redirect_stdout,
|
||||
redirect_stderr,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -556,6 +587,7 @@ pub enum EngineCallResponse<D> {
|
||||
PipelineData(D),
|
||||
Config(SharedCow<Config>),
|
||||
ValueMap(HashMap<String, Value>),
|
||||
Identifier(usize),
|
||||
}
|
||||
|
||||
impl<D> EngineCallResponse<D> {
|
||||
@ -570,6 +602,7 @@ impl<D> EngineCallResponse<D> {
|
||||
EngineCallResponse::PipelineData(data) => EngineCallResponse::PipelineData(f(data)?),
|
||||
EngineCallResponse::Config(config) => EngineCallResponse::Config(config),
|
||||
EngineCallResponse::ValueMap(map) => EngineCallResponse::ValueMap(map),
|
||||
EngineCallResponse::Identifier(id) => EngineCallResponse::Identifier(id),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user