2022-03-16 18:17:06 +00:00
|
|
|
use crate::util::report_error;
|
2022-02-09 22:08:16 +00:00
|
|
|
use log::info;
|
2022-01-26 17:26:43 +00:00
|
|
|
use miette::Result;
|
|
|
|
use nu_engine::{convert_env_values, eval_block};
|
2022-05-01 21:49:31 +00:00
|
|
|
use nu_parser::parse;
|
2022-03-16 18:17:06 +00:00
|
|
|
use nu_protocol::engine::Stack;
|
2022-01-26 17:26:43 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
engine::{EngineState, StateDelta, StateWorkingSet},
|
2022-04-18 22:28:01 +00:00
|
|
|
PipelineData, Spanned,
|
2022-01-26 17:26:43 +00:00
|
|
|
};
|
2022-02-09 22:08:16 +00:00
|
|
|
use std::path::Path;
|
2022-01-26 17:26:43 +00:00
|
|
|
|
2022-03-16 18:17:06 +00:00
|
|
|
pub fn evaluate_commands(
|
2022-01-26 17:26:43 +00:00
|
|
|
commands: &Spanned<String>,
|
|
|
|
init_cwd: &Path,
|
|
|
|
engine_state: &mut EngineState,
|
2022-03-16 18:17:06 +00:00
|
|
|
stack: &mut Stack,
|
2022-01-26 17:26:43 +00:00
|
|
|
input: PipelineData,
|
2022-03-16 18:17:06 +00:00
|
|
|
is_perf_true: bool,
|
2022-01-26 17:26:43 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
// Run a command (or commands) given to us by the user
|
|
|
|
let (block, delta) = {
|
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
|
|
|
|
2022-05-01 21:49:31 +00:00
|
|
|
let (output, err) = parse(&mut working_set, None, commands.item.as_bytes(), false, &[]);
|
2022-01-26 17:26:43 +00:00
|
|
|
if let Some(err) = err {
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
(output, working_set.render())
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = engine_state.merge_delta(delta, None, init_cwd) {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
}
|
|
|
|
|
2022-04-18 22:28:01 +00:00
|
|
|
let config = engine_state.get_config().clone();
|
2022-01-26 17:26:43 +00:00
|
|
|
|
|
|
|
// Merge the delta in case env vars changed in the config
|
2022-03-16 18:17:06 +00:00
|
|
|
match nu_engine::env::current_dir(engine_state, stack) {
|
2022-01-26 17:26:43 +00:00
|
|
|
Ok(cwd) => {
|
2022-05-07 19:39:22 +00:00
|
|
|
if let Err(e) =
|
|
|
|
engine_state.merge_delta(StateDelta::new(engine_state), Some(stack), cwd)
|
|
|
|
{
|
2022-01-26 17:26:43 +00:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
2022-03-04 22:46:18 +00:00
|
|
|
std::process::exit(1);
|
2022-01-26 17:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
2022-03-04 22:46:18 +00:00
|
|
|
std::process::exit(1);
|
2022-01-26 17:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate environment variables from Strings to Values
|
2022-03-16 18:17:06 +00:00
|
|
|
if let Some(e) = convert_env_values(engine_state, stack) {
|
2022-01-26 17:26:43 +00:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:17:06 +00:00
|
|
|
match eval_block(engine_state, stack, &block, input, false, false) {
|
2022-01-26 17:26:43 +00:00
|
|
|
Ok(pipeline_data) => {
|
2022-03-16 18:17:06 +00:00
|
|
|
crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &config)
|
2022-01-26 17:26:43 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:17:06 +00:00
|
|
|
if is_perf_true {
|
2022-02-09 22:08:16 +00:00
|
|
|
info!("evaluate {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:26:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|