asdf-vm/asdf

bug: exit code for a failure in terraform comamnd

anush0247 opened this issue · 2 comments

Describe the Bug

I am not able get the error code when wrapping the terraform command with asdf

Steps to Reproduce

The below script is not working

#!/bin/bash
set -e
exec /root/.asdf/bin/asdf exec "terraform" "$@"
# Capture the exit status of the Terraform command
exit_status=$?

# Check the exit status and display a message
if [ $exit_status -eq 0 ]; then
          echo "Terraform command succeeded"
  else
            echo "Terraform command failed with an error"
fi

# Exit with the captured exit status
exit $exit_status

where as below script is working

The below script is not working, I need to execute few more commands based on tf plan / any tf command is success or failure?

#!/bin/bash
set -e
exec "terraform" "$@"
# Capture the exit status of the Terraform command
exit_status=$?

# Check the exit status and display a message
if [ $exit_status -eq 0 ]; then
          echo "Terraform command succeeded"
  else
            echo "Terraform command failed with an error"
fi

# Exit with the captured exit status
exit $exit_status

Expected Behaviour

give the exit_status

Actual Behaviour

Its not giving the exit_status

Environment

local / docker

asdf plugins affected (if relevant)

No response

This isn't an issue with Terraform or asdf - it's an issue with your script. When you use exec, execution immediately shifts into the executable that you specify. The rest of the script is never executed. From the perspective of Bash, exec is the last line of code that it executes before stopping.

I think ShellCheck catches this case, I'd recommend using it to help find these kind of errors.

Thanks it worked, I am closing the thread