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)