nushell/crates/nu-engine/src/config_holder.rs
Leonhard Kipp c42b588782
Refactor nu-cli/env* (#3041)
* Revert "History, more test coverage improvements, and refactorings. (#3217)"

This reverts commit 8fc8fc89aa.

* Add tests

* Refactor .nu-env

* Change logic of Config write to logic of read()

* Fix reload always appends to old vars

* Fix reload always takes last_modified of global config

* Add reload_config in evaluation context

* Reload config after writing to it in cfg set / cfg set_into

* Add --no-history to cli options

* Use --no-history in tests

* Add comment about maybe_print_errors

* Get ctrl_exit var from context.global_config

* Use context.global_config in command "config"

* Add Readme in engine how env vars are now handled

* Update docs from autoenv command

* Move history_path from engine to nu_data

* Move load history out of if

* No let before return

* Add import for indexmap
2021-03-31 18:52:34 +13:00

53 lines
1.3 KiB
Rust

use std::path::Path;
use nu_data::config::NuConfig;
use nu_protocol::ConfigPath;
/// ConfigHolder holds information which configs have been loaded.
#[derive(Clone)]
pub struct ConfigHolder {
pub global_config: Option<NuConfig>,
pub local_configs: Vec<NuConfig>,
}
impl Default for ConfigHolder {
fn default() -> Self {
Self::new()
}
}
impl ConfigHolder {
pub fn new() -> ConfigHolder {
ConfigHolder {
global_config: None,
local_configs: vec![],
}
}
pub fn add_local_cfg(&mut self, cfg: NuConfig) {
self.local_configs.push(cfg);
}
pub fn set_global_cfg(&mut self, cfg: NuConfig) {
self.global_config = Some(cfg);
}
pub fn remove_cfg(&mut self, cfg_path: &ConfigPath) {
match cfg_path {
ConfigPath::Global(_) => self.global_config = None,
ConfigPath::Local(p) => self.remove_local_cfg(p),
}
}
fn remove_local_cfg<P: AsRef<Path>>(&mut self, cfg_path: P) {
// Remove the first loaded local config with specified cfg_path
if let Some(index) = self
.local_configs
.iter()
.rev()
.position(|cfg| cfg.file_path == cfg_path.as_ref())
{
self.local_configs.remove(index);
}
}
}