summaryrefslogtreecommitdiff
path: root/core/modules/git.sh
blob: 0fa22f1edca90b49a1248e7b19472e5555f8f749 (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
#!/bin/bash
# Module: git
# Description: Gestiona repositorios Git en hosts remotos (clone, pull, checkout, fetch-file)
# License: GPLv3
# Author: Luis GuLo
# Version: 1.2.0
# Dependencies: ssh, git, curl, tar

git_task() {
  local host="$1"; shift
  declare -A args
  for arg in "$@"; do key="${arg%%=*}"; value="${arg#*=}"; args["$key"]="$value"; done

  local action="${args[action]}"
  local repo="${args[repo]}"
  local dest="${args[dest]}"
  local branch="${args[branch]}"
  local file_path="${args[file_path]}"
  local become="${args[become]}"
  local prefix=""
  [ "$become" = "true" ] && prefix="sudo"

  # 🌐 Cargar traducciones
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/git.tr.${lang}"
  declare -A tr
  if [[ -f "$trfile" ]]; then
    while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"
  fi

  case "$action" in
    clone)
      echo "$(render_msg "${tr[cloning]}" "repo=$repo" "dest=$dest")"
      ssh "$host" "[ -d '$dest/.git' ] || $prefix git clone '$repo' '$dest'"
      ;;
    pull)
      echo "$(render_msg "${tr[pulling]}" "dest=$dest")"
      ssh "$host" "[ -d '$dest/.git' ] && cd '$dest' && $prefix git pull"
      ;;
    checkout)
      echo "$(render_msg "${tr[checkout]}" "branch=$branch" "dest=$dest")"
      ssh "$host" "[ -d '$dest/.git' ] && cd '$dest' && $prefix git checkout '$branch'"
      ;;
    fetch-file)
      echo "$(render_msg "${tr[fetching]}" "file=$file_path" "repo=$repo" "branch=$branch")"
      fetch_file_from_repo "$host" "$repo" "$branch" "$file_path" "$dest" "$become"
      ;;
    *)
      echo "$(render_msg "${tr[unsupported]}" "action=$action")"
      return 1
      ;;
  esac
}

fetch_file_from_repo() {
  local host="$1"
  local repo="$2"
  local branch="$3"
  local file_path="$4"
  local dest="$5"
  local become="$6"
  local prefix=""
  [ "$become" = "true" ] && prefix="sudo"

  ssh "$host" "$prefix git archive --remote='$repo' '$branch' '$file_path' | $prefix tar -xO > '$dest'"
}

check_dependencies_git() {
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/git.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 ssh git curl tar; do
    command -v "$cmd" &> /dev/null || missing+=("$cmd")
  done

  if [[ ${#missing[@]} -gt 0 ]]; then
    echo "$(render_msg "${tr[missing_deps]}" "cmds=${missing[*]}")"
    return 1
  fi

  echo "${tr[deps_ok]:-✅ [git] Todas las dependencias están disponibles}"
  return 0
}