mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
Allow plugins to receive configuration from the nushell configuration (#10955)
# 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)
This commit is contained in:
@ -3,6 +3,22 @@ use nu_protocol::{Record, Value};
|
||||
pub struct Example;
|
||||
|
||||
impl Example {
|
||||
pub fn config(
|
||||
&self,
|
||||
config: &Option<Value>,
|
||||
call: &EvaluatedCall,
|
||||
) -> Result<Value, LabeledError> {
|
||||
match config {
|
||||
Some(config) => Ok(config.clone()),
|
||||
None => Err(LabeledError {
|
||||
label: "No config sent".into(),
|
||||
msg: "Configuration for this plugin was not found in `$env.config.plugins.example`"
|
||||
.into(),
|
||||
span: Some(call.head),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn print_values(
|
||||
&self,
|
||||
index: u32,
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::Example;
|
||||
use nu_plugin::{EvaluatedCall, LabeledError, Plugin};
|
||||
use nu_protocol::{Category, PluginExample, PluginSignature, SyntaxShape, Value};
|
||||
use nu_protocol::{Category, PluginExample, PluginSignature, SyntaxShape, Type, Value};
|
||||
|
||||
impl Plugin for Example {
|
||||
fn signature(&self) -> Vec<PluginSignature> {
|
||||
@ -42,12 +42,19 @@ impl Plugin for Example {
|
||||
.named("named", SyntaxShape::String, "named string", Some('n'))
|
||||
.rest("rest", SyntaxShape::String, "rest value string")
|
||||
.category(Category::Experimental),
|
||||
PluginSignature::build("nu-example-config")
|
||||
.usage("Show plugin configuration")
|
||||
.extra_usage("The configuration is set under $env.config.plugins.example")
|
||||
.category(Category::Experimental)
|
||||
.search_terms(vec!["example".into(), "configuration".into()])
|
||||
.input_output_type(Type::Nothing, Type::Table(vec![])),
|
||||
]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&mut self,
|
||||
name: &str,
|
||||
config: &Option<Value>,
|
||||
call: &EvaluatedCall,
|
||||
input: &Value,
|
||||
) -> Result<Value, LabeledError> {
|
||||
@ -56,6 +63,7 @@ impl Plugin for Example {
|
||||
"nu-example-1" => self.test1(call, input),
|
||||
"nu-example-2" => self.test2(call, input),
|
||||
"nu-example-3" => self.test3(call, input),
|
||||
"nu-example-config" => self.config(config, call),
|
||||
_ => 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(),
|
||||
|
Reference in New Issue
Block a user