mirror of
https://github.com/nushell/nushell
synced 2024-12-29 06:23:11 +00:00
run ENV_CONVERSIONS when it's assigned to, immediately
This commit is contained in:
parent
bbd8b2701a
commit
ce89b8414b
2 changed files with 47 additions and 4 deletions
|
@ -3,7 +3,7 @@ use nu_path::canonicalize_with;
|
|||
use nu_protocol::{
|
||||
ast::Expr,
|
||||
engine::{Call, EngineState, Stack, StateWorkingSet},
|
||||
ShellError, Span, Value, VarId,
|
||||
ShellError, Span, Type, Value, VarId,
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
|
@ -11,7 +11,7 @@ use std::{
|
|||
sync::Arc,
|
||||
};
|
||||
|
||||
const ENV_CONVERSIONS: &str = "ENV_CONVERSIONS";
|
||||
pub const ENV_CONVERSIONS: &str = "ENV_CONVERSIONS";
|
||||
|
||||
enum ConversionError {
|
||||
ShellError(ShellError),
|
||||
|
@ -24,6 +24,40 @@ impl From<ShellError> for ConversionError {
|
|||
}
|
||||
}
|
||||
|
||||
/// Translate environment variables from Strings to Values.
|
||||
pub fn convert_env_vars(
|
||||
stack: &mut Stack,
|
||||
engine_state: &EngineState,
|
||||
conversions: &Value,
|
||||
) -> Result<(), ShellError> {
|
||||
let conversions = conversions.as_record()?;
|
||||
for (key, conversion) in conversions.into_iter() {
|
||||
if let Some(val) = stack.get_env_var_insensitive(engine_state, key) {
|
||||
match val.get_type() {
|
||||
Type::String => {}
|
||||
_ => continue,
|
||||
}
|
||||
|
||||
let conversion = conversion
|
||||
.as_record()?
|
||||
.get("from_string")
|
||||
.ok_or(ShellError::MissingRequiredColumn {
|
||||
column: "from_string",
|
||||
span: conversion.span(),
|
||||
})?
|
||||
.as_closure()?;
|
||||
|
||||
let new_val = ClosureEvalOnce::new(engine_state, stack, conversion.clone())
|
||||
.debug(false)
|
||||
.run_with_value(val.clone())?
|
||||
.into_value(val.span())?;
|
||||
|
||||
stack.add_env_var(key.clone(), new_val);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Translate environment variables from Strings to Values. Requires config to be already set up in
|
||||
/// case the user defined custom env conversions in config.nu.
|
||||
///
|
||||
|
|
|
@ -14,7 +14,10 @@ use nu_protocol::{
|
|||
};
|
||||
use nu_utils::IgnoreCaseExt;
|
||||
|
||||
use crate::{eval::is_automatic_env_var, eval_block_with_early_return, redirect_env};
|
||||
use crate::{
|
||||
convert_env_vars, eval::is_automatic_env_var, eval_block_with_early_return, redirect_env,
|
||||
ENV_CONVERSIONS,
|
||||
};
|
||||
|
||||
/// Evaluate the compiled representation of a [`Block`].
|
||||
pub fn eval_ir_block<D: DebugContext>(
|
||||
|
@ -384,10 +387,16 @@ fn eval_instruction<D: DebugContext>(
|
|||
|
||||
if !is_automatic_env_var(&key) {
|
||||
let is_config = key == "config";
|
||||
ctx.stack.add_env_var(key.into_owned(), value);
|
||||
let update_conversions = key == ENV_CONVERSIONS;
|
||||
|
||||
ctx.stack.add_env_var(key.into_owned(), value.clone());
|
||||
|
||||
if is_config {
|
||||
ctx.stack.update_config(ctx.engine_state)?;
|
||||
}
|
||||
if update_conversions {
|
||||
convert_env_vars(ctx.stack, ctx.engine_state, &value)?;
|
||||
}
|
||||
Ok(Continue)
|
||||
} else {
|
||||
Err(ShellError::AutomaticEnvVarSetManually {
|
||||
|
|
Loading…
Reference in a new issue