chore(bootstrap): Added templating
This commit is contained in:
35
init-app.sh
35
init-app.sh
@@ -84,7 +84,7 @@ fi
|
|||||||
TEMPLATE_DIR="${SCRIPT_DIR}/.templates"
|
TEMPLATE_DIR="${SCRIPT_DIR}/.templates"
|
||||||
APP_PROJECT_SRC="${TEMPLATE_DIR}/app-project.yaml"
|
APP_PROJECT_SRC="${TEMPLATE_DIR}/app-project.yaml"
|
||||||
APPLICATION_SRC="${TEMPLATE_DIR}/application.yaml"
|
APPLICATION_SRC="${TEMPLATE_DIR}/application.yaml"
|
||||||
KUSTOMIZATION_SRC="${TEMPLATE_DIR}/kustomization.yaml}
|
KUSTOMIZATION_SRC="${TEMPLATE_DIR}/kustomization.yaml"
|
||||||
|
|
||||||
missing=false
|
missing=false
|
||||||
for f in "$APP_PROJECT_SRC" "$APPLICATION_SRC" "$KUSTOMIZATION_SRC"; do
|
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')"
|
indent="$(grep -E '^[[:space:]]*-[[:space:]]' "$PARENT_KUSTOMIZATION" | sed -n '1s/^\([[:space:]]*\)-.*/\1/p')"
|
||||||
[[ -z "${indent:-}" ]] && indent=" "
|
[[ -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 }')"
|
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
|
if [[ -n "${last_res_start_line:-}" ]]; then
|
||||||
|
# Slice parent from resources start to EOF
|
||||||
tmp_slice="$(mktemp)"
|
tmp_slice="$(mktemp)"
|
||||||
tail -n +"$last_res_start_line" "$PARENT_KUSTOMIZATION" > "$tmp_slice"
|
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" \
|
slice_last_line="$(nl -ba "$tmp_slice" \
|
||||||
| awk '/^[[:space:]]*[0-9]+\s+[[:space:]]*-[[:space:]]/ { print $1 }' \
|
| awk '/^[[:space:]]*[0-9]+\s+[[:space:]]*-[[:space:]]/ { print $1 }' \
|
||||||
| tail -n1)"
|
| tail -n1)"
|
||||||
|
|
||||||
if [[ -n "${slice_last_line:-}" ]]; then
|
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 ))
|
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)"
|
tmp_parent="$(mktemp)"
|
||||||
{
|
head -n "$abs_last_line" "$PARENT_KUSTOMIZATION" > "$tmp_parent"
|
||||||
head -n "$abs_last_line" "$PARENT_KUSTOMIZATION"
|
printf "%s- %s\n" "$indent" "$APPLICATION_NAME" >> "$tmp_parent"
|
||||||
printf "%s- %s\n" "$indent" "$APPLICATION_NAME"
|
next_line=$(( abs_last_line + 1 ))
|
||||||
tail -n +"$((abs_last_line + 1))" "$PARENT_KUSTOMIZATION"
|
tail -n +"$next_line" "$PARENT_KUSTOMIZATION" >> "$tmp_parent"
|
||||||
} > "$tmp_parent"
|
|
||||||
mv "$tmp_parent" "$PARENT_KUSTOMIZATION"
|
mv "$tmp_parent" "$PARENT_KUSTOMIZATION"
|
||||||
echo "Updated parent kustomization.yaml: appended '${APPLICATION_NAME}' to resources."
|
echo "Updated parent kustomization.yaml: appended '${APPLICATION_NAME}' to resources."
|
||||||
else
|
else
|
||||||
# No items yet → insert directly after the resources key line
|
# No items yet → insert directly after the resources key line
|
||||||
tmp_parent="$(mktemp)"
|
tmp_parent="$(mktemp)"
|
||||||
{
|
head -n "$last_res_start_line" "$PARENT_KUSTOMIZATION" > "$tmp_parent"
|
||||||
head -n "$last_res_start_line" "$PARENT_KUSTOMIZATION"
|
printf "%s- %s\n" "$indent" "$APPLICATION_NAME" >> "$tmp_parent"
|
||||||
printf "%s- %s\n" "$indent" "$APPLICATION_NAME"
|
next_line=$(( last_res_start_line + 1 ))
|
||||||
tail -n +"$((last_res_start_line + 1))" "$PARENT_KUSTOMIZATION"
|
tail -n +"$next_line" "$PARENT_KUSTOMIZATION" >> "$tmp_parent"
|
||||||
} > "$tmp_parent"
|
|
||||||
mv "$tmp_parent" "$PARENT_KUSTOMIZATION"
|
mv "$tmp_parent" "$PARENT_KUSTOMIZATION"
|
||||||
echo "Updated parent kustomization.yaml: added first resource '${APPLICATION_NAME}' under resources."
|
echo "Updated parent kustomization.yaml: added first resource '${APPLICATION_NAME}' under resources."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
rm -f "$tmp_slice"
|
rm -f "$tmp_slice"
|
||||||
else
|
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)"
|
tmp_parent="$(mktemp)"
|
||||||
{
|
cat "$PARENT_KUSTOMIZATION" > "$tmp_parent"
|
||||||
cat "$PARENT_KUSTOMIZATION"
|
printf "\nresources:\n%s- %s\n" "$indent" "$APPLICATION_NAME" >> "$tmp_parent"
|
||||||
printf "\nresources:\n%s- %s\n" "$indent" "$APPLICATION_NAME"
|
|
||||||
} > "$tmp_parent"
|
|
||||||
mv "$tmp_parent" "$PARENT_KUSTOMIZATION"
|
mv "$tmp_parent" "$PARENT_KUSTOMIZATION"
|
||||||
echo "Updated parent kustomization.yaml: created resources section with '${APPLICATION_NAME}'."
|
echo "Updated parent kustomization.yaml: created resources section with '${APPLICATION_NAME}'."
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user