From 01386f4d58ed1b83ffd9c9b3a8daeedf54d20970 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Mon, 1 Aug 2022 09:44:33 +0800 Subject: [PATCH] adds a `config reset` command (#6149) * moves config files to nu_utils * fmt * fix dockerfile * fix docs --- crates/nu-command/src/default_context.rs | 1 + .../nu-command/src/env/config/config_reset.rs | 113 ++++++++++++++++++ crates/nu-command/src/env/config/mod.rs | 2 + crates/nu-command/src/env/mod.rs | 1 + crates/nu-utils/src/lib.rs | 3 +- .../src}/sample_config/default_config.nu | 0 .../src}/sample_config/default_env.nu | 0 .../src}/sample_config/sample_login.nu | 0 crates/nu-utils/src/utils.rs | 8 ++ docker/Dockerfile | 4 +- docs/README.md | 4 +- src/config_files.rs | 7 +- 12 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 crates/nu-command/src/env/config/config_reset.rs rename {docs => crates/nu-utils/src}/sample_config/default_config.nu (100%) rename {docs => crates/nu-utils/src}/sample_config/default_env.nu (100%) rename {docs => crates/nu-utils/src}/sample_config/sample_login.nu (100%) diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index c5d2a00c4..8f1929cb7 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -353,6 +353,7 @@ pub fn create_default_context() -> EngineState { ConfigNu, ConfigEnv, ConfigMeta, + ConfigReset, }; // Math diff --git a/crates/nu-command/src/env/config/config_reset.rs b/crates/nu-command/src/env/config/config_reset.rs new file mode 100644 index 000000000..7b29031f8 --- /dev/null +++ b/crates/nu-command/src/env/config/config_reset.rs @@ -0,0 +1,113 @@ +use chrono::Local; +use nu_protocol::{ + ast::Call, + engine::{Command, EngineState, Stack}, + Category, Example, PipelineData, ShellError, Signature, +}; +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')) + .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 { + 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 { + 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 = 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( + "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 = 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( + "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)) + } +} diff --git a/crates/nu-command/src/env/config/mod.rs b/crates/nu-command/src/env/config/mod.rs index 5e70f6315..9cd93d249 100644 --- a/crates/nu-command/src/env/config/mod.rs +++ b/crates/nu-command/src/env/config/mod.rs @@ -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; diff --git a/crates/nu-command/src/env/mod.rs b/crates/nu-command/src/env/mod.rs index e0e1a1820..6bd891485 100644 --- a/crates/nu-command/src/env/mod.rs +++ b/crates/nu-command/src/env/mod.rs @@ -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; diff --git a/crates/nu-utils/src/lib.rs b/crates/nu-utils/src/lib.rs index f15481848..9199d0587 100644 --- a/crates/nu-utils/src/lib.rs +++ b/crates/nu-utils/src/lib.rs @@ -1,5 +1,6 @@ pub mod utils; pub use utils::{ - enable_vt_processing, get_ls_colors, stderr_write_all_and_flush, stdout_write_all_and_flush, + enable_vt_processing, get_default_config, get_default_env, get_ls_colors, + stderr_write_all_and_flush, stdout_write_all_and_flush, }; diff --git a/docs/sample_config/default_config.nu b/crates/nu-utils/src/sample_config/default_config.nu similarity index 100% rename from docs/sample_config/default_config.nu rename to crates/nu-utils/src/sample_config/default_config.nu diff --git a/docs/sample_config/default_env.nu b/crates/nu-utils/src/sample_config/default_env.nu similarity index 100% rename from docs/sample_config/default_env.nu rename to crates/nu-utils/src/sample_config/default_env.nu diff --git a/docs/sample_config/sample_login.nu b/crates/nu-utils/src/sample_config/sample_login.nu similarity index 100% rename from docs/sample_config/sample_login.nu rename to crates/nu-utils/src/sample_config/sample_login.nu diff --git a/crates/nu-utils/src/utils.rs b/crates/nu-utils/src/utils.rs index 44372050d..368822544 100644 --- a/crates/nu-utils/src/utils.rs +++ b/crates/nu-utils/src/utils.rs @@ -51,6 +51,14 @@ where ret } +pub fn get_default_env() -> &'static str { + include_str!("sample_config/default_env.nu") +} + +pub fn get_default_config() -> &'static str { + include_str!("sample_config/default_config.nu") +} + pub fn get_ls_colors(lscolors_env_string: Option) -> LsColors { match lscolors_env_string { Some(s) => LsColors::from_string(&s), diff --git a/docker/Dockerfile b/docker/Dockerfile index c6da69d89..66806c23c 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -10,8 +10,8 @@ LABEL maintainer=nushell RUN echo '/usr/bin/nu' >> /etc/shells \ && adduser -D -s /usr/bin/nu nushell \ && mkdir -p /home/nushell/.config/nushell/ \ - && wget -q https://raw.githubusercontent.com/nushell/nushell/main/docs/sample_config/default_config.nu -O /home/nushell/.config/nushell/config.nu \ - && wget -q https://raw.githubusercontent.com/nushell/nushell/main/docs/sample_config/default_env.nu -O /home/nushell/.config/nushell/env.nu \ + && wget -q https://raw.githubusercontent.com/nushell/nushell/main/crates/nu-utils/src/sample_config/default_config.nu -O /home/nushell/.config/nushell/config.nu \ + && wget -q https://raw.githubusercontent.com/nushell/nushell/main/crates/nu-utils/src/sample_config/default_env.nu -O /home/nushell/.config/nushell/env.nu \ && cd /tmp \ && wget -qO - https://api.github.com/repos/nushell/nushell/releases/latest \ |grep browser_download_url \ diff --git a/docs/README.md b/docs/README.md index 553eaa86e..9ff29d0aa 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,7 @@ # Documentation -This directory contains [default_config.nu](./sample_config/default_config.nu) -which is the configuration file one gets when they startup Nushell for the first time. +The default configurations can be found at [sample_config](../crates/nu-utils/src/sample_config) +which are the configuration files one gets when they startup Nushell for the first time. It sets all of the default configuration to run Nushell. From here one can then customize this file for their specific needs. diff --git a/src/config_files.rs b/src/config_files.rs index 955b021c5..789fda613 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -4,6 +4,7 @@ use nu_parser::ParseError; use nu_path::canonicalize_with; use nu_protocol::engine::{EngineState, Stack, StateWorkingSet}; use nu_protocol::{PipelineData, Span, Spanned}; +use nu_utils::{get_default_config, get_default_env}; use std::fs::File; use std::io::Write; @@ -65,9 +66,9 @@ pub(crate) fn read_config_file( .expect("Failed to read user input"); let config_file = if is_env_config { - include_str!("../docs/sample_config/default_env.nu") + get_default_env() } else { - include_str!("../docs/sample_config/default_config.nu") + get_default_config() }; match answer.to_lowercase().trim() { @@ -134,7 +135,7 @@ pub(crate) fn read_default_env_file( stack: &mut Stack, is_perf_true: bool, ) { - let config_file = include_str!("../docs/sample_config/default_env.nu"); + let config_file = get_default_env(); eval_source( engine_state, stack,