load default env when user don't specified env path (#6040)

This commit is contained in:
WindSoilder 2022-07-14 13:53:13 +08:00 committed by GitHub
parent ceafe434b5
commit ed50210832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -2,7 +2,7 @@ use log::info;
use nu_cli::{eval_config_contents, eval_source, report_error};
use nu_parser::ParseError;
use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::engine::{EngineState, Stack, StateDelta, StateWorkingSet};
use nu_protocol::{PipelineData, Span, Spanned};
use std::fs::File;
use std::io::Write;
@ -121,3 +121,37 @@ pub(crate) fn read_loginshell_file(
info!("read_loginshell_file {}:{}:{}", file!(), line!(), column!());
}
}
pub(crate) fn read_default_env_file(
engine_state: &mut EngineState,
stack: &mut Stack,
is_perf_true: bool,
) {
let config_file = include_str!("../docs/sample_config/default_env.nu");
eval_source(
engine_state,
stack,
config_file.as_bytes(),
"default_env.nu",
PipelineData::new(Span::new(0, 0)),
);
if is_perf_true {
info!("read_config_file {}:{}:{}", file!(), line!(), column!());
}
// Merge the delta 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_delta(StateDelta::new(engine_state), Some(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);
}
}
}

View File

@ -215,6 +215,12 @@ fn main() -> Result<()> {
is_perf_true(),
true,
);
} else {
config_files::read_default_env_file(
&mut engine_state,
&mut stack,
is_perf_true(),
)
}
if binary_args.config_file.is_some() {
@ -262,6 +268,12 @@ fn main() -> Result<()> {
is_perf_true(),
true,
);
} else {
config_files::read_default_env_file(
&mut engine_state,
&mut stack,
is_perf_true(),
)
}
if binary_args.config_file.is_some() {