mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 05:52:34 +02:00
Move config to be an env var (#5230)
* Move config to be an env var * fix fmt and tests
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
use super::{Command, Stack};
|
||||
use crate::{
|
||||
ast::Block, AliasId, BlockId, DeclId, Example, Overlay, OverlayId, ShellError, Signature, Span,
|
||||
Type, VarId, Variable,
|
||||
ast::Block, AliasId, BlockId, Config, DeclId, Example, Overlay, OverlayId, ShellError,
|
||||
Signature, Span, Type, VarId, Variable,
|
||||
};
|
||||
use core::panic;
|
||||
use std::{
|
||||
@ -176,14 +176,14 @@ pub struct EngineState {
|
||||
pub scope: Vec<ScopeFrame>,
|
||||
pub ctrlc: Option<Arc<AtomicBool>>,
|
||||
pub env_vars: HashMap<String, Value>,
|
||||
pub config: Config,
|
||||
#[cfg(feature = "plugin")]
|
||||
pub plugin_signatures: Option<PathBuf>,
|
||||
}
|
||||
|
||||
pub const NU_VARIABLE_ID: usize = 0;
|
||||
pub const IN_VARIABLE_ID: usize = 1;
|
||||
pub const CONFIG_VARIABLE_ID: usize = 2;
|
||||
pub const ENV_VARIABLE_ID: usize = 3;
|
||||
pub const ENV_VARIABLE_ID: usize = 2;
|
||||
// NOTE: If you add more to this list, make sure to update the > checks based on the last in the list
|
||||
|
||||
impl EngineState {
|
||||
@ -205,6 +205,7 @@ impl EngineState {
|
||||
scope: vec![ScopeFrame::new()],
|
||||
ctrlc: None,
|
||||
env_vars: HashMap::new(),
|
||||
config: Config::default(),
|
||||
#[cfg(feature = "plugin")]
|
||||
plugin_signatures: None,
|
||||
}
|
||||
@ -263,6 +264,10 @@ impl EngineState {
|
||||
if let Some(stack) = stack {
|
||||
for mut env_scope in stack.env_vars.drain(..) {
|
||||
for (k, v) in env_scope.drain() {
|
||||
if k == "config" {
|
||||
self.config = v.clone().into_config().unwrap_or_default();
|
||||
}
|
||||
|
||||
self.env_vars.insert(k, v);
|
||||
}
|
||||
}
|
||||
@ -474,6 +479,10 @@ impl EngineState {
|
||||
panic!("internal error: span missing in file contents cache")
|
||||
}
|
||||
|
||||
pub fn get_config(&self) -> &Config {
|
||||
&self.config
|
||||
}
|
||||
|
||||
pub fn get_var(&self, var_id: VarId) -> &Variable {
|
||||
self.vars
|
||||
.get(var_id)
|
||||
@ -1233,6 +1242,10 @@ impl<'a> StateWorkingSet<'a> {
|
||||
self.permanent_state.env_vars.get(name)
|
||||
}
|
||||
|
||||
pub fn get_config(&self) -> &Config {
|
||||
&self.permanent_state.config
|
||||
}
|
||||
|
||||
pub fn list_env(&self) -> Vec<String> {
|
||||
let mut env_vars = vec![];
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::engine::EngineState;
|
||||
use crate::{Config, ShellError, Span, Value, VarId, CONFIG_VARIABLE_ID};
|
||||
use crate::{ShellError, Span, Value, VarId};
|
||||
|
||||
/// A runtime value stack used during evaluation
|
||||
///
|
||||
@ -97,11 +97,6 @@ impl Stack {
|
||||
output.env_vars = self.env_vars.clone();
|
||||
output.env_vars.push(HashMap::new());
|
||||
|
||||
let config = self
|
||||
.get_var(CONFIG_VARIABLE_ID, Span::new(0, 0))
|
||||
.expect("internal error: config is missing");
|
||||
output.vars.insert(CONFIG_VARIABLE_ID, config);
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
@ -122,11 +117,6 @@ impl Stack {
|
||||
output.env_vars = self.env_vars.clone();
|
||||
output.env_vars.push(HashMap::new());
|
||||
|
||||
let config = self
|
||||
.get_var(CONFIG_VARIABLE_ID, fake_span)
|
||||
.expect("internal error: config is missing");
|
||||
output.vars.insert(CONFIG_VARIABLE_ID, config);
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
@ -212,27 +202,27 @@ impl Stack {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_config(&self) -> Result<Config, ShellError> {
|
||||
let config = self.get_var(CONFIG_VARIABLE_ID, Span::new(0, 0));
|
||||
// pub fn get_config(&self) -> Result<Config, ShellError> {
|
||||
// let config = self.get_var(CONFIG_VARIABLE_ID, Span::new(0, 0));
|
||||
|
||||
match config {
|
||||
Ok(config) => config.into_config(),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
// match config {
|
||||
// Ok(config) => config.into_config(),
|
||||
// Err(e) => Err(e),
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn update_config(&mut self, name: &str, value: Value) {
|
||||
if let Some(Value::Record { cols, vals, .. }) = self.vars.get_mut(&CONFIG_VARIABLE_ID) {
|
||||
for col_val in cols.iter().zip(vals.iter_mut()) {
|
||||
if col_val.0 == name {
|
||||
*col_val.1 = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
cols.push(name.to_string());
|
||||
vals.push(value);
|
||||
}
|
||||
}
|
||||
// pub fn update_config(&mut self, name: &str, value: Value) {
|
||||
// if let Some(Value::Record { cols, vals, .. }) = self.vars.get_mut(&CONFIG_VARIABLE_ID) {
|
||||
// for col_val in cols.iter().zip(vals.iter_mut()) {
|
||||
// if col_val.0 == name {
|
||||
// *col_val.1 = value;
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// cols.push(name.to_string());
|
||||
// vals.push(value);
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn print_stack(&self) {
|
||||
println!("vars:");
|
||||
|
@ -15,7 +15,7 @@ mod value;
|
||||
mod variable;
|
||||
|
||||
pub use config::*;
|
||||
pub use engine::{CONFIG_VARIABLE_ID, ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID};
|
||||
pub use engine::{ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID};
|
||||
pub use example::*;
|
||||
pub use exportable::*;
|
||||
pub use id::*;
|
||||
|
Reference in New Issue
Block a user