forked from extern/nushell
Refactor/config commands (#3265)
* Use ctx.configs in all config commands * Remove all setting/accessing of vars.("config-path") * Add tests * Add comment * Reload cfg on remove * Hypocratic ws change * Use history_path in hist_or_default * Make clippy happy * Fix rebase stuff * Fix clippy lint
This commit is contained in:
@ -227,7 +227,7 @@ fn evaluate_literal(literal: &hir::Literal, span: Span) -> Value {
|
||||
|
||||
fn evaluate_reference(name: &str, ctx: &EvaluationContext, tag: Tag) -> Result<Value, ShellError> {
|
||||
match name {
|
||||
"$nu" => crate::evaluate::variables::nu(&ctx.scope, tag),
|
||||
"$nu" => crate::evaluate::variables::nu(&ctx.scope, tag, ctx),
|
||||
|
||||
"$scope" => crate::evaluate::variables::scope(&ctx.scope.get_aliases(), tag),
|
||||
|
||||
|
@ -1,11 +1,15 @@
|
||||
use crate::evaluate::scope::Scope;
|
||||
use crate::{evaluate::scope::Scope, EvaluationContext};
|
||||
use indexmap::IndexMap;
|
||||
use nu_data::config::path::history_path;
|
||||
use nu_data::config::path::{default_history_path, history_path};
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Primitive, TaggedDictBuilder, UntaggedValue, Value};
|
||||
use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
|
||||
use nu_source::{Spanned, Tag};
|
||||
|
||||
pub fn nu(scope: &Scope, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
pub fn nu(
|
||||
scope: &Scope,
|
||||
tag: impl Into<Tag>,
|
||||
ctx: &EvaluationContext,
|
||||
) -> Result<Value, ShellError> {
|
||||
let env = &scope.get_env_vars();
|
||||
let tag = tag.into();
|
||||
|
||||
@ -17,18 +21,33 @@ pub fn nu(scope: &Scope, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
dict.insert_untagged(v.0, UntaggedValue::string(v.1));
|
||||
}
|
||||
}
|
||||
|
||||
nu_dict.insert_value("env", dict.into_value());
|
||||
|
||||
let config_file = match scope.get_var("config-path") {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::FilePath(path)),
|
||||
..
|
||||
}) => Some(path),
|
||||
_ => None,
|
||||
};
|
||||
nu_dict.insert_value(
|
||||
"history-path",
|
||||
UntaggedValue::filepath(default_history_path()).into_value(&tag),
|
||||
);
|
||||
|
||||
let config = nu_data::config::read(&tag, &config_file)?;
|
||||
nu_dict.insert_value("config", UntaggedValue::row(config).into_value(&tag));
|
||||
if let Some(global_cfg) = &ctx.configs.lock().global_config {
|
||||
nu_dict.insert_value(
|
||||
"config",
|
||||
UntaggedValue::row(global_cfg.vars.clone()).into_value(&tag),
|
||||
);
|
||||
|
||||
nu_dict.insert_value(
|
||||
"config-path",
|
||||
UntaggedValue::filepath(global_cfg.file_path.clone()).into_value(&tag),
|
||||
);
|
||||
|
||||
// overwrite hist-path if present
|
||||
if let Some(hist_path) = history_path(global_cfg) {
|
||||
nu_dict.insert_value(
|
||||
"history-path",
|
||||
UntaggedValue::filepath(hist_path).into_value(&tag),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mut table = vec![];
|
||||
for v in env.iter() {
|
||||
@ -50,17 +69,6 @@ pub fn nu(scope: &Scope, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
let temp = std::env::temp_dir();
|
||||
nu_dict.insert_value("temp-dir", UntaggedValue::filepath(temp).into_value(&tag));
|
||||
|
||||
let config = if let Some(path) = config_file {
|
||||
path
|
||||
} else {
|
||||
nu_data::config::default_path()?
|
||||
};
|
||||
|
||||
nu_dict.insert_value(
|
||||
"config-path",
|
||||
UntaggedValue::filepath(config).into_value(&tag),
|
||||
);
|
||||
|
||||
#[cfg(feature = "rustyline-support")]
|
||||
{
|
||||
let keybinding_path = nu_data::keybinding::keybinding_path()?;
|
||||
@ -70,13 +78,6 @@ pub fn nu(scope: &Scope, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
);
|
||||
}
|
||||
|
||||
let config: Box<dyn nu_data::config::Conf> = Box::new(nu_data::config::NuConfig::new());
|
||||
let history = history_path(&config);
|
||||
nu_dict.insert_value(
|
||||
"history-path",
|
||||
UntaggedValue::filepath(history).into_value(&tag),
|
||||
);
|
||||
|
||||
Ok(nu_dict.into_value())
|
||||
}
|
||||
|
||||
|
@ -161,7 +161,7 @@ impl EvaluationContext {
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let tag = config::cfg_path_to_scope_tag(cfg_path);
|
||||
let tag = config::cfg_path_to_scope_tag(cfg_path.get_path());
|
||||
|
||||
self.scope.enter_scope_with_tag(tag);
|
||||
self.scope.add_env(cfg.env_map());
|
||||
@ -188,30 +188,8 @@ impl EvaluationContext {
|
||||
/// If an error occurs while reloading the config:
|
||||
/// The config is not reloaded
|
||||
/// The error is returned
|
||||
pub fn reload_config(&self, cfg_path: &ConfigPath) -> Result<(), ShellError> {
|
||||
trace!("Reloading cfg {:?}", cfg_path);
|
||||
|
||||
let mut configs = self.configs.lock();
|
||||
let cfg = match cfg_path {
|
||||
ConfigPath::Global(path) => {
|
||||
configs.global_config.iter_mut().find(|cfg| &cfg.file_path == path).ok_or_else(||
|
||||
ShellError::labeled_error(
|
||||
&format!("Error reloading global config with path of {}. No such global config present.", path.display()),
|
||||
"Config path error",
|
||||
Span::unknown(),
|
||||
)
|
||||
)?
|
||||
}
|
||||
ConfigPath::Local(path) => {
|
||||
configs.local_configs.iter_mut().find(|cfg| &cfg.file_path == path).ok_or_else(||
|
||||
ShellError::labeled_error(
|
||||
&format!("Error reloading local config with path of {}. No such local config present.", path.display()),
|
||||
"Config path error",
|
||||
Span::unknown(),
|
||||
)
|
||||
)?
|
||||
}
|
||||
};
|
||||
pub fn reload_config(&self, cfg: &mut NuConfig) -> Result<(), ShellError> {
|
||||
trace!("Reloading cfg {:?}", cfg.file_path);
|
||||
|
||||
cfg.reload();
|
||||
|
||||
@ -244,7 +222,7 @@ impl EvaluationContext {
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let tag = config::cfg_path_to_scope_tag(cfg_path);
|
||||
let tag = config::cfg_path_to_scope_tag(&cfg.file_path);
|
||||
let mut frame = ScopeFrame::with_tag(tag.clone());
|
||||
|
||||
frame.env = cfg.env_map();
|
||||
@ -265,7 +243,7 @@ impl EvaluationContext {
|
||||
pub fn unload_config(&self, cfg_path: &ConfigPath) {
|
||||
trace!("UnLoading cfg {:?}", cfg_path);
|
||||
|
||||
let tag = config::cfg_path_to_scope_tag(cfg_path);
|
||||
let tag = config::cfg_path_to_scope_tag(cfg_path.get_path());
|
||||
|
||||
//Run exitscripts with scope frame and cfg still applied
|
||||
if let Some(scripts) = self.scope.get_exitscripts_of_frame_with_tag(&tag) {
|
||||
|
Reference in New Issue
Block a user