mirror of
https://github.com/nushell/nushell
synced 2024-12-27 21:43:09 +00:00
allow for easy reset of config files with a single command (#6041)
* allow for easy config reset with a single command * add slightly better help, rebase * add option to make no backups, make all backups unique through including UNIX Epoch Time in the filename * time is now formatted in rfc3339 * time is now formatted in a window-friendly format
This commit is contained in:
parent
47c1f475bf
commit
89b374cb16
4 changed files with 116 additions and 0 deletions
|
@ -343,6 +343,7 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
|
|||
WithEnv,
|
||||
ConfigNu,
|
||||
ConfigEnv,
|
||||
ConfigReset,
|
||||
ConfigMeta,
|
||||
};
|
||||
|
||||
|
|
112
crates/nu-command/src/env/config/config_reset.rs
vendored
Normal file
112
crates/nu-command/src/env/config/config_reset.rs
vendored
Normal file
|
@ -0,0 +1,112 @@
|
|||
use chrono::Local;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, Example, PipelineData, ShellError, Signature,
|
||||
};
|
||||
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'))
|
||||
.category(Category::Env)
|
||||
}
|
||||
|
||||
fn usage(&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<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let only_nu = call.has_flag("nu");
|
||||
let only_env = call.has_flag("env");
|
||||
let no_backup = call.has_flag("without-backup");
|
||||
let span = call.head;
|
||||
let mut config_path = match nu_path::config_dir() {
|
||||
Some(path) => path,
|
||||
None => {
|
||||
return Err(ShellError::GenericError(
|
||||
"Could not find config path".to_string(),
|
||||
"Could not find config path".to_string(),
|
||||
None,
|
||||
None,
|
||||
Vec::new(),
|
||||
));
|
||||
}
|
||||
};
|
||||
config_path.push("nushell");
|
||||
if !only_env {
|
||||
let mut nu_config = config_path.clone();
|
||||
nu_config.push("config.nu");
|
||||
let config_file = include_str!("../../../../../docs/sample_config/default_config.nu");
|
||||
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(
|
||||
"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(
|
||||
"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 = include_str!("../../../../../docs/sample_config/default_env.nu");
|
||||
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(
|
||||
"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(
|
||||
"env.nu could not be written to".into(),
|
||||
span,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(PipelineData::new(span))
|
||||
}
|
||||
}
|
2
crates/nu-command/src/env/config/mod.rs
vendored
2
crates/nu-command/src/env/config/mod.rs
vendored
|
@ -1,7 +1,9 @@
|
|||
mod config_;
|
||||
mod config_env;
|
||||
mod config_nu;
|
||||
mod config_reset;
|
||||
mod utils;
|
||||
pub use config_::ConfigMeta;
|
||||
pub use config_env::ConfigEnv;
|
||||
pub use config_nu::ConfigNu;
|
||||
pub use config_reset::ConfigReset;
|
||||
|
|
1
crates/nu-command/src/env/mod.rs
vendored
1
crates/nu-command/src/env/mod.rs
vendored
|
@ -7,6 +7,7 @@ mod with_env;
|
|||
pub use config::ConfigEnv;
|
||||
pub use config::ConfigMeta;
|
||||
pub use config::ConfigNu;
|
||||
pub use config::ConfigReset;
|
||||
pub use env_command::Env;
|
||||
pub use let_env::LetEnv;
|
||||
pub use load_env::LoadEnv;
|
||||
|
|
Loading…
Reference in a new issue