mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 05:55:12 +02:00
flag to pass config file in nu (#4552)
* flag to pass config file in nu * return when no folder is created * simple syntax for function
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
use crate::is_perf_true;
|
||||
use crate::utils::{eval_source, report_error};
|
||||
use log::info;
|
||||
use nu_parser::ParseError;
|
||||
use nu_path::canonicalize_with;
|
||||
use nu_protocol::engine::{EngineState, Stack, StateDelta, StateWorkingSet};
|
||||
use nu_protocol::{PipelineData, Span};
|
||||
use nu_protocol::{PipelineData, Span, Spanned};
|
||||
use std::path::PathBuf;
|
||||
|
||||
const NUSHELL_FOLDER: &str = "nushell";
|
||||
@ -38,54 +40,73 @@ pub(crate) fn read_plugin_file(engine_state: &mut EngineState, stack: &mut Stack
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn read_config_file(engine_state: &mut EngineState, stack: &mut Stack) {
|
||||
pub(crate) fn read_config_file(
|
||||
engine_state: &mut EngineState,
|
||||
stack: &mut Stack,
|
||||
config_file: Option<Spanned<String>>,
|
||||
) {
|
||||
// Load config startup file
|
||||
if let Some(mut config_path) = nu_path::config_dir() {
|
||||
if let Some(file) = config_file {
|
||||
let working_set = StateWorkingSet::new(engine_state);
|
||||
let cwd = working_set.get_cwd();
|
||||
|
||||
match canonicalize_with(&file.item, cwd) {
|
||||
Ok(path) => {
|
||||
eval_config_contents(path, engine_state, stack);
|
||||
}
|
||||
Err(_) => {
|
||||
let e = ParseError::FileNotFound(file.item, file.span);
|
||||
report_error(&working_set, &e);
|
||||
}
|
||||
}
|
||||
} else if let Some(mut config_path) = nu_path::config_dir() {
|
||||
config_path.push(NUSHELL_FOLDER);
|
||||
|
||||
// Create config directory if it does not exist
|
||||
if !config_path.exists() {
|
||||
if let Err(err) = std::fs::create_dir_all(&config_path) {
|
||||
eprintln!("Failed to create config directory: {}", err);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
config_path.push(CONFIG_FILE);
|
||||
}
|
||||
|
||||
if config_path.exists() {
|
||||
// FIXME: remove this message when we're ready
|
||||
//println!("Loading config from: {:?}", config_path);
|
||||
let config_filename = config_path.to_string_lossy().to_owned();
|
||||
config_path.push(CONFIG_FILE);
|
||||
eval_config_contents(config_path, engine_state, stack);
|
||||
}
|
||||
|
||||
if let Ok(contents) = std::fs::read(&config_path) {
|
||||
eval_source(
|
||||
engine_state,
|
||||
stack,
|
||||
&contents,
|
||||
&config_filename,
|
||||
PipelineData::new(Span::new(0, 0)),
|
||||
);
|
||||
// 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(), 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);
|
||||
}
|
||||
if is_perf_true() {
|
||||
info!("read_config_file {}:{}:{}", file!(), line!(), column!());
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_config_contents(config_path: PathBuf, engine_state: &mut EngineState, stack: &mut Stack) {
|
||||
if config_path.exists() & config_path.is_file() {
|
||||
let config_filename = config_path.to_string_lossy().to_owned();
|
||||
|
||||
if let Ok(contents) = std::fs::read(&config_path) {
|
||||
eval_source(
|
||||
engine_state,
|
||||
stack,
|
||||
&contents,
|
||||
&config_filename,
|
||||
PipelineData::new(Span::new(0, 0)),
|
||||
);
|
||||
|
||||
// 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(), 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_perf_true() {
|
||||
info!("read_config_file {}:{}:{}", file!(), line!(), column!());
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create_history_path() -> Option<PathBuf> {
|
||||
|
Reference in New Issue
Block a user