Added loginshell config file #4620 (#5714)

* Added loginshell config file #4620

* added sample login.nu

* added environment variable loginshell-path
This commit is contained in:
sec65 2022-06-06 13:52:37 +02:00 committed by GitHub
parent 75b2d26187
commit 3c421c5726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 1 deletions

View File

@ -19,13 +19,14 @@ fn variables_completions() {
// Test completions for $nu // Test completions for $nu
let suggestions = completer.complete("$nu.", 4); let suggestions = completer.complete("$nu.", 4);
assert_eq!(8, suggestions.len()); assert_eq!(9, suggestions.len());
let expected: Vec<String> = vec![ let expected: Vec<String> = vec![
"config-path".into(), "config-path".into(),
"env-path".into(), "env-path".into(),
"history-path".into(), "history-path".into(),
"home-path".into(), "home-path".into(),
"loginshell-path".into(),
"os-info".into(), "os-info".into(),
"pid".into(), "pid".into(),
"scope".into(), "scope".into(),

View File

@ -1196,6 +1196,7 @@ pub fn eval_variable(
if let Some(mut config_path) = nu_path::config_dir() { if let Some(mut config_path) = nu_path::config_dir() {
config_path.push("nushell"); config_path.push("nushell");
let mut env_config_path = config_path.clone(); let mut env_config_path = config_path.clone();
let mut loginshell_path = config_path.clone();
let mut history_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(), val: env_config_path.to_string_lossy().to_string(),
span, 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")] #[cfg(feature = "plugin")]

View File

@ -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

View File

@ -11,6 +11,7 @@ use std::path::PathBuf;
pub(crate) const NUSHELL_FOLDER: &str = "nushell"; pub(crate) const NUSHELL_FOLDER: &str = "nushell";
const CONFIG_FILE: &str = "config.nu"; const CONFIG_FILE: &str = "config.nu";
const ENV_FILE: &str = "env.nu"; const ENV_FILE: &str = "env.nu";
const LOGINSHELL_FILE: &str = "login.nu";
const HISTORY_FILE: &str = "history.txt"; const HISTORY_FILE: &str = "history.txt";
pub(crate) fn read_config_file( 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<PathBuf> { pub(crate) fn create_history_path() -> Option<PathBuf> {
nu_path::config_dir().and_then(|mut history_path| { nu_path::config_dir().and_then(|mut history_path| {
history_path.push(NUSHELL_FOLDER); history_path.push(NUSHELL_FOLDER);

View File

@ -275,6 +275,7 @@ fn main() -> Result<()> {
&mut stack, &mut stack,
binary_args.config_file, binary_args.config_file,
binary_args.env_file, binary_args.env_file,
binary_args.login_shell.is_some(),
); );
let history_path = config_files::create_history_path(); let history_path = config_files::create_history_path();
@ -296,6 +297,7 @@ fn setup_config(
stack: &mut Stack, stack: &mut Stack,
config_file: Option<Spanned<String>>, config_file: Option<Spanned<String>>,
env_file: Option<Spanned<String>>, env_file: Option<Spanned<String>>,
is_login_shell: bool,
) { ) {
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
read_plugin_file(engine_state, stack, NUSHELL_FOLDER, is_perf_true()); 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, env_file, is_perf_true(), true);
config_files::read_config_file(engine_state, stack, config_file, is_perf_true(), false); 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 // Give a warning if we see `$config` for a few releases
{ {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);