mirror of
https://github.com/nushell/nushell.git
synced 2025-04-23 12:48:22 +02:00
# Description When nushell calls a plugin it now sends a configuration `Value` from the nushell config under `$env.config.plugins.PLUGIN_SHORT_NAME`. This allows plugin authors to read configuration provided by plugin users. The `PLUGIN_SHORT_NAME` must match the registered filename after `nu_plugin_`. If you register `target/debug/nu_plugin_config` the `PLUGIN_NAME` will be `config` and the nushell config will loook like: $env.config = { # ... plugins: { config: [ some values ] } } Configuration may also use a closure which allows passing values from `$env` to a plugin: $env.config = { # ... plugins: { config: {|| $env.some_value } } } This is a breaking change for the plugin API as the `Plugin::run()` function now accepts a new configuration argument which is an `&Option<Value>`. If no configuration was supplied the value is `None`. Plugins compiled after this change should work with older nushell, and will behave as if the configuration was not set. Initially discussed in #10867 # User-Facing Changes * Plugins can read configuration data stored in `$env.config.plugins` * The plugin `CallInfo` now includes a `config` entry, existing plugins will require updates # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting - [ ] Update [Creating a plugin (in Rust)](https://www.nushell.sh/contributor-book/plugins.html#creating-a-plugin-in-rust) [source](https://github.com/nushell/nushell.github.io/blob/main/contributor-book/plugins.md) - [ ] Add "Configuration" section to [Plugins documentation](https://www.nushell.sh/contributor-book/plugins.html)
80 lines
2.8 KiB
Rust
80 lines
2.8 KiB
Rust
mod cool_custom_value;
|
|
mod second_custom_value;
|
|
|
|
use cool_custom_value::CoolCustomValue;
|
|
use nu_plugin::{serve_plugin, MsgPackSerializer, Plugin};
|
|
use nu_plugin::{EvaluatedCall, LabeledError};
|
|
use nu_protocol::{Category, PluginSignature, ShellError, Value};
|
|
use second_custom_value::SecondCustomValue;
|
|
|
|
struct CustomValuePlugin;
|
|
|
|
impl Plugin for CustomValuePlugin {
|
|
fn signature(&self) -> Vec<nu_protocol::PluginSignature> {
|
|
vec![
|
|
PluginSignature::build("custom-value generate")
|
|
.usage("PluginSignature for a plugin that generates a custom value")
|
|
.category(Category::Experimental),
|
|
PluginSignature::build("custom-value generate2")
|
|
.usage("PluginSignature for a plugin that generates a different custom value")
|
|
.category(Category::Experimental),
|
|
PluginSignature::build("custom-value update")
|
|
.usage("PluginSignature for a plugin that updates a custom value")
|
|
.category(Category::Experimental),
|
|
]
|
|
}
|
|
|
|
fn run(
|
|
&mut self,
|
|
name: &str,
|
|
_config: &Option<Value>,
|
|
call: &EvaluatedCall,
|
|
input: &Value,
|
|
) -> Result<Value, LabeledError> {
|
|
match name {
|
|
"custom-value generate" => self.generate(call, input),
|
|
"custom-value generate2" => self.generate2(call, input),
|
|
"custom-value update" => self.update(call, input),
|
|
_ => Err(LabeledError {
|
|
label: "Plugin call with wrong name signature".into(),
|
|
msg: "the signature used to call the plugin does not match any name in the plugin signature vector".into(),
|
|
span: Some(call.head),
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl CustomValuePlugin {
|
|
fn generate(&mut self, call: &EvaluatedCall, _input: &Value) -> Result<Value, LabeledError> {
|
|
Ok(CoolCustomValue::new("abc").into_value(call.head))
|
|
}
|
|
|
|
fn generate2(&mut self, call: &EvaluatedCall, _input: &Value) -> Result<Value, LabeledError> {
|
|
Ok(SecondCustomValue::new("xyz").into_value(call.head))
|
|
}
|
|
|
|
fn update(&mut self, call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
|
|
if let Ok(mut value) = CoolCustomValue::try_from_value(input) {
|
|
value.cool += "xyz";
|
|
return Ok(value.into_value(call.head));
|
|
}
|
|
|
|
if let Ok(mut value) = SecondCustomValue::try_from_value(input) {
|
|
value.something += "abc";
|
|
return Ok(value.into_value(call.head));
|
|
}
|
|
|
|
Err(ShellError::CantConvert {
|
|
to_type: "cool or second".into(),
|
|
from_type: "non-cool and non-second".into(),
|
|
span: call.head,
|
|
help: None,
|
|
}
|
|
.into())
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
serve_plugin(&mut CustomValuePlugin, MsgPackSerializer {})
|
|
}
|