Skip to content

Commit 2bbc903

Browse files
committed
fix(mixins): recurse into nested components and only update existing copies
Closes #47. The `mixins:update-all` task previously assumed a flat `./components/*` layout, so nested deployable instances like `aws-iam/delegated-tf-role` were never updated. It also unconditionally copied the mixin into every top-level component directory, which is wrong: a given mixin is not universally applicable (e.g. the `secrets.sops.tf` mixin only belongs in components that actually consume SOPS secrets). Copying it everywhere creates unused files that have to be deleted by hand. Changes: - Locate destinations by recursively finding existing copies of the named mixin under `./components/` (`find ... -type f -name <file>`). A component opts in to a mixin by having a copy of it on disk; this task only keeps those existing copies in sync. To adopt a mixin in a new component, copy it in manually once — that becomes the explicit opt-in and future `update-all` runs will track the template. - Recursion picks up nested instance layouts (e.g. `components/aws-iam/delegated-tf-role/<file>`) automatically. - Prune `.terraform/` and `.terragrunt-cache/` so vendored child-module copies that Terraform/Terragrunt drop on disk during init are not overwritten. - Set `dir: "{{.USER_WORKING_DIR}}"` so `./mixins/` and `./components/` resolve from where the user invoked `task`, not from the included taskfile's own directory. - Drop the `generates:` glob — it cannot express nested outputs and was causing Task to falsely treat the task as up-to-date. - Rewrite the loop to avoid `read -d ''` (not supported by Task's mvdan/sh interpreter).
1 parent dc2bbef commit 2bbc903

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

lib/mixins/Taskfile.yml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,30 @@ version: "3"
22

33
tasks:
44
update-all:
5-
desc: "Update a mixin across the components. Example: `task mixins:update-all -- context.tf`."
5+
# Updates (by overwriting) every existing copy of the named mixin under
6+
# ./components/ with the current ./mixins/<file> template.
7+
#
8+
# Directories opt in to a mixin by having a copy of it on disk; this task only
9+
# touches directories that already contain <file>. It does NOT add the mixin
10+
# to directories that don't already have it — adopt a mixin in a new component
11+
# by copying it in manually first as it needs to be an explicit choice, then
12+
# future `update-all` runs will keep it in sync.
13+
#
14+
# `.terraform/` and `.terragrunt-cache/` are pruned so we don't overwrite the
15+
# mixin inside vendored child-module copies that Terraform/Terragrunt drop on
16+
# disk during init.
17+
desc: "Update (by overwriting) every existing copy of a mixin under ./components/ with the current template. Does not add the mixin to directories that don't already have it. Example: `task mixins:update-all -- context.tf`."
18+
dir: "{{.USER_WORKING_DIR}}"
619
preconditions:
720
- sh: test -f ./mixins/{{.CLI_ARGS}}
821
msg: "File does not exist: ./mixins/{{.CLI_ARGS}}"
9-
- sh: test -d ./components && [ "$(ls -A ./components)" ]
10-
msg: ./components/ directory is empty or does not exist.
1122
sources:
1223
- ./mixins/{{.CLI_ARGS}}
13-
generates:
14-
- ./components/*/{{.CLI_ARGS}}
1524
cmds:
1625
- cmd: |
17-
for comp in ./components/*; do
18-
cp -v ./mixins/{{.CLI_ARGS}} $comp/{{.CLI_ARGS}}
19-
done;
26+
find ./components -type d \( -name .terraform -o -name .terragrunt-cache \) -prune -o -type f -name {{.CLI_ARGS}} -print | while IFS= read -r dest; do
27+
cp -v ./mixins/{{.CLI_ARGS}} "$dest"
28+
done
2029
silent: true
2130
2231
pull-sops:

0 commit comments

Comments
 (0)