summaryrefslogtreecommitdiff
path: root/core/modules/lineinfile.sh
blob: aded42c153ab0b699713198be07ad8690e919fdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash
# Module: lineinfile
# Description: Asegura la presencia o reemplazo de una línea en un archivo
# Author: Luis GuLo
# Version: 0.2.0
# Dependencies: grep, sed, tee, awk

lineinfile_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 line="${args[line]}"
  local regexp="${args[regexp]}"
  local insert_after="${args[insert_after]}"
  local create="${args[create]:-true}"
  local backup="${args[backup]:-true}"
  local become="${args[become]}"
  local prefix=""
  [ "$become" = "true" ] && prefix="sudo"

  # 🌐 Cargar traducciones
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/lineinfile.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 [[ -n "$regexp" && $(grep -Eq "$regexp" "$path" && echo "yes") == "yes" ]]; then
    echo "$(render_msg "${tr[replacing]}" "regexp=$regexp")"
    $prefix sed -i "s|$regexp|$line|" "$path"
    return 0
  fi

  if [[ -n "$insert_after" && $(grep -q "$insert_after" "$path" && echo "yes") == "yes" ]]; then
    echo "$(render_msg "${tr[inserting]}" "after=$insert_after")"
    $prefix sed -i "/$insert_after/a $line" "$path"
    return 0
  fi

  if grep -Fxq "$line" "$path"; then
    echo "$(render_msg "${tr[exists]}" "line=$line")"
    return 0
  fi

  echo "${tr[appending]:-➕ [lineinfile] Añadiendo línea al final}"
  echo "$line" | $prefix tee -a "$path" > /dev/null
}

check_dependencies_lineinfile() {
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/lineinfile.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]:-✅ [lineinfile] Todas las dependencias están disponibles}"
  return 0
}