From 3c421c5726f5020fffdb02c4cea67c15edd1c7b4 Mon Sep 17 00:00:00 2001 From: sec65 <106604020+sec65@users.noreply.github.com> Date: Mon, 6 Jun 2022 13:52:37 +0200 Subject: [PATCH] Added loginshell config file #4620 (#5714) * Added loginshell config file #4620 * added sample login.nu * added environment variable loginshell-path --- crates/nu-cli/tests/variables_completions.rs | 3 ++- crates/nu-engine/src/eval.rs | 9 +++++++++ docs/sample_config/sample_login.nu | 9 +++++++++ src/config_files.rs | 21 ++++++++++++++++++++ src/main.rs | 6 ++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 docs/sample_config/sample_login.nu diff --git a/crates/nu-cli/tests/variables_completions.rs b/crates/nu-cli/tests/variables_completions.rs index bd8adec95b..53f1a9d47b 100644 --- a/crates/nu-cli/tests/variables_completions.rs +++ b/crates/nu-cli/tests/variables_completions.rs @@ -19,13 +19,14 @@ fn variables_completions() { // Test completions for $nu let suggestions = completer.complete("$nu.", 4); - assert_eq!(8, suggestions.len()); + assert_eq!(9, suggestions.len()); let expected: Vec = vec![ "config-path".into(), "env-path".into(), "history-path".into(), "home-path".into(), + "loginshell-path".into(), "os-info".into(), "pid".into(), "scope".into(), diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index a19d3ed8b8..0469f30434 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -1196,6 +1196,7 @@ pub fn eval_variable( if let Some(mut config_path) = nu_path::config_dir() { config_path.push("nushell"); let mut env_config_path = config_path.clone(); + let mut loginshell_path = config_path.clone(); let mut history_path = config_path.clone(); @@ -1222,6 +1223,14 @@ pub fn eval_variable( val: env_config_path.to_string_lossy().to_string(), span, }); + + loginshell_path.push("login.nu"); + + output_cols.push("loginshell-path".into()); + output_vals.push(Value::String { + val: loginshell_path.to_string_lossy().to_string(), + span, + }); } #[cfg(feature = "plugin")] diff --git a/docs/sample_config/sample_login.nu b/docs/sample_config/sample_login.nu new file mode 100644 index 0000000000..4bb43f2543 --- /dev/null +++ b/docs/sample_config/sample_login.nu @@ -0,0 +1,9 @@ +# Example Nushell Loginshell Config File +# - has to be as login.nu in the default config directory +# - will be sourced after config.nu and env.nu in case of nushell started as login shell + +# just as an example for overwriting of an environment variable of env.nu +let-env PROMPT_INDICATOR = { "(LS)〉" } + +# Similar to env-path and config-path there is a variable containing the path to login.nu +echo $nu.loginshell-path \ No newline at end of file diff --git a/src/config_files.rs b/src/config_files.rs index a5317401e2..937f3ab8d3 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -11,6 +11,7 @@ use std::path::PathBuf; pub(crate) const NUSHELL_FOLDER: &str = "nushell"; const CONFIG_FILE: &str = "config.nu"; const ENV_FILE: &str = "env.nu"; +const LOGINSHELL_FILE: &str = "login.nu"; const HISTORY_FILE: &str = "history.txt"; pub(crate) fn read_config_file( @@ -104,6 +105,26 @@ pub(crate) fn read_config_file( } } +pub(crate) fn read_loginshell_file( + engine_state: &mut EngineState, + stack: &mut Stack, + is_perf_true: bool, +) { + // read and execute loginshell file if exists + if let Some(mut config_path) = nu_path::config_dir() { + config_path.push(NUSHELL_FOLDER); + config_path.push(LOGINSHELL_FILE); + + if config_path.exists() { + eval_config_contents(config_path, engine_state, stack); + } + } + + if is_perf_true { + info!("read_loginshell_file {}:{}:{}", file!(), line!(), column!()); + } +} + pub(crate) fn create_history_path() -> Option { nu_path::config_dir().and_then(|mut history_path| { history_path.push(NUSHELL_FOLDER); diff --git a/src/main.rs b/src/main.rs index 3db83f5d49..b9b154a8ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -275,6 +275,7 @@ fn main() -> Result<()> { &mut stack, binary_args.config_file, binary_args.env_file, + binary_args.login_shell.is_some(), ); let history_path = config_files::create_history_path(); @@ -296,6 +297,7 @@ fn setup_config( stack: &mut Stack, config_file: Option>, env_file: Option>, + is_login_shell: bool, ) { #[cfg(feature = "plugin")] read_plugin_file(engine_state, stack, NUSHELL_FOLDER, is_perf_true()); @@ -307,6 +309,10 @@ fn setup_config( config_files::read_config_file(engine_state, stack, env_file, is_perf_true(), true); config_files::read_config_file(engine_state, stack, config_file, is_perf_true(), false); + if is_login_shell { + config_files::read_loginshell_file(engine_state, stack, is_perf_true()); + } + // Give a warning if we see `$config` for a few releases { let working_set = StateWorkingSet::new(engine_state);