add LAST_EXIT_CODE variable (#4655)

This commit is contained in:
LordMZTE 2022-02-26 14:57:45 +01:00 committed by GitHub
parent 11bc056576
commit d2bd71d2aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -86,7 +86,7 @@ pub(crate) fn evaluate(
let exceptions = crate::utils::external_exceptions(engine_state, &stack); let exceptions = crate::utils::external_exceptions(engine_state, &stack);
engine_state.external_exceptions = exceptions; engine_state.external_exceptions = exceptions;
// seed the cmd_duration_ms env var // seed env vars
stack.add_env_var( stack.add_env_var(
"CMD_DURATION_MS".into(), "CMD_DURATION_MS".into(),
Value::String { Value::String {
@ -95,6 +95,14 @@ pub(crate) fn evaluate(
}, },
); );
stack.add_env_var(
"LAST_EXIT_CODE".into(),
Value::Int {
val: 0,
span: Span { start: 0, end: 0 },
},
);
loop { loop {
if is_perf_true() { if is_perf_true() {
info!( info!(

View file

@ -227,7 +227,13 @@ pub(crate) fn eval_source(
} }
match eval_block(engine_state, stack, &block, input, false, false) { match eval_block(engine_state, stack, &block, input, false, false) {
Ok(pipeline_data) => { Ok(mut pipeline_data) => {
if let PipelineData::ExternalStream { exit_code, .. } = &mut pipeline_data {
if let Some(exit_code) = exit_code.take().and_then(|it| it.last()) {
stack.add_env_var("LAST_EXIT_CODE".to_string(), exit_code);
}
}
if let Err(err) = print_pipeline_data(pipeline_data, engine_state, stack) { if let Err(err) = print_pipeline_data(pipeline_data, engine_state, stack) {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);