From 0812a08bfbccc32a837f88fb7c0f1cb4fc3eca89 Mon Sep 17 00:00:00 2001 From: nibon7 Date: Sun, 24 Jul 2022 20:00:52 +0800 Subject: [PATCH] Don't panic if nu failed to create config files (#6104) * Don't panic if nu failed to create config files Signed-off-by: nibon7 * eval default config Signed-off-by: nibon7 * tweak words Signed-off-by: nibon7 * tweak words again Signed-off-by: nibon7 --- src/config_files.rs | 88 +++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 31 deletions(-) diff --git a/src/config_files.rs b/src/config_files.rs index c07ca80b8a..955b021c55 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -71,40 +71,31 @@ pub(crate) fn read_config_file( }; match answer.to_lowercase().trim() { - "y" | "" => { - let mut output = File::create(&config_path).expect("Unable to create file"); - write!(output, "{}", config_file).expect("Unable to write to config file"); - println!("Config file created at: {}", config_path.to_string_lossy()); - } - _ => { - println!("Continuing without config file"); - // Just use the contents of "default_config.nu" or "default_env.nu" - eval_source( - engine_state, - stack, - config_file.as_bytes(), - if is_env_config { - "default_env.nu" - } else { - "default_config.nu" - }, - PipelineData::new(Span::new(0, 0)), - ); - - // Merge the environment in case env vars changed in the config - match nu_engine::env::current_dir(engine_state, stack) { - Ok(cwd) => { - if let Err(e) = engine_state.merge_env(stack, cwd) { - let working_set = StateWorkingSet::new(engine_state); - report_error(&working_set, &e); - } + "y" | "" => match File::create(&config_path) { + Ok(mut output) => match write!(output, "{}", config_file) { + Ok(_) => { + println!("Config file created at: {}", config_path.to_string_lossy()) } - Err(e) => { - let working_set = StateWorkingSet::new(engine_state); - report_error(&working_set, &e); + Err(_) => { + eprintln!( + "Unable to write to {}, sourcing default file instead", + config_path.to_string_lossy(), + ); + eval_default_config(engine_state, stack, config_file, is_env_config); + return; } + }, + Err(_) => { + eprintln!( + "Unable to create {}, sourcing default file instead", + config_file + ); + eval_default_config(engine_state, stack, config_file, is_env_config); + return; } - + }, + _ => { + eval_default_config(engine_state, stack, config_file, is_env_config); return; } } @@ -169,3 +160,38 @@ pub(crate) fn read_default_env_file( } } } + +fn eval_default_config( + engine_state: &mut EngineState, + stack: &mut Stack, + config_file: &str, + is_env_config: bool, +) { + println!("Continuing without config file"); + // Just use the contents of "default_config.nu" or "default_env.nu" + eval_source( + engine_state, + stack, + config_file.as_bytes(), + if is_env_config { + "default_env.nu" + } else { + "default_config.nu" + }, + PipelineData::new(Span::new(0, 0)), + ); + + // Merge the environment in case env vars changed in the config + match nu_engine::env::current_dir(engine_state, stack) { + Ok(cwd) => { + if let Err(e) = engine_state.merge_env(stack, cwd) { + let working_set = StateWorkingSet::new(engine_state); + report_error(&working_set, &e); + } + } + Err(e) => { + let working_set = StateWorkingSet::new(engine_state); + report_error(&working_set, &e); + } + } +}