2022-02-09 22:08:16 +00:00
|
|
|
use log::info;
|
2022-03-16 18:17:06 +00:00
|
|
|
use nu_cli::{eval_config_contents, eval_source, report_error};
|
2022-02-19 20:54:43 +00:00
|
|
|
use nu_parser::ParseError;
|
|
|
|
use nu_path::canonicalize_with;
|
2022-07-14 14:09:27 +00:00
|
|
|
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
2022-02-19 20:54:43 +00:00
|
|
|
use nu_protocol::{PipelineData, Span, Spanned};
|
2022-08-01 01:44:33 +00:00
|
|
|
use nu_utils::{get_default_config, get_default_env};
|
2022-02-20 10:08:53 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2022-01-18 08:48:28 +00:00
|
|
|
|
2022-03-16 18:17:06 +00:00
|
|
|
pub(crate) const NUSHELL_FOLDER: &str = "nushell";
|
2022-01-18 08:48:28 +00:00
|
|
|
const CONFIG_FILE: &str = "config.nu";
|
2022-04-06 17:11:51 +00:00
|
|
|
const ENV_FILE: &str = "env.nu";
|
2022-06-06 11:52:37 +00:00
|
|
|
const LOGINSHELL_FILE: &str = "login.nu";
|
2022-02-26 08:57:51 +00:00
|
|
|
|
2022-02-19 20:54:43 +00:00
|
|
|
pub(crate) fn read_config_file(
|
|
|
|
engine_state: &mut EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
config_file: Option<Spanned<String>>,
|
2022-03-16 18:17:06 +00:00
|
|
|
is_perf_true: bool,
|
2022-04-06 17:11:51 +00:00
|
|
|
is_env_config: bool,
|
2022-02-19 20:54:43 +00:00
|
|
|
) {
|
2022-01-18 08:48:28 +00:00
|
|
|
// Load config startup file
|
2022-02-19 20:54:43 +00:00
|
|
|
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() {
|
2022-01-18 08:48:28 +00:00
|
|
|
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);
|
2022-02-19 20:54:43 +00:00
|
|
|
return;
|
2022-01-18 08:48:28 +00:00
|
|
|
}
|
2022-02-19 20:54:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:11:51 +00:00
|
|
|
config_path.push(if is_env_config { ENV_FILE } else { CONFIG_FILE });
|
2022-02-20 10:08:53 +00:00
|
|
|
|
2022-07-10 12:16:46 +00:00
|
|
|
if !config_path.exists() {
|
2022-04-06 17:11:51 +00:00
|
|
|
let file_msg = if is_env_config {
|
|
|
|
"environment config"
|
|
|
|
} else {
|
|
|
|
"config"
|
|
|
|
};
|
|
|
|
println!(
|
|
|
|
"No {} file found at {}",
|
|
|
|
file_msg,
|
|
|
|
config_path.to_string_lossy()
|
|
|
|
);
|
2022-02-25 18:58:47 +00:00
|
|
|
println!("Would you like to create one with defaults (Y/n): ");
|
2022-02-20 10:08:53 +00:00
|
|
|
|
|
|
|
let mut answer = String::new();
|
|
|
|
std::io::stdin()
|
|
|
|
.read_line(&mut answer)
|
|
|
|
.expect("Failed to read user input");
|
|
|
|
|
2022-07-10 12:16:46 +00:00
|
|
|
let config_file = if is_env_config {
|
2022-08-01 01:44:33 +00:00
|
|
|
get_default_env()
|
2022-07-10 12:16:46 +00:00
|
|
|
} else {
|
2022-08-01 01:44:33 +00:00
|
|
|
get_default_config()
|
2022-07-10 12:16:46 +00:00
|
|
|
};
|
|
|
|
|
2022-02-20 10:08:53 +00:00
|
|
|
match answer.to_lowercase().trim() {
|
2022-07-24 12:00:52 +00:00
|
|
|
"y" | "" => match File::create(&config_path) {
|
|
|
|
Ok(mut output) => match write!(output, "{}", config_file) {
|
|
|
|
Ok(_) => {
|
|
|
|
println!("Config file created at: {}", config_path.to_string_lossy())
|
2022-07-14 14:09:27 +00:00
|
|
|
}
|
2022-07-24 12:00:52 +00:00
|
|
|
Err(_) => {
|
|
|
|
eprintln!(
|
|
|
|
"Unable to write to {}, sourcing default file instead",
|
|
|
|
config_path.to_string_lossy(),
|
|
|
|
);
|
|
|
|
eval_default_config(engine_state, stack, config_file, is_env_config);
|
|
|
|
return;
|
2022-07-14 14:09:27 +00:00
|
|
|
}
|
2022-07-24 12:00:52 +00:00
|
|
|
},
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!(
|
|
|
|
"Unable to create {}, sourcing default file instead",
|
|
|
|
config_file
|
|
|
|
);
|
|
|
|
eval_default_config(engine_state, stack, config_file, is_env_config);
|
|
|
|
return;
|
2022-07-14 14:09:27 +00:00
|
|
|
}
|
2022-07-24 12:00:52 +00:00
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
eval_default_config(engine_state, stack, config_file, is_env_config);
|
2022-02-20 10:08:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 20:54:43 +00:00
|
|
|
eval_config_contents(config_path, engine_state, stack);
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:17:06 +00:00
|
|
|
if is_perf_true {
|
2022-02-19 20:54:43 +00:00
|
|
|
info!("read_config_file {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 14:09:27 +00:00
|
|
|
|
2022-06-06 11:52:37 +00:00
|
|
|
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!());
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 05:53:13 +00:00
|
|
|
|
|
|
|
pub(crate) fn read_default_env_file(
|
|
|
|
engine_state: &mut EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
is_perf_true: bool,
|
|
|
|
) {
|
2022-08-01 01:44:33 +00:00
|
|
|
let config_file = get_default_env();
|
2022-07-14 05:53:13 +00:00
|
|
|
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!());
|
|
|
|
}
|
2022-07-14 14:09:27 +00:00
|
|
|
// Merge the environment in case env vars changed in the config
|
2022-07-14 05:53:13 +00:00
|
|
|
match nu_engine::env::current_dir(engine_state, stack) {
|
|
|
|
Ok(cwd) => {
|
2022-07-14 14:09:27 +00:00
|
|
|
if let Err(e) = engine_state.merge_env(stack, cwd) {
|
2022-07-14 05:53:13 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-24 12:00:52 +00:00
|
|
|
|
|
|
|
fn eval_default_config(
|
|
|
|
engine_state: &mut EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
config_file: &str,
|
|
|
|
is_env_config: bool,
|
|
|
|
) {
|
|
|
|
println!("Continuing without config file");
|
|
|
|
// Just use the contents of "default_config.nu" or "default_env.nu"
|
|
|
|
eval_source(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
config_file.as_bytes(),
|
|
|
|
if is_env_config {
|
|
|
|
"default_env.nu"
|
|
|
|
} else {
|
|
|
|
"default_config.nu"
|
|
|
|
},
|
|
|
|
PipelineData::new(Span::new(0, 0)),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Merge the environment 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_env(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|