adds a config reset command (#6149)

* moves config files to nu_utils

* fmt

* fix dockerfile

* fix docs
This commit is contained in:
pwygab 2022-08-01 09:44:33 +08:00 committed by GitHub
parent 1086fbe9b5
commit 01386f4d58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 135 additions and 8 deletions

View File

@ -353,6 +353,7 @@ pub fn create_default_context() -> EngineState {
ConfigNu, ConfigNu,
ConfigEnv, ConfigEnv,
ConfigMeta, ConfigMeta,
ConfigReset,
}; };
// Math // Math

View File

@ -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<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 = 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))
}
}

View File

@ -1,7 +1,9 @@
mod config_; mod config_;
mod config_env; mod config_env;
mod config_nu; mod config_nu;
mod config_reset;
mod utils; mod utils;
pub use config_::ConfigMeta; pub use config_::ConfigMeta;
pub use config_env::ConfigEnv; pub use config_env::ConfigEnv;
pub use config_nu::ConfigNu; pub use config_nu::ConfigNu;
pub use config_reset::ConfigReset;

View File

@ -7,6 +7,7 @@ mod with_env;
pub use config::ConfigEnv; pub use config::ConfigEnv;
pub use config::ConfigMeta; pub use config::ConfigMeta;
pub use config::ConfigNu; pub use config::ConfigNu;
pub use config::ConfigReset;
pub use env_command::Env; pub use env_command::Env;
pub use let_env::LetEnv; pub use let_env::LetEnv;
pub use load_env::LoadEnv; pub use load_env::LoadEnv;

View File

@ -1,5 +1,6 @@
pub mod utils; pub mod utils;
pub use 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,
}; };

View File

@ -51,6 +51,14 @@ where
ret 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<String>) -> LsColors { pub fn get_ls_colors(lscolors_env_string: Option<String>) -> LsColors {
match lscolors_env_string { match lscolors_env_string {
Some(s) => LsColors::from_string(&s), Some(s) => LsColors::from_string(&s),

View File

@ -10,8 +10,8 @@ LABEL maintainer=nushell
RUN echo '/usr/bin/nu' >> /etc/shells \ RUN echo '/usr/bin/nu' >> /etc/shells \
&& adduser -D -s /usr/bin/nu nushell \ && adduser -D -s /usr/bin/nu nushell \
&& mkdir -p /home/nushell/.config/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/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/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_env.nu -O /home/nushell/.config/nushell/env.nu \
&& cd /tmp \ && cd /tmp \
&& wget -qO - https://api.github.com/repos/nushell/nushell/releases/latest \ && wget -qO - https://api.github.com/repos/nushell/nushell/releases/latest \
|grep browser_download_url \ |grep browser_download_url \

View File

@ -1,7 +1,7 @@
# Documentation # Documentation
This directory contains [default_config.nu](./sample_config/default_config.nu) The default configurations can be found at [sample_config](../crates/nu-utils/src/sample_config)
which is the configuration file one gets when they startup Nushell for the first time. 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 It sets all of the default configuration to run Nushell. From here one can
then customize this file for their specific needs. then customize this file for their specific needs.

View File

@ -4,6 +4,7 @@ use nu_parser::ParseError;
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet}; use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{PipelineData, Span, Spanned}; use nu_protocol::{PipelineData, Span, Spanned};
use nu_utils::{get_default_config, get_default_env};
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
@ -65,9 +66,9 @@ pub(crate) fn read_config_file(
.expect("Failed to read user input"); .expect("Failed to read user input");
let config_file = if is_env_config { let config_file = if is_env_config {
include_str!("../docs/sample_config/default_env.nu") get_default_env()
} else { } else {
include_str!("../docs/sample_config/default_config.nu") get_default_config()
}; };
match answer.to_lowercase().trim() { match answer.to_lowercase().trim() {
@ -134,7 +135,7 @@ pub(crate) fn read_default_env_file(
stack: &mut Stack, stack: &mut Stack,
is_perf_true: bool, is_perf_true: bool,
) { ) {
let config_file = include_str!("../docs/sample_config/default_env.nu"); let config_file = get_default_env();
eval_source( eval_source(
engine_state, engine_state,
stack, stack,