nu_command/env/config/
config_reset.rs1use chrono::Local;
2use nu_config::ConfigFileKind;
3use nu_engine::command_prelude::*;
4use std::io::Write;
5
6#[derive(Clone)]
7pub struct ConfigReset;
8
9impl Command for ConfigReset {
10 fn name(&self) -> &str {
11 "config reset"
12 }
13
14 fn signature(&self) -> Signature {
15 Signature::build(self.name())
16 .switch("nu", "Reset only nu config, config.nu.", Some('n'))
17 .switch("env", "Reset only env config, env.nu.", Some('e'))
18 .switch("without-backup", "Do not make a backup.", Some('w'))
19 .input_output_types(vec![(Type::Nothing, Type::Nothing)])
20 .allow_variants_without_examples(true)
21 .category(Category::Env)
22 }
23
24 fn description(&self) -> &str {
25 "Reset nushell environment configurations to default, and saves old config files in the config location as oldconfig.nu and oldenv.nu."
26 }
27
28 fn examples(&self) -> Vec<Example<'_>> {
29 vec![Example {
30 description: "Reset nushell configuration files.",
31 example: "config reset",
32 result: None,
33 }]
34 }
35
36 fn run(
37 &self,
38 engine_state: &EngineState,
39 stack: &mut Stack,
40 call: &Call,
41 _input: PipelineData,
42 ) -> Result<PipelineData, ShellError> {
43 let only_nu = call.has_flag(engine_state, stack, "nu")?;
44 let only_env = call.has_flag(engine_state, stack, "env")?;
45 let no_backup = call.has_flag(engine_state, stack, "without-backup")?;
46 let span = call.head;
47 let config_path = &engine_state.config_dirs.config_home;
48 if !only_env {
49 let kind = ConfigFileKind::Config;
50 let mut nu_config = config_path.clone();
51 nu_config.push(kind.path());
52 let config_file = kind.scaffold();
53 if !no_backup {
54 let mut backup_path = config_path.clone();
55 backup_path.push(format!(
56 "oldconfig-{}.nu",
57 Local::now().format("%F-%H-%M-%S"),
58 ));
59 if let Err(err) = std::fs::rename(nu_config.clone(), &backup_path) {
60 return Err(ShellError::Io(IoError::new_with_additional_context(
61 err.not_found_as(NotFound::Directory),
62 span,
63 backup_path,
64 "config.nu could not be backed up",
65 )));
66 }
67 }
68 if let Ok(mut file) = std::fs::File::create(&nu_config)
69 && let Err(err) = writeln!(&mut file, "{config_file}")
70 {
71 return Err(ShellError::Io(IoError::new_with_additional_context(
72 err.not_found_as(NotFound::File),
73 span,
74 nu_config,
75 "config.nu could not be written to",
76 )));
77 }
78 }
79 if !only_nu {
80 let kind = ConfigFileKind::Env;
81 let mut env_config = config_path.clone();
82 env_config.push(kind.path());
83 let config_file = kind.scaffold();
84 if !no_backup {
85 let mut backup_path = config_path.clone();
86 backup_path.push(format!("oldenv-{}.nu", Local::now().format("%F-%H-%M-%S"),));
87 if let Err(err) = std::fs::rename(env_config.clone(), &backup_path) {
88 return Err(ShellError::Io(IoError::new_with_additional_context(
89 err.not_found_as(NotFound::Directory),
90 span,
91 backup_path,
92 "env.nu could not be backed up",
93 )));
94 }
95 }
96 if let Ok(mut file) = std::fs::File::create(&env_config)
97 && let Err(err) = writeln!(&mut file, "{config_file}")
98 {
99 return Err(ShellError::Io(IoError::new_with_additional_context(
100 err.not_found_as(NotFound::File),
101 span,
102 env_config,
103 "env.nu could not be written to",
104 )));
105 }
106 }
107 Ok(PipelineData::empty())
108 }
109}