summaryrefslogtreecommitdiff
path: root/core/modules/archive.sh
blob: 4459bfd4117edfcdd50629f1bf629704ad81b8f0 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash
# Module: archive
# Description: Comprime, descomprime y extrae archivos en remoto (tar, zip, gzip, bzip2)
# License: GPLv3
# Author: Luis GuLo
# Version: 1.6.0
# Dependencies: ssh, tar, gzip, bzip2, zip, unzip

archive_task() {
  local host="$1"; shift
  declare -A args
  local files=()

  for arg in "$@"; do
    key="${arg%%=*}"; value="${arg#*=}"
    [[ "$key" == "files" ]] && IFS=',' read -r -a files <<< "$value" || args["$key"]="$value"
  done

  local action="${args[action]}"
  local format="${args[format]:-tar}"
  local become="${args[become]:-false}"
  local prefix=""
  [[ "$become" == "true" ]] && prefix="sudo"

  local output="" archive="" dest=""
  case "$action" in
    compress) output="${args[output]}" ;;
    decompress|extract) archive="${args[archive]}"; dest="${args[dest]:-$(dirname "$archive")}" ;;
  esac

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

  if [[ "$action" == "extract" || "$action" == "decompress" ]]; then
    ssh "$host" "[ -d '$dest' ] || $prefix mkdir -p '$dest'" || {
      echo "$(render_msg "${tr[mkdir_fail]}" "dest=$dest")"
      return 1
    }
  fi

  case "$action" in
    compress)
      case "$format" in
        tar)
          ssh "$host" "$prefix tar -czf '$output' ${files[*]}" && echo "$(render_msg "${tr[compressed_tar]}" "output=$output")"
          ;;
        zip)
          ssh "$host" "$prefix zip -r '$output' ${files[*]}" && echo "$(render_msg "${tr[compressed_zip]}" "output=$output")"
          ;;
        gzip)
          for file in "${files[@]}"; do
            ssh "$host" "$prefix gzip -f '$file'" && echo "$(render_msg "${tr[compressed_gzip]}" "file=$file")"
          done
          ;;
        bzip2)
          for file in "${files[@]}"; do
            ssh "$host" "$prefix bzip2 -f '$file'" && echo "$(render_msg "${tr[compressed_bzip2]}" "file=$file")"
          done
          ;;
        *) echo "$(render_msg "${tr[unsupported_format]}" "format=$format")"; return 1 ;;
      esac
      ;;
    decompress)
      case "$format" in
        gzip)
          ssh "$host" "$prefix gunzip -f '$archive'" && echo "$(render_msg "${tr[decompressed_gzip]}" "archive=$archive")"
          ;;
        bzip2)
          ssh "$host" "$prefix bunzip2 -f '$archive'" && echo "$(render_msg "${tr[decompressed_bzip2]}" "archive=$archive")"
          ;;
        zip)
          ssh "$host" "$prefix unzip -o '$archive' -d '$dest'" && echo "$(render_msg "${tr[decompressed_zip]}" "dest=$dest")"
          ;;
        *) echo "$(render_msg "${tr[unsupported_format]}" "format=$format")"; return 1 ;;
      esac
      ;;
    extract)
      case "$format" in
        tar)
          if [[ ${#files[@]} -gt 0 ]]; then
            ssh "$host" "$prefix tar -xzf '$archive' -C '$dest' ${files[*]}" && echo "$(render_msg "${tr[extracted_tar]}" "dest=$dest")"
          else
            ssh "$host" "$prefix tar -xzf '$archive' -C '$dest'" && echo "$(render_msg "${tr[extracted_tar]}" "dest=$dest")"
          fi
          ;;
        zip)
          ssh "$host" "$prefix unzip -o '$archive' -d '$dest'" && echo "$(render_msg "${tr[extracted_zip]}" "dest=$dest")"
          ;;
        *) echo "$(render_msg "${tr[unsupported_format]}" "format=$format")"; return 1 ;;
      esac
      ;;
    *) echo "$(render_msg "${tr[unsupported_action]}" "action=$action")"; return 1 ;;
  esac
}

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

  for cmd in ssh tar gzip bzip2 zip unzip; do
    if ! command -v "$cmd" &> /dev/null; then
      echo "$(render_msg "${tr[missing_cmd]}" "cmd=$cmd")"
    else
      echo "$(render_msg "${tr[cmd_ok]}" "cmd=$cmd")"
    fi
  done
  return 0
}