mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 16:54:58 +02:00
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:
@ -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()
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user