diff --git a/src/config_files.rs b/src/config_files.rs index 2a685faf34..1dd2a4b66d 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -27,6 +27,7 @@ pub(crate) fn read_config_file( stack: &mut Stack, config_file: Option>, is_env_config: bool, + ask_to_create: bool, ) { warn!( "read_config_file() config_file_specified: {:?}, is_env_config: {is_env_config}", @@ -66,17 +67,25 @@ pub(crate) fn read_config_file( } else { "config" }; - println!( - "No {} file found at {}", - file_msg, - config_path.to_string_lossy() - ); - println!("Would you like to create one with defaults (Y/n): "); - let mut answer = String::new(); - std::io::stdin() - .read_line(&mut answer) - .expect("Failed to read user input"); + let will_create_file = match ask_to_create { + true => { + println!( + "No {} file found at {}", + file_msg, + config_path.to_string_lossy() + ); + println!("Would you like to create one with defaults (Y/n): "); + + let mut answer = String::new(); + std::io::stdin() + .read_line(&mut answer) + .expect("Failed to read user input"); + + matches!(answer.trim(), "y" | "Y" | "") + } + _ => false, + }; let config_file = if is_env_config { get_default_env() @@ -84,8 +93,8 @@ pub(crate) fn read_config_file( get_default_config() }; - match answer.trim() { - "y" | "Y" | "" => { + match will_create_file { + true => { if let Ok(mut output) = File::create(&config_path) { if write!(output, "{config_file}").is_ok() { let config_type = if is_env_config { @@ -226,7 +235,6 @@ fn eval_default_config( "eval_default_config() config_file_specified: {:?}, is_env_config: {}", &config_file, is_env_config ); - println!("Continuing without config file"); // Just use the contents of "default_config.nu" or "default_env.nu" eval_source( engine_state, @@ -259,12 +267,26 @@ pub(crate) fn setup_config( "setup_config() config_file_specified: {:?}, env_file_specified: {:?}, login: {}", &config_file, &env_file, is_login_shell ); + + let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() { + config_path.push(NUSHELL_FOLDER); + !config_path.exists() + } else { + false + }; + let result = catch_unwind(AssertUnwindSafe(|| { #[cfg(feature = "plugin")] read_plugin_file(engine_state, plugin_file, NUSHELL_FOLDER); - read_config_file(engine_state, stack, env_file, true); - read_config_file(engine_state, stack, config_file, false); + read_config_file(engine_state, stack, env_file, true, ask_to_create_config); + read_config_file( + engine_state, + stack, + config_file, + false, + ask_to_create_config, + ); if is_login_shell { read_loginshell_file(engine_state, stack); diff --git a/src/run.rs b/src/run.rs index 068a581b72..16dbe0a5b6 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,8 +1,6 @@ -#[cfg(feature = "plugin")] -use crate::config_files::NUSHELL_FOLDER; use crate::{ command, - config_files::{self, setup_config}, + config_files::{self, setup_config, NUSHELL_FOLDER}, }; use log::trace; #[cfg(feature = "plugin")] @@ -26,6 +24,13 @@ pub(crate) fn run_commands( let mut stack = Stack::new(); let start_time = std::time::Instant::now(); + let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() { + config_path.push(NUSHELL_FOLDER); + !config_path.exists() + } else { + false + }; + if stack.has_env_var(engine_state, "NU_DISABLE_IR") { stack.use_ir = false; } @@ -49,6 +54,7 @@ pub(crate) fn run_commands( &mut stack, parsed_nu_cli_args.env_file, true, + ask_to_create_config, ); } else { config_files::read_default_env_file(engine_state, &mut stack) @@ -57,6 +63,13 @@ pub(crate) fn run_commands( perf!("read env.nu", start_time, use_color); let start_time = std::time::Instant::now(); + let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() { + config_path.push(config_files::NUSHELL_FOLDER); + !config_path.exists() + } else { + false + }; + // If we have a config file parameter *OR* we have a login shell parameter, read the config file if parsed_nu_cli_args.config_file.is_some() || parsed_nu_cli_args.login_shell.is_some() { config_files::read_config_file( @@ -64,6 +77,7 @@ pub(crate) fn run_commands( &mut stack, parsed_nu_cli_args.config_file, false, + ask_to_create_config, ); } @@ -126,6 +140,12 @@ pub(crate) fn run_file( // if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files if parsed_nu_cli_args.no_config_file.is_none() { let start_time = std::time::Instant::now(); + let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() { + config_path.push(config_files::NUSHELL_FOLDER); + !config_path.exists() + } else { + false + }; #[cfg(feature = "plugin")] read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER); perf!("read plugins", start_time, use_color); @@ -138,6 +158,7 @@ pub(crate) fn run_file( &mut stack, parsed_nu_cli_args.env_file, true, + ask_to_create_config, ); } else { config_files::read_default_env_file(engine_state, &mut stack) @@ -151,6 +172,7 @@ pub(crate) fn run_file( &mut stack, parsed_nu_cli_args.config_file, false, + ask_to_create_config, ); } perf!("read config.nu", start_time, use_color); @@ -208,7 +230,7 @@ pub(crate) fn run_repl( let ret_val = evaluate_repl( engine_state, stack, - config_files::NUSHELL_FOLDER, + NUSHELL_FOLDER, parsed_nu_cli_args.execute, parsed_nu_cli_args.no_std_lib, entire_start_time,