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:
Leonhard Kipp
2021-04-09 08:03:12 +02:00
committed by GitHub
parent 111ad868a7
commit ac070ae942
19 changed files with 357 additions and 254 deletions

View File

@ -16,8 +16,8 @@ use indexmap::IndexMap;
use log::trace;
use nu_errors::{CoerceInto, ShellError};
use nu_protocol::{
ConfigPath, Dictionary, Primitive, ShellTypeName, TaggedDictBuilder, UnspannedPathMember,
UntaggedValue, Value,
Dictionary, Primitive, ShellTypeName, TaggedDictBuilder, UnspannedPathMember, UntaggedValue,
Value,
};
use nu_source::{SpannedItem, Tag, TaggedItem};
use std::fs::{self, OpenOptions};
@ -328,6 +328,6 @@ fn touch(path: &Path) -> io::Result<()> {
}
}
pub fn cfg_path_to_scope_tag(cfg_path: &ConfigPath) -> String {
cfg_path.get_path().to_string_lossy().to_string()
pub fn cfg_path_to_scope_tag(cfg_path: &Path) -> String {
cfg_path.to_string_lossy().to_string()
}

View File

@ -5,6 +5,8 @@ use nu_protocol::Value;
use nu_source::Tag;
use std::{fmt::Debug, path::PathBuf};
use super::write;
#[derive(Debug, Clone, Default)]
pub struct NuConfig {
pub vars: IndexMap<String, Value>,
@ -63,6 +65,11 @@ impl NuConfig {
})
}
/// Writes self.values under self.file_path
pub fn write(&self) -> Result<(), ShellError> {
write(&self.vars, &Some(self.file_path.clone()))
}
pub fn new() -> NuConfig {
let vars = if let Ok(variables) = read(Tag::unknown(), &None) {
variables

View File

@ -1,6 +1,7 @@
use crate::config::Conf;
use std::path::PathBuf;
use super::NuConfig;
const DEFAULT_LOCATION: &str = "history.txt";
pub fn default_history_path() -> PathBuf {
@ -12,14 +13,18 @@ pub fn default_history_path() -> PathBuf {
.unwrap_or_else(|_| PathBuf::from(DEFAULT_LOCATION))
}
pub fn history_path(config: &dyn Conf) -> PathBuf {
let default_history_path = default_history_path();
config.var("history-path").map_or(
default_history_path.clone(),
|custom_path| match custom_path.as_string() {
Ok(path) => PathBuf::from(path),
Err(_) => default_history_path,
},
)
/// Get history path of config, if present
pub fn history_path(config: &NuConfig) -> Option<PathBuf> {
config
.var("history-path")
.map(|custom_path| match custom_path.as_string() {
Ok(path) => Some(PathBuf::from(path)),
Err(_) => None,
})
.flatten()
}
/// Get history path in config or default
pub fn history_path_or_default(config: &NuConfig) -> PathBuf {
history_path(config).unwrap_or_else(default_history_path)
}