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::{
|
2022-07-14 14:09:27 +00:00
|
|
|
engine::{EngineState, StateWorkingSet},
|
2022-05-11 21:15:31 +00:00
|
|
|
PipelineData, Spanned, Value,
|
2022-01-26 17:26:43 +00:00
|
|
|
};
|
|
|
|
|
2022-07-14 14:09:27 +00:00
|
|
|
/// Run a command (or commands) given to us by the user
|
2022-03-16 18:17:06 +00:00
|
|
|
pub fn evaluate_commands(
|
2022-01-26 17:26:43 +00:00
|
|
|
commands: &Spanned<String>,
|
|
|
|
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-05-11 21:15:31 +00:00
|
|
|
table_mode: Option<Value>,
|
2022-06-20 14:05:11 +00:00
|
|
|
) -> Result<Option<i64>> {
|
2022-07-14 14:09:27 +00:00
|
|
|
// Translate environment variables from Strings to Values
|
|
|
|
if let Some(e) = convert_env_values(engine_state, stack) {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the source code
|
2022-01-26 17:26:43 +00:00
|
|
|
let (block, delta) = {
|
2022-05-12 12:27:44 +00:00
|
|
|
if let Some(ref t_mode) = table_mode {
|
|
|
|
let mut config = engine_state.get_config().clone();
|
|
|
|
config.table_mode = t_mode.as_string()?;
|
|
|
|
engine_state.set_config(&config);
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:26:43 +00:00
|
|
|
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())
|
|
|
|
};
|
|
|
|
|
2022-07-14 14:09:27 +00:00
|
|
|
// Update permanent state
|
|
|
|
if let Err(err) = engine_state.merge_delta(delta) {
|
2022-01-26 17:26:43 +00:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
}
|
|
|
|
|
2022-07-14 14:09:27 +00:00
|
|
|
// Run the block
|
2022-06-20 14:05:11 +00:00
|
|
|
let exit_code = match eval_block(engine_state, stack, &block, input, false, false) {
|
2022-01-26 17:26:43 +00:00
|
|
|
Ok(pipeline_data) => {
|
2022-07-14 14:09:27 +00:00
|
|
|
let mut config = engine_state.get_config().clone();
|
|
|
|
if let Some(t_mode) = table_mode {
|
|
|
|
config.table_mode = t_mode.as_string()?;
|
|
|
|
}
|
2022-05-11 21:15:31 +00:00
|
|
|
crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut 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-06-20 14:05:11 +00:00
|
|
|
};
|
2022-01-26 17:26:43 +00:00
|
|
|
|
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-06-20 14:05:11 +00:00
|
|
|
Ok(exit_code)
|
2022-01-26 17:26:43 +00:00
|
|
|
}
|