Skip to content

Commit 043168b

Browse files
authored
fix: use correct exit code in the plan command (#79)
* fix: use correct exit code in the plan command * docs: add reference to original detailed-exitcode * chore: refactor so that the `min` function is not called in every iteration
1 parent 3424752 commit 043168b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

terranova/commands/binds.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ def plan(
305305

306306
# Store errors if fail_at_end
307307
errors = False
308+
error_exit_codes = []
308309

309310
# Format all paths
310311
for full_path, rel_path in paths:
@@ -322,14 +323,20 @@ def plan(
322323
parallelism=parallelism,
323324
detailed_exitcode=detailed_exitcode,
324325
)
325-
except sh.ErrorReturnCode:
326+
except sh.ErrorReturnCode as err:
326327
errors = True
328+
error_exit_codes.append(err.exit_code)
327329
if not fail_at_end:
328330
break
329331

330332
# Report any errors if fail_at_end has been enabled
331333
if errors:
332-
raise Exit(code=1)
334+
# The error_exit_codes list contains the numbers 1, 2, or both if detailed-exitcode is enabled.
335+
# See https://developer.hashicorp.com/terraform/cli/commands/plan#detailed-exitcode fur further details.
336+
# If 1 is present, the plan failed for at least one path, hence we should return 1.
337+
# If all exit codes are 2, the plan succeeded for all paths, but there are changes, hence we should return 2.
338+
exit_code = 1 if 1 in error_exit_codes else 2
339+
raise Exit(code=exit_code)
333340

334341

335342
@click.command("apply")

0 commit comments

Comments
 (0)