Reduce duplication in history path construction (#13475)

# Description
Currently there is a bit of chaos regarding construction of history file
paths. Various pieces of code across a number of crates reimplement the
same/similar logic:
- There is `get_history_path`, but it requires a directory parameter (it
really just joins it with a file name).
- Some places use a const for the directory parameter, others use a
string literal - in all cases the value seems to be `"nushell"`.
- Some places assume the `"nushell"` value, other plumb it down from
close to the top of the call stack.
- Some places use a constant for history file names while others assume
it.

This PR tries to make it so that the history/config path format is
defined in a single places and so dependencies on it are easier to
follow:
- It removes `get_history_path` and adds a `file_path` method to
`HistoryConfig` instead (an extra motivation being, this is a convenient
place that can be used from all creates that need a history file path)
- Adds a `nu_config_dir` function that returns the nushell configuration
directory.
- Updates existing code to rely on the above, effectively removing
duplicate uses of `"nushell"` and `NUSHELL_FOLDER` and assumptions about
file names associated with different history formats

# User-Facing Changes
None
This commit is contained in:
Piotr Kufel
2024-10-11 05:51:50 -07:00
committed by GitHub
parent 9f714e62cb
commit bcb7ef48b6
12 changed files with 82 additions and 145 deletions

View File

@ -17,7 +17,6 @@ use std::{
sync::Arc,
};
pub(crate) const NUSHELL_FOLDER: &str = "nushell";
const CONFIG_FILE: &str = "config.nu";
const ENV_FILE: &str = "env.nu";
const LOGINSHELL_FILE: &str = "login.nu";
@ -48,9 +47,7 @@ pub(crate) fn read_config_file(
report_shell_error(engine_state, &e);
}
}
} else if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(NUSHELL_FOLDER);
} else if let Some(mut config_path) = nu_path::nu_config_dir() {
// Create config directory if it does not exist
if !config_path.exists() {
if let Err(err) = std::fs::create_dir_all(&config_path) {
@ -141,8 +138,7 @@ pub(crate) fn read_loginshell_file(engine_state: &mut EngineState, stack: &mut S
);
// read and execute loginshell file if exists
if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(NUSHELL_FOLDER);
if let Some(mut config_path) = nu_path::nu_config_dir() {
config_path.push(LOGINSHELL_FILE);
warn!("loginshell_file: {}", config_path.display());
@ -268,16 +264,11 @@ pub(crate) fn setup_config(
&config_file, &env_file, is_login_shell
);
let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(NUSHELL_FOLDER);
!config_path.exists()
} else {
false
};
let ask_to_create_config = nu_path::nu_config_dir().map_or(false, |p| !p.exists());
let result = catch_unwind(AssertUnwindSafe(|| {
#[cfg(feature = "plugin")]
read_plugin_file(engine_state, plugin_file, NUSHELL_FOLDER);
read_plugin_file(engine_state, plugin_file);
read_config_file(engine_state, stack, env_file, true, ask_to_create_config);
read_config_file(
@ -315,8 +306,7 @@ pub(crate) fn set_config_path(
);
let config_path = match config_file {
Some(s) => canonicalize_with(&s.item, cwd).ok(),
None => nu_path::config_dir().map(|mut p| {
p.push(NUSHELL_FOLDER);
None => nu_path::nu_config_dir().map(|p| {
let mut p = canonicalize_with(&p, cwd).unwrap_or(p.into());
p.push(default_config_name);
canonicalize_with(&p, cwd).unwrap_or(p)

View File

@ -98,14 +98,7 @@ fn main() -> Result<()> {
// Set default NU_LIB_DIRS and NU_PLUGIN_DIRS here before the env.nu is processed. If
// the env.nu file exists, these values will be overwritten, if it does not exist, or
// there is an error reading it, these values will be used.
let nushell_config_path = if let Some(mut path) = nu_path::config_dir() {
path.push("nushell");
path.into()
} else {
// Not really sure what to default this to if nu_path::config_dir() returns None
std::path::PathBuf::new()
};
let nushell_config_path: PathBuf = nu_path::nu_config_dir().map(Into::into).unwrap_or_default();
if let Ok(xdg_config_home) = std::env::var("XDG_CONFIG_HOME") {
if !xdg_config_home.is_empty() {
if nushell_config_path

View File

@ -1,6 +1,6 @@
use crate::{
command,
config_files::{self, setup_config, NUSHELL_FOLDER},
config_files::{self, setup_config},
};
use log::trace;
#[cfg(feature = "plugin")]
@ -21,16 +21,11 @@ pub(crate) fn run_commands(
entire_start_time: std::time::Instant,
) {
trace!("run_commands");
let mut stack = Stack::new();
let start_time = std::time::Instant::now();
let ask_to_create_config = nu_path::nu_config_dir().map_or(false, |p| !p.exists());
let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(NUSHELL_FOLDER);
!config_path.exists()
} else {
false
};
let mut stack = Stack::new();
if stack.has_env_var(engine_state, "NU_DISABLE_IR") {
stack.use_ir = false;
}
@ -42,7 +37,7 @@ pub(crate) fn run_commands(
// if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files
if parsed_nu_cli_args.no_config_file.is_none() {
#[cfg(feature = "plugin")]
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER);
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
perf!("read plugins", start_time, use_color);
@ -63,12 +58,7 @@ pub(crate) fn run_commands(
perf!("read env.nu", start_time, use_color);
let start_time = std::time::Instant::now();
let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(config_files::NUSHELL_FOLDER);
!config_path.exists()
} else {
false
};
let ask_to_create_config = nu_path::nu_config_dir().map_or(false, |p| !p.exists());
// If we have a config file parameter *OR* we have a login shell parameter, read the config file
if parsed_nu_cli_args.config_file.is_some() || parsed_nu_cli_args.login_shell.is_some() {
@ -140,14 +130,9 @@ pub(crate) fn run_file(
// if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files
if parsed_nu_cli_args.no_config_file.is_none() {
let start_time = std::time::Instant::now();
let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(config_files::NUSHELL_FOLDER);
!config_path.exists()
} else {
false
};
let ask_to_create_config = nu_path::nu_config_dir().map_or(false, |p| !p.exists());
#[cfg(feature = "plugin")]
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER);
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
perf!("read plugins", start_time, use_color);
let start_time = std::time::Instant::now();
@ -230,7 +215,6 @@ pub(crate) fn run_repl(
let ret_val = evaluate_repl(
engine_state,
stack,
NUSHELL_FOLDER,
parsed_nu_cli_args.execute,
parsed_nu_cli_args.no_std_lib,
entire_start_time,