Get $nu.config-path and $nu.env-path from EngineState (#6366)

* Get `$nu.config-path` and `$nu.env-path` from `EngineState`

Signed-off-by: nibon7 <nibon7@163.com>

* replace tuple with hashmap

Signed-off-by: nibon7 <nibon7@163.com>

* refactor set_config_path

Signed-off-by: nibon7 <nibon7@163.com>

Signed-off-by: nibon7 <nibon7@163.com>
This commit is contained in:
nibon7 2022-08-23 00:30:09 +08:00 committed by GitHub
parent 9c4bbe3c63
commit 772ad896c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 13 deletions

View File

@ -1298,6 +1298,22 @@ pub fn eval_variable(
let mut output_cols = vec![]; let mut output_cols = vec![];
let mut output_vals = vec![]; let mut output_vals = vec![];
if let Some(path) = engine_state.get_config_path("config-path") {
output_cols.push("config-path".into());
output_vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
});
}
if let Some(path) = engine_state.get_config_path("env-path") {
output_cols.push("env-path".into());
output_vals.push(Value::String {
val: path.to_string_lossy().to_string(),
span,
});
}
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();
@ -1321,21 +1337,25 @@ pub fn eval_variable(
span, span,
}); });
config_path.push("config.nu"); if engine_state.get_config_path("config-path").is_none() {
config_path.push("config.nu");
output_cols.push("config-path".into()); output_cols.push("config-path".into());
output_vals.push(Value::String { output_vals.push(Value::String {
val: config_path.to_string_lossy().to_string(), val: config_path.to_string_lossy().to_string(),
span, span,
}); });
}
env_config_path.push("env.nu"); if engine_state.get_config_path("env-path").is_none() {
env_config_path.push("env.nu");
output_cols.push("env-path".into()); output_cols.push("env-path".into());
output_vals.push(Value::String { output_vals.push(Value::String {
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"); loginshell_path.push("login.nu");

View File

@ -76,6 +76,7 @@ pub struct EngineState {
pub plugin_signatures: Option<PathBuf>, pub plugin_signatures: Option<PathBuf>,
#[cfg(not(windows))] #[cfg(not(windows))]
sig_quit: Option<Arc<AtomicBool>>, sig_quit: Option<Arc<AtomicBool>>,
config_path: HashMap<String, PathBuf>,
} }
pub const NU_VARIABLE_ID: usize = 0; pub const NU_VARIABLE_ID: usize = 0;
@ -113,6 +114,7 @@ impl EngineState {
plugin_signatures: None, plugin_signatures: None,
#[cfg(not(windows))] #[cfg(not(windows))]
sig_quit: None, sig_quit: None,
config_path: HashMap::new(),
} }
} }
@ -753,6 +755,14 @@ impl EngineState {
pub fn set_sig_quit(&mut self, sig_quit: Arc<AtomicBool>) { pub fn set_sig_quit(&mut self, sig_quit: Arc<AtomicBool>) {
self.sig_quit = Some(sig_quit) self.sig_quit = Some(sig_quit)
} }
pub fn set_config_path(&mut self, key: &str, val: PathBuf) {
self.config_path.insert(key.to_string(), val);
}
pub fn get_config_path(&self, key: &str) -> Option<&PathBuf> {
self.config_path.get(key)
}
} }
/// A temporary extension to the global state. This handles bridging between the global state and the /// A temporary extension to the global state. This handles bridging between the global state and the

View File

@ -18,6 +18,7 @@ use nu_cli::{
use nu_command::{create_default_context, BufferedReader}; use nu_command::{create_default_context, BufferedReader};
use nu_engine::{get_full_help, CallExt}; use nu_engine::{get_full_help, CallExt};
use nu_parser::{escape_for_script_arg, escape_quote_string, parse}; use nu_parser::{escape_for_script_arg, escape_quote_string, parse};
use nu_path::canonicalize_with;
use nu_protocol::{ use nu_protocol::{
ast::{Call, Expr, Expression}, ast::{Call, Expr, Expression},
engine::{Command, EngineState, Stack, StateWorkingSet}, engine::{Command, EngineState, Stack, StateWorkingSet},
@ -25,7 +26,7 @@ use nu_protocol::{
Spanned, SyntaxShape, Value, Spanned, SyntaxShape, Value,
}; };
use nu_utils::stdout_write_all_and_flush; use nu_utils::stdout_write_all_and_flush;
use std::cell::RefCell; use std::{cell::RefCell, path::Path};
use std::{ use std::{
io::BufReader, io::BufReader,
sync::{ sync::{
@ -142,6 +143,24 @@ fn main() -> Result<()> {
nu_system::signal::set_terminal_leader() nu_system::signal::set_terminal_leader()
} }
if let Ok(ref args) = parsed_nu_cli_args {
set_config_path(
&mut engine_state,
&init_cwd,
"config.nu",
"config-path",
&args.config_file,
);
set_config_path(
&mut engine_state,
&init_cwd,
"env.nu",
"env-path",
&args.env_file,
);
}
match parsed_nu_cli_args { match parsed_nu_cli_args {
Ok(binary_args) => { Ok(binary_args) => {
if let Some(t) = binary_args.threads { if let Some(t) = binary_args.threads {
@ -690,3 +709,24 @@ fn set_is_perf_value(value: bool) {
*new_value.borrow_mut() = value; *new_value.borrow_mut() = value;
}); });
} }
fn set_config_path(
engine_state: &mut EngineState,
cwd: &Path,
default_config_name: &str,
key: &str,
config_file: &Option<Spanned<String>>,
) {
let config_path = match config_file {
Some(s) => canonicalize_with(&s.item, cwd).ok(),
None => nu_path::config_dir().map(|mut p| {
p.push(config_files::NUSHELL_FOLDER);
p.push(default_config_name);
p
}),
};
if let Some(path) = config_path {
engine_state.set_config_path(key, path);
}
}