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

@ -45,13 +45,9 @@ impl Command for ConfigReset {
let only_env = call.has_flag(engine_state, stack, "env")?;
let no_backup = call.has_flag(engine_state, stack, "without-backup")?;
let span = call.head;
let mut config_path = match nu_path::config_dir() {
Some(path) => path,
None => {
return Err(ShellError::ConfigDirNotFound { span: None });
}
let Some(config_path) = nu_path::nu_config_dir() else {
return Err(ShellError::ConfigDirNotFound { span: None });
};
config_path.push("nushell");
if !only_env {
let mut nu_config = config_path.clone();
nu_config.push("config.nu");