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:
Eric Hodel
2024-01-15 00:59:47 -08:00
committed by GitHub
parent e72a4116ec
commit 7071617f18
21 changed files with 256 additions and 4 deletions

View File

@ -73,6 +73,12 @@ pub struct Config {
pub error_style: ErrorStyle,
pub use_kitty_protocol: bool,
pub highlight_resolved_externals: bool,
/// Configuration for plugins.
///
/// Users can provide configuration for a plugin through this entry. The entry name must
/// match the registered plugin name so `register nu_plugin_example` will be able to place
/// its configuration under a `nu_plugin_example` column.
pub plugins: HashMap<String, Value>,
}
impl Default for Config {
@ -139,6 +145,8 @@ impl Default for Config {
use_kitty_protocol: false,
highlight_resolved_externals: false,
plugins: HashMap::new(),
}
}
}
@ -627,6 +635,22 @@ impl Value {
"highlight_resolved_externals" => {
process_bool_config(value, &mut errors, &mut config.highlight_resolved_externals);
}
"plugins" => {
if let Ok(map) = create_map(value) {
config.plugins = map;
} else {
report_invalid_value("should be a record", span, &mut errors);
// Reconstruct
*value = Value::record(
config
.explore
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
span,
);
}
}
// Menus
"menus" => match create_menus(value) {
Ok(map) => config.menus = map,

View File

@ -712,6 +712,14 @@ impl EngineState {
self.config = conf;
}
/// Fetch the configuration for a plugin
///
/// The `plugin` must match the registered name of a plugin. For `register nu_plugin_example`
/// the plugin name to use will be `"example"`
pub fn get_plugin_config(&self, plugin: &str) -> Option<&Value> {
self.config.plugins.get(plugin)
}
pub fn get_var(&self, var_id: VarId) -> &Variable {
self.vars
.get(var_id)
@ -972,4 +980,27 @@ mod engine_state_tests {
assert_eq!(variables, vec![varname_with_sigil]);
Ok(())
}
#[test]
fn get_plugin_config() {
let mut engine_state = EngineState::new();
assert!(
engine_state.get_plugin_config("example").is_none(),
"Unexpected plugin configuration"
);
let mut plugins = HashMap::new();
plugins.insert("example".into(), Value::string("value", Span::test_data()));
let mut config = engine_state.get_config().clone();
config.plugins = plugins;
engine_state.set_config(config);
assert!(
engine_state.get_plugin_config("example").is_some(),
"Plugin configuration not found"
);
}
}