summaryrefslogtreecommitdiff
path: root/core/modules/blockinfile.sh
diff options
context:
space:
mode:
authorluisgulo <luisgulo@gmail.com>2025-10-24 18:01:10 +0200
committerluisgulo <luisgulo@gmail.com>2025-10-24 18:01:10 +0200
commit533e79ba959143f0459431a486bfb85c56c72ddc (patch)
tree91974de1bbbdc4c51c76ed591fc5c6e02a3342b6 /core/modules/blockinfile.sh
parent45019c81cfd0fc1d18dce18cdfd5f127c6d61073 (diff)
Releasing code version 1.8.0
Diffstat (limited to 'core/modules/blockinfile.sh')
-rw-r--r--core/modules/blockinfile.sh79
1 files changed, 79 insertions, 0 deletions
diff --git a/core/modules/blockinfile.sh b/core/modules/blockinfile.sh
new file mode 100644
index 0000000..e42f5d8
--- /dev/null
+++ b/core/modules/blockinfile.sh
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+# Module: blockinfile
+# Description: Inserta o actualiza bloques de texto delimitados en archivos
+# Author: Luis GuLo
+# Version: 0.2.0
+# Dependencies: grep, sed, tee, awk
+
+blockinfile_task() {
+ local host="$1"; shift
+ declare -A args
+ for arg in "$@"; do
+ key="${arg%%=*}"; value="${arg#*=}"; args["$key"]="$value"
+ done
+
+ local path="${args[path]}"
+ local block="${args[block]}"
+ local marker="${args[marker]:-SHFLOW}"
+ local create="${args[create]:-true}"
+ local backup="${args[backup]:-true}"
+ local become="${args[become]}"
+ local prefix=""
+ [ "$become" = "true" ] && prefix="sudo"
+
+ local start="# BEGIN $marker"
+ local end="# END $marker"
+
+ # 🌐 Cargar traducciones
+ local lang="${shflow_vars[language]:-es}"
+ local trfile="$(dirname "${BASH_SOURCE[0]}")/blockinfile.tr.${lang}"
+ declare -A tr
+ if [[ -f "$trfile" ]]; then while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"; fi
+
+ if [[ ! -f "$path" ]]; then
+ if [[ "$create" == "true" ]]; then
+ echo "$(render_msg "${tr[creating]}" "path=$path")"
+ touch "$path"
+ else
+ echo "$(render_msg "${tr[missing_file]}" "path=$path")"
+ return 1
+ fi
+ fi
+
+ if [[ "$backup" == "true" ]]; then
+ cp "$path" "$path.bak"
+ echo "$(render_msg "${tr[backup]}" "path=$path")"
+ fi
+
+ if grep -q "$start" "$path"; then
+ echo "$(render_msg "${tr[replacing]}" "marker=$marker")"
+ $prefix sed -i "/$start/,/$end/d" "$path"
+ fi
+
+ echo "$(render_msg "${tr[inserting]}" "marker=$marker")"
+ {
+ echo "$start"
+ echo "$block"
+ echo "$end"
+ } | $prefix tee -a "$path" > /dev/null
+}
+
+check_dependencies_blockinfile() {
+ local lang="${shflow_vars[language]:-es}"
+ local trfile="$(dirname "${BASH_SOURCE[0]}")/blockinfile.tr.${lang}"
+ declare -A tr
+ if [[ -f "$trfile" ]]; then while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"; fi
+
+ local missing=()
+ for cmd in grep sed tee awk; do
+ command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd")
+ done
+
+ if [[ ${#missing[@]} -gt 0 ]]; then
+ echo "$(render_msg "${tr[missing_deps]}" "cmds=${missing[*]}")"
+ return 1
+ fi
+
+ echo "${tr[deps_ok]:-✅ [blockinfile] Todas las dependencias están disponibles}"
+ return 0
+}