11#! /usr/bin/env bash
2- # Usage: scripts/tf-all.sh <plan|apply> [--auto-approve]
2+ # Usage: scripts/tf-all.sh <plan|apply> [--auto-approve|<make-target> ]
33#
4- # plan Preview changes across all modules in dependency order
5- # apply Apply changes, prompting per module
6- # apply --auto-approve Apply all changes without prompting
4+ # plan Preview all modules (make target: all)
5+ # plan ci-plan Preview CI-scoped modules only (make target: ci-plan)
6+ # apply Apply all modules, prompting per module
7+ # apply --auto-approve Apply all modules without prompting
78set -euo pipefail
89
910REPO_ROOT=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) /.." && pwd) "
10- TOTAL_MODULES=15 # keep in sync with number of targets called in Makefile `all` chain
1111
1212# ── Parse args ────────────────────────────────────────────────────────────────
1313CMD=" ${1:- } "
1414AUTO_APPROVE=0
15+ MAKE_TARGET=" all"
1516
1617if [[ " $CMD " == " apply" && " ${2:- } " == " --auto-approve" ]]; then
1718 AUTO_APPROVE=1
19+ elif [[ " $CMD " == " plan" && -n " ${2:- } " ]]; then
20+ MAKE_TARGET=" ${2} "
1821fi
1922
2023if [[ " $CMD " != " plan" && " $CMD " != " apply" ]]; then
21- echo " Usage: $0 <plan|apply> [--auto-approve]"
24+ echo " Usage: $0 <plan|apply> [--auto-approve|<make-target> ]"
2225 echo " "
23- echo " plan Preview changes across all modules"
24- echo " apply Apply changes, prompting per module"
25- echo " apply --auto-approve Apply all changes without prompting"
26+ echo " plan Preview all modules"
27+ echo " plan ci-plan Preview CI-scoped modules only"
28+ echo " apply Apply all modules, prompting per module"
29+ echo " apply --auto-approve Apply all modules without prompting"
2630 exit 1
2731fi
2832
2933# ── Progress tracking ─────────────────────────────────────────────────────────
3034PROGRESS_DIR=$( mktemp -d /tmp/tf-progress.XXXXXX)
3135trap ' rm -rf "$PROGRESS_DIR"' EXIT
3236
33- printf ' 0' > " $PROGRESS_DIR /counter"
37+ # Derive total dynamically so it stays correct for any make target
38+ TOTAL_MODULES=$( make --dry-run " $MAKE_TARGET " CMD=plan 2> /dev/null | grep -c ' tf-module\.sh' || echo 0)
39+
40+ printf ' 0' > " $PROGRESS_DIR /counter"
3441printf ' %s' " $TOTAL_MODULES " > " $PROGRESS_DIR /total"
3542touch " $PROGRESS_DIR /results"
3643
@@ -39,17 +46,30 @@ export TF_PROGRESS_DIR="$PROGRESS_DIR"
3946# ── Run ───────────────────────────────────────────────────────────────────────
4047cd " $REPO_ROOT "
4148set +e
42- # -k (keep going) in plan mode: continue past individual module failures so all
43- # modules are planned and the summary shows the full picture . Apply mode stays
49+ # -k (keep going) in plan mode: tf-module.sh exits 0 for "changes detected" so
50+ # Make prerequisites are never broken by pending diffs . Apply mode stays
4451# fail-fast — downstream modules depend on upstream state being applied first.
4552if [[ " $CMD " == " plan" ]]; then
46- make -k all CMD=" $CMD " AUTO_APPROVE=" $AUTO_APPROVE "
53+ make -k " $MAKE_TARGET " CMD=" $CMD " AUTO_APPROVE=" $AUTO_APPROVE "
4754else
48- make all CMD=" $CMD " AUTO_APPROVE=" $AUTO_APPROVE "
55+ make " $MAKE_TARGET " CMD=" $CMD " AUTO_APPROVE=" $AUTO_APPROVE "
4956fi
5057MAKE_EXIT=$?
5158set -e
5259
60+ # ── Synthesize exit from results (plan mode only) ─────────────────────────────
61+ # tf-module.sh exits 0 for both clean and drift to keep Make deps intact.
62+ # Re-derive the correct signal here: 1=error, 2=drift, 0=all clean.
63+ if [[ " $CMD " == " plan" ]]; then
64+ if grep -q ' ^✗' " $PROGRESS_DIR /results" 2> /dev/null; then
65+ MAKE_EXIT=1
66+ elif grep -q ' ^~' " $PROGRESS_DIR /results" 2> /dev/null; then
67+ MAKE_EXIT=2
68+ else
69+ MAKE_EXIT=0
70+ fi
71+ fi
72+
5373# ── Summary ───────────────────────────────────────────────────────────────────
5474echo " "
5575echo " ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
0 commit comments