From 5a837869037dc7216fcbc354250cb3657859618f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Groothuis?= Date: Thu, 23 Oct 2025 14:21:11 +0200 Subject: [PATCH] chore(bootstrap): Added templating --- init-app.sh | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/init-app.sh b/init-app.sh index ca1c651..5b4c596 100755 --- a/init-app.sh +++ b/init-app.sh @@ -84,7 +84,7 @@ fi TEMPLATE_DIR="${SCRIPT_DIR}/.templates" APP_PROJECT_SRC="${TEMPLATE_DIR}/app-project.yaml" APPLICATION_SRC="${TEMPLATE_DIR}/application.yaml" -KUSTOMIZATION_SRC="${TEMPLATE_DIR}/kustomization.yaml} +KUSTOMIZATION_SRC="${TEMPLATE_DIR}/kustomization.yaml" missing=false for f in "$APP_PROJECT_SRC" "$APPLICATION_SRC" "$KUSTOMIZATION_SRC"; do @@ -157,47 +157,48 @@ else indent="$(grep -E '^[[:space:]]*-[[:space:]]' "$PARENT_KUSTOMIZATION" | sed -n '1s/^\([[:space:]]*\)-.*/\1/p')" [[ -z "${indent:-}" ]] && indent=" " - # Work within the most recent 'resources:' block + # Work within the most recent 'resources:' block (if any) last_res_start_line="$(nl -ba "$PARENT_KUSTOMIZATION" | awk '/^[[:space:]]*[0-9]+\s+resources:[[:space:]]*$/ { line=$1 } END { if (line!="") print line }')" if [[ -n "${last_res_start_line:-}" ]]; then + # Slice parent from resources start to EOF tmp_slice="$(mktemp)" tail -n +"$last_res_start_line" "$PARENT_KUSTOMIZATION" > "$tmp_slice" + # In the slice, find last '-' resource line number slice_last_line="$(nl -ba "$tmp_slice" \ | awk '/^[[:space:]]*[0-9]+\s+[[:space:]]*-[[:space:]]/ { print $1 }' \ | tail -n1)" if [[ -n "${slice_last_line:-}" ]]; then + # Map slice line back to parent absolute line abs_last_line=$(( last_res_start_line + slice_last_line - 1 )) + + # Build new file via tmp_parent; avoid arithmetic in command args tmp_parent="$(mktemp)" - { - head -n "$abs_last_line" "$PARENT_KUSTOMIZATION" - printf "%s- %s\n" "$indent" "$APPLICATION_NAME" - tail -n +"$((abs_last_line + 1))" "$PARENT_KUSTOMIZATION" - } > "$tmp_parent" + head -n "$abs_last_line" "$PARENT_KUSTOMIZATION" > "$tmp_parent" + printf "%s- %s\n" "$indent" "$APPLICATION_NAME" >> "$tmp_parent" + next_line=$(( abs_last_line + 1 )) + tail -n +"$next_line" "$PARENT_KUSTOMIZATION" >> "$tmp_parent" mv "$tmp_parent" "$PARENT_KUSTOMIZATION" echo "Updated parent kustomization.yaml: appended '${APPLICATION_NAME}' to resources." else # No items yet → insert directly after the resources key line tmp_parent="$(mktemp)" - { - head -n "$last_res_start_line" "$PARENT_KUSTOMIZATION" - printf "%s- %s\n" "$indent" "$APPLICATION_NAME" - tail -n +"$((last_res_start_line + 1))" "$PARENT_KUSTOMIZATION" - } > "$tmp_parent" + head -n "$last_res_start_line" "$PARENT_KUSTOMIZATION" > "$tmp_parent" + printf "%s- %s\n" "$indent" "$APPLICATION_NAME" >> "$tmp_parent" + next_line=$(( last_res_start_line + 1 )) + tail -n +"$next_line" "$PARENT_KUSTOMIZATION" >> "$tmp_parent" mv "$tmp_parent" "$PARENT_KUSTOMIZATION" echo "Updated parent kustomization.yaml: added first resource '${APPLICATION_NAME}' under resources." fi rm -f "$tmp_slice" else - # No resources key exists at all → create section at the end ONCE + # No resources key exists at all → create section at the end ONCE via tmp_parent tmp_parent="$(mktemp)" - { - cat "$PARENT_KUSTOMIZATION" - printf "\nresources:\n%s- %s\n" "$indent" "$APPLICATION_NAME" - } > "$tmp_parent" + cat "$PARENT_KUSTOMIZATION" > "$tmp_parent" + printf "\nresources:\n%s- %s\n" "$indent" "$APPLICATION_NAME" >> "$tmp_parent" mv "$tmp_parent" "$PARENT_KUSTOMIZATION" echo "Updated parent kustomization.yaml: created resources section with '${APPLICATION_NAME}'." fi