mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
Allow plugins to set environment variables in their caller's scope (#12204)
# Description Adds the `AddEnvVar` plugin call, which allows plugins to set environment variables in the caller's scope. This is the first engine call that mutates the caller's stack, and opens the door to more operations like this if needed. This also comes with an extra benefit: in doing this, I needed to refactor how context was handled, and I was able to avoid cloning `EngineInterface` / `Stack` / `Call` in most cases that plugin calls are used. They now only need to be cloned if the plugin call returns a stream. The performance increase is welcome (5.5x faster on `inc`!): ```nushell # Before > timeit { 1..100 | each { |i| $"2.0.($i)" | inc -p } } 405ms 941µs 952ns # After > timeit { 1..100 | each { |i| $"2.0.($i)" | inc -p } } 73ms 68µs 749ns ``` # User-Facing Changes - New engine call: `add_env_var()` - Performance enhancement for plugin calls # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting - [x] Document env manipulation in plugins guide - [x] Document `AddEnvVar` in plugin protocol
This commit is contained in:
@ -19,6 +19,12 @@ impl SimplePluginCommand for NuExampleEnv {
|
||||
"The name of the environment variable to get",
|
||||
)
|
||||
.switch("cwd", "Get current working directory instead", None)
|
||||
.named(
|
||||
"set",
|
||||
SyntaxShape::Any,
|
||||
"Set an environment variable to the value",
|
||||
None,
|
||||
)
|
||||
.search_terms(vec!["example".into(), "env".into()])
|
||||
.input_output_type(Type::Nothing, Type::Any)
|
||||
}
|
||||
@ -31,8 +37,22 @@ impl SimplePluginCommand for NuExampleEnv {
|
||||
_input: &Value,
|
||||
) -> Result<Value, LabeledError> {
|
||||
if call.has_flag("cwd")? {
|
||||
// Get working directory
|
||||
Ok(Value::string(engine.get_current_dir()?, call.head))
|
||||
match call.get_flag_value("set") {
|
||||
None => {
|
||||
// Get working directory
|
||||
Ok(Value::string(engine.get_current_dir()?, call.head))
|
||||
}
|
||||
Some(value) => Err(LabeledError {
|
||||
label: "Invalid arguments".into(),
|
||||
msg: "--cwd can't be used with --set".into(),
|
||||
span: Some(value.span()),
|
||||
}),
|
||||
}
|
||||
} else if let Some(value) = call.get_flag_value("set") {
|
||||
// Set single env var
|
||||
let name = call.req::<String>(0)?;
|
||||
engine.add_env_var(name, value)?;
|
||||
Ok(Value::nothing(call.head))
|
||||
} else if let Some(name) = call.opt::<String>(0)? {
|
||||
// Get single env var
|
||||
Ok(engine
|
||||
|
Reference in New Issue
Block a user