mirror of
https://github.com/nushell/nushell.git
synced 2025-07-15 13:55:26 +02:00
# 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
103 lines
3.7 KiB
Rust
103 lines
3.7 KiB
Rust
use chrono::Local;
|
|
use nu_engine::command_prelude::*;
|
|
|
|
use nu_utils::{get_default_config, get_default_env};
|
|
use std::io::Write;
|
|
|
|
#[derive(Clone)]
|
|
pub struct ConfigReset;
|
|
|
|
impl Command for ConfigReset {
|
|
fn name(&self) -> &str {
|
|
"config reset"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.switch("nu", "reset only nu config, config.nu", Some('n'))
|
|
.switch("env", "reset only env config, env.nu", Some('e'))
|
|
.switch("without-backup", "do not make a backup", Some('w'))
|
|
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
|
.allow_variants_without_examples(true)
|
|
.category(Category::Env)
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Reset nushell environment configurations to default, and saves old config files in the config location as oldconfig.nu and oldenv.nu."
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "reset nushell configuration files",
|
|
example: "config reset",
|
|
result: None,
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
_input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let only_nu = call.has_flag(engine_state, stack, "nu")?;
|
|
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 Some(config_path) = nu_path::nu_config_dir() else {
|
|
return Err(ShellError::ConfigDirNotFound { span: None });
|
|
};
|
|
if !only_env {
|
|
let mut nu_config = config_path.clone();
|
|
nu_config.push("config.nu");
|
|
let config_file = get_default_config();
|
|
if !no_backup {
|
|
let mut backup_path = config_path.clone();
|
|
backup_path.push(format!(
|
|
"oldconfig-{}.nu",
|
|
Local::now().format("%F-%H-%M-%S"),
|
|
));
|
|
if std::fs::rename(nu_config.clone(), backup_path).is_err() {
|
|
return Err(ShellError::FileNotFoundCustom {
|
|
msg: "config.nu could not be backed up".into(),
|
|
span,
|
|
});
|
|
}
|
|
}
|
|
if let Ok(mut file) = std::fs::File::create(nu_config) {
|
|
if writeln!(&mut file, "{config_file}").is_err() {
|
|
return Err(ShellError::FileNotFoundCustom {
|
|
msg: "config.nu could not be written to".into(),
|
|
span,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
if !only_nu {
|
|
let mut env_config = config_path.clone();
|
|
env_config.push("env.nu");
|
|
let config_file = get_default_env();
|
|
if !no_backup {
|
|
let mut backup_path = config_path.clone();
|
|
backup_path.push(format!("oldenv-{}.nu", Local::now().format("%F-%H-%M-%S"),));
|
|
if std::fs::rename(env_config.clone(), backup_path).is_err() {
|
|
return Err(ShellError::FileNotFoundCustom {
|
|
msg: "env.nu could not be backed up".into(),
|
|
span,
|
|
});
|
|
}
|
|
}
|
|
if let Ok(mut file) = std::fs::File::create(env_config) {
|
|
if writeln!(&mut file, "{config_file}").is_err() {
|
|
return Err(ShellError::FileNotFoundCustom {
|
|
msg: "env.nu could not be written to".into(),
|
|
span,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
Ok(PipelineData::empty())
|
|
}
|
|
}
|