omni-catalyst 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1137 -0
  3. package/core/bin/core +85 -0
  4. package/core/bin/omni +21 -0
  5. package/core/cli/commands/--version.sh +8 -0
  6. package/core/cli/commands/brain.sh +1549 -0
  7. package/core/cli/commands/doctor.sh +834 -0
  8. package/core/cli/commands/env.sh +226 -0
  9. package/core/cli/commands/init.sh +1266 -0
  10. package/core/cli/commands/install.sh +759 -0
  11. package/core/cli/commands/list.sh +397 -0
  12. package/core/cli/commands/open.sh +71 -0
  13. package/core/cli/commands/pg.sh +761 -0
  14. package/core/cli/commands/reinstall.sh +694 -0
  15. package/core/cli/commands/show.sh +90 -0
  16. package/core/cli/commands/uninstall.sh +700 -0
  17. package/core/cli/commands/update.sh +754 -0
  18. package/core/cli/commands/voice.sh +170 -0
  19. package/core/cli/core.sh +82 -0
  20. package/core/cli/omni.sh +634 -0
  21. package/core/modules/ai.sh +152 -0
  22. package/core/modules/auto.sh +90 -0
  23. package/core/modules/db.sh +94 -0
  24. package/core/modules/deploy.sh +50 -0
  25. package/core/modules/dev.sh +126 -0
  26. package/core/modules/editor.sh +101 -0
  27. package/core/modules/lang.sh +100 -0
  28. package/core/modules/npm.sh +122 -0
  29. package/core/modules/shell.sh +312 -0
  30. package/core/modules/ui.sh +115 -0
  31. package/core/tools/ai/all.sh +63 -0
  32. package/core/tools/ai/antigravity-cli/README.md +45 -0
  33. package/core/tools/ai/antigravity-cli/bin/agy +28 -0
  34. package/core/tools/ai/antigravity-cli/helper/agy_helper.c +39 -0
  35. package/core/tools/ai/antigravity-cli/install.sh +469 -0
  36. package/core/tools/ai/claude-code/README.md +45 -0
  37. package/core/tools/ai/claude-code/bin/claude +29 -0
  38. package/core/tools/ai/claude-code/helper/claude_helper.c +42 -0
  39. package/core/tools/ai/claude-code/install.sh +278 -0
  40. package/core/tools/ai/codegraph/README.md +43 -0
  41. package/core/tools/ai/codegraph/bin/codegraph +3 -0
  42. package/core/tools/ai/codegraph/install.sh +149 -0
  43. package/core/tools/ai/codex/README.md +44 -0
  44. package/core/tools/ai/codex/install.sh +104 -0
  45. package/core/tools/ai/command-code/README.md +62 -0
  46. package/core/tools/ai/command-code/install.sh +136 -0
  47. package/core/tools/ai/engram/README.md +44 -0
  48. package/core/tools/ai/engram/install.sh +127 -0
  49. package/core/tools/ai/freebuff/README.md +52 -0
  50. package/core/tools/ai/freebuff/bin/freebuff +28 -0
  51. package/core/tools/ai/freebuff/helper/freebuff_helper.c +47 -0
  52. package/core/tools/ai/freebuff/install.sh +301 -0
  53. package/core/tools/ai/gemini-cli/README.md +46 -0
  54. package/core/tools/ai/gemini-cli/install.sh +111 -0
  55. package/core/tools/ai/gentle-ai/README.md +42 -0
  56. package/core/tools/ai/gentle-ai/install.sh +253 -0
  57. package/core/tools/ai/gentle-ai/termux-patches.go +256 -0
  58. package/core/tools/ai/gga/README.md +49 -0
  59. package/core/tools/ai/gga/install.sh +134 -0
  60. package/core/tools/ai/hermes-agent/README.md +50 -0
  61. package/core/tools/ai/hermes-agent/install.sh +80 -0
  62. package/core/tools/ai/heygen/README.md +24 -0
  63. package/core/tools/ai/heygen/install.sh +126 -0
  64. package/core/tools/ai/kilocode-cli/README.md +45 -0
  65. package/core/tools/ai/kilocode-cli/bin/kilocode +21 -0
  66. package/core/tools/ai/kilocode-cli/helper/kilocode_helper.c +47 -0
  67. package/core/tools/ai/kilocode-cli/install.sh +179 -0
  68. package/core/tools/ai/kimchi-code/README.md +8 -0
  69. package/core/tools/ai/kimchi-code/install.sh +120 -0
  70. package/core/tools/ai/kimi-code/README.md +45 -0
  71. package/core/tools/ai/kimi-code/install.sh +111 -0
  72. package/core/tools/ai/kiro-cli/README.md +23 -0
  73. package/core/tools/ai/kiro-cli/install.sh +127 -0
  74. package/core/tools/ai/mimocode/README.md +43 -0
  75. package/core/tools/ai/mimocode/bin/mimo +28 -0
  76. package/core/tools/ai/mimocode/helper/mimocode_helper.c +47 -0
  77. package/core/tools/ai/mimocode/install.sh +287 -0
  78. package/core/tools/ai/minimax-cli/README.md +45 -0
  79. package/core/tools/ai/minimax-cli/install.sh +111 -0
  80. package/core/tools/ai/mistral-vibe/README.md +46 -0
  81. package/core/tools/ai/mistral-vibe/install.sh +119 -0
  82. package/core/tools/ai/odysseus/README.md +35 -0
  83. package/core/tools/ai/odysseus/install.sh +163 -0
  84. package/core/tools/ai/ollama/README.md +44 -0
  85. package/core/tools/ai/ollama/install.sh +78 -0
  86. package/core/tools/ai/openclaude/README.md +46 -0
  87. package/core/tools/ai/openclaude/install.sh +110 -0
  88. package/core/tools/ai/openclaw/README.md +47 -0
  89. package/core/tools/ai/openclaw/install.sh +112 -0
  90. package/core/tools/ai/opencode/README.md +51 -0
  91. package/core/tools/ai/opencode/bin/opencode +28 -0
  92. package/core/tools/ai/opencode/helper/opencode_helper.c +57 -0
  93. package/core/tools/ai/opencode/install.sh +290 -0
  94. package/core/tools/ai/pi/README.md +46 -0
  95. package/core/tools/ai/pi/install.sh +104 -0
  96. package/core/tools/ai/qwen-code/README.md +45 -0
  97. package/core/tools/ai/qwen-code/install.sh +111 -0
  98. package/core/tools/ai/seedance/README.md +22 -0
  99. package/core/tools/ai/seedance/install.sh +134 -0
  100. package/core/tools/ai/veo3/README.md +36 -0
  101. package/core/tools/ai/veo3/install.sh +151 -0
  102. package/core/tools/auto/all.sh +57 -0
  103. package/core/tools/auto/n8n/README.md +47 -0
  104. package/core/tools/auto/n8n/install.sh +102 -0
  105. package/core/tools/db/all.sh +60 -0
  106. package/core/tools/db/mariadb/README.md +43 -0
  107. package/core/tools/db/mariadb/install.sh +65 -0
  108. package/core/tools/db/mongodb/README.md +42 -0
  109. package/core/tools/db/mongodb/install.sh +73 -0
  110. package/core/tools/db/postgresql/README.md +45 -0
  111. package/core/tools/db/postgresql/install.sh +65 -0
  112. package/core/tools/db/sqlite/README.md +44 -0
  113. package/core/tools/db/sqlite/install.sh +65 -0
  114. package/core/tools/deploy/all.sh +39 -0
  115. package/core/tools/deploy/netlify/install.sh +41 -0
  116. package/core/tools/deploy/railway/install.sh +47 -0
  117. package/core/tools/deploy/vercel/install.sh +43 -0
  118. package/core/tools/dev/all.sh +75 -0
  119. package/core/tools/dev/bat/README.md +44 -0
  120. package/core/tools/dev/bat/install.sh +82 -0
  121. package/core/tools/dev/bc/README.md +44 -0
  122. package/core/tools/dev/bc/install.sh +82 -0
  123. package/core/tools/dev/cloudflared/README.md +43 -0
  124. package/core/tools/dev/cloudflared/install.sh +82 -0
  125. package/core/tools/dev/curl/README.md +43 -0
  126. package/core/tools/dev/curl/install.sh +82 -0
  127. package/core/tools/dev/fzf/README.md +44 -0
  128. package/core/tools/dev/fzf/install.sh +82 -0
  129. package/core/tools/dev/gh/README.md +43 -0
  130. package/core/tools/dev/gh/install.sh +82 -0
  131. package/core/tools/dev/html2text/README.md +44 -0
  132. package/core/tools/dev/html2text/install.sh +82 -0
  133. package/core/tools/dev/imagemagick/README.md +44 -0
  134. package/core/tools/dev/imagemagick/install.sh +82 -0
  135. package/core/tools/dev/jq/README.md +44 -0
  136. package/core/tools/dev/jq/install.sh +82 -0
  137. package/core/tools/dev/lsd/README.md +44 -0
  138. package/core/tools/dev/lsd/install.sh +82 -0
  139. package/core/tools/dev/make/README.md +44 -0
  140. package/core/tools/dev/make/install.sh +82 -0
  141. package/core/tools/dev/ncurses/README.md +42 -0
  142. package/core/tools/dev/ncurses/install.sh +82 -0
  143. package/core/tools/dev/proot/README.md +43 -0
  144. package/core/tools/dev/proot/install.sh +82 -0
  145. package/core/tools/dev/shfmt/README.md +44 -0
  146. package/core/tools/dev/shfmt/install.sh +82 -0
  147. package/core/tools/dev/tmate/README.md +44 -0
  148. package/core/tools/dev/tmate/install.sh +82 -0
  149. package/core/tools/dev/translate/README.md +44 -0
  150. package/core/tools/dev/translate/install.sh +82 -0
  151. package/core/tools/dev/tree/README.md +44 -0
  152. package/core/tools/dev/tree/install.sh +82 -0
  153. package/core/tools/dev/udocker/README.md +44 -0
  154. package/core/tools/dev/udocker/install.sh +82 -0
  155. package/core/tools/dev/wget/README.md +44 -0
  156. package/core/tools/dev/wget/install.sh +82 -0
  157. package/core/tools/editor/all.sh +58 -0
  158. package/core/tools/editor/neovim/README.md +43 -0
  159. package/core/tools/editor/neovim/install.sh +66 -0
  160. package/core/tools/editor/nvchad/README.md +45 -0
  161. package/core/tools/editor/nvchad/install.sh +110 -0
  162. package/core/tools/lang/all.sh +63 -0
  163. package/core/tools/lang/clang/README.md +43 -0
  164. package/core/tools/lang/clang/install.sh +79 -0
  165. package/core/tools/lang/golang/README.md +43 -0
  166. package/core/tools/lang/golang/install.sh +79 -0
  167. package/core/tools/lang/nodejs/README.md +43 -0
  168. package/core/tools/lang/nodejs/install.sh +79 -0
  169. package/core/tools/lang/perl/README.md +43 -0
  170. package/core/tools/lang/perl/install.sh +79 -0
  171. package/core/tools/lang/php/README.md +43 -0
  172. package/core/tools/lang/php/install.sh +79 -0
  173. package/core/tools/lang/python/README.md +43 -0
  174. package/core/tools/lang/python/install.sh +79 -0
  175. package/core/tools/lang/rust/README.md +43 -0
  176. package/core/tools/lang/rust/install.sh +79 -0
  177. package/core/tools/npm/all.sh +67 -0
  178. package/core/tools/npm/live-server/README.md +43 -0
  179. package/core/tools/npm/live-server/install.sh +95 -0
  180. package/core/tools/npm/localtunnel/README.md +43 -0
  181. package/core/tools/npm/localtunnel/install.sh +111 -0
  182. package/core/tools/npm/markserv/README.md +43 -0
  183. package/core/tools/npm/markserv/install.sh +94 -0
  184. package/core/tools/npm/ncu/README.md +43 -0
  185. package/core/tools/npm/ncu/install.sh +94 -0
  186. package/core/tools/npm/nestjs/README.md +43 -0
  187. package/core/tools/npm/nestjs/install.sh +94 -0
  188. package/core/tools/npm/ngrok/README.md +44 -0
  189. package/core/tools/npm/ngrok/install.sh +94 -0
  190. package/core/tools/npm/prettier/README.md +43 -0
  191. package/core/tools/npm/prettier/install.sh +94 -0
  192. package/core/tools/npm/psqlformat/README.md +43 -0
  193. package/core/tools/npm/psqlformat/install.sh +94 -0
  194. package/core/tools/npm/turbopack/README.md +8 -0
  195. package/core/tools/npm/turbopack/install.sh +41 -0
  196. package/core/tools/npm/typescript/README.md +43 -0
  197. package/core/tools/npm/typescript/install.sh +94 -0
  198. package/core/tools/npm/vercel/README.md +43 -0
  199. package/core/tools/npm/vercel/install.sh +94 -0
  200. package/core/tools/shell/all.sh +67 -0
  201. package/core/tools/shell/better-npm/README.md +43 -0
  202. package/core/tools/shell/better-npm/install.sh +85 -0
  203. package/core/tools/shell/fzf-tab/README.md +44 -0
  204. package/core/tools/shell/fzf-tab/install.sh +85 -0
  205. package/core/tools/shell/history-substring/README.md +43 -0
  206. package/core/tools/shell/history-substring/install.sh +85 -0
  207. package/core/tools/shell/powerlevel10k/README.md +43 -0
  208. package/core/tools/shell/powerlevel10k/install.sh +85 -0
  209. package/core/tools/shell/you-should-use/README.md +43 -0
  210. package/core/tools/shell/you-should-use/install.sh +85 -0
  211. package/core/tools/shell/zsh-autopair/README.md +44 -0
  212. package/core/tools/shell/zsh-autopair/install.sh +85 -0
  213. package/core/tools/shell/zsh-autosuggestions/README.md +43 -0
  214. package/core/tools/shell/zsh-autosuggestions/install.sh +85 -0
  215. package/core/tools/shell/zsh-completions/README.md +43 -0
  216. package/core/tools/shell/zsh-completions/install.sh +85 -0
  217. package/core/tools/shell/zsh-defer/README.md +43 -0
  218. package/core/tools/shell/zsh-defer/install.sh +85 -0
  219. package/core/tools/shell/zsh-syntax-highlighting/README.md +43 -0
  220. package/core/tools/shell/zsh-syntax-highlighting/install.sh +72 -0
  221. package/core/tools/ui/all.sh +61 -0
  222. package/core/tools/ui/banner/README.md +42 -0
  223. package/core/tools/ui/banner/install.sh +151 -0
  224. package/core/tools/ui/cursor/README.md +42 -0
  225. package/core/tools/ui/cursor/install.sh +58 -0
  226. package/core/tools/ui/extra-keys/README.md +43 -0
  227. package/core/tools/ui/extra-keys/install.sh +62 -0
  228. package/core/tools/ui/font/README.md +43 -0
  229. package/core/tools/ui/font/install.sh +63 -0
  230. package/core/utils/banner.sh +519 -0
  231. package/core/utils/bootstrap.sh +25 -0
  232. package/core/utils/colors.sh +31 -0
  233. package/core/utils/dialogrc +30 -0
  234. package/core/utils/env.sh +53 -0
  235. package/core/utils/log.sh +598 -0
  236. package/install.sh +272 -0
  237. package/package.json +43 -0
@@ -0,0 +1,761 @@
1
+ #!/usr/bin/env bash
2
+
3
+ import "@/utils/log"
4
+ import "@/utils/colors"
5
+
6
+ # Variables de PostgreSQL
7
+ PG_DATA="$PREFIX/var/lib/postgresql"
8
+ PG_LOG="$OMNI_CACHE/postgresql.log"
9
+ PG_USER="postgres"
10
+
11
+ # Mostrar ayuda
12
+ pg_help() {
13
+ echo
14
+ box "Omni PostgreSQL Manager"
15
+ echo
16
+ log_info "Usage: omni pg <command> [options]"
17
+ echo
18
+ separator_section "Available Commands"
19
+ echo
20
+ printf " ${D_CYAN}%-12s${NC} %s\n" "start" "Start PostgreSQL server"
21
+ printf " ${D_CYAN}%-12s${NC} %s\n" "stop" "Stop PostgreSQL server"
22
+ printf " ${D_CYAN}%-12s${NC} %s\n" "restart" "Restart PostgreSQL server"
23
+ printf " ${D_CYAN}%-12s${NC} %s\n" "status" "Check PostgreSQL status"
24
+ printf " ${D_CYAN}%-12s${NC} %s\n" "init" "Initialize PostgreSQL database"
25
+ printf " ${D_CYAN}%-12s${NC} %s\n" "create" "Create a new database"
26
+ printf " ${D_CYAN}%-12s${NC} %s\n" "drop" "Drop a database"
27
+ printf " ${D_CYAN}%-12s${NC} %s\n" "backup" "Backup a database with compression & checksum"
28
+ printf " ${D_CYAN}%-12s${NC} %s\n" "restore" "Restore a database from a compressed file"
29
+ printf " ${D_CYAN}%-12s${NC} %s\n" "list-backups" "List available database backups"
30
+ printf " ${D_CYAN}%-12s${NC} %s\n" "schedule" "Schedule automatic backups via cron"
31
+ printf " ${D_CYAN}%-12s${NC} %s\n" "list" "List all databases"
32
+ printf " ${D_CYAN}%-12s${NC} %s\n" "shell" "Open psql shell"
33
+ echo
34
+ separator_section "Examples"
35
+ echo
36
+ printf " ${D_CYAN}omni pg start${NC} # Start PostgreSQL\n"
37
+ printf " ${D_CYAN}omni pg backup mydb${NC} # Backup 'mydb'\n"
38
+ printf " ${D_CYAN}omni pg restore mydb${NC} # Restore 'mydb'\n"
39
+ printf " ${D_CYAN}omni pg list-backups${NC} # List backups table\n"
40
+ printf " ${D_CYAN}omni pg schedule${NC} # Setup automated crontab backup\n"
41
+ printf " ${D_CYAN}omni pg shell${NC} # Open psql shell\n"
42
+ echo
43
+ }
44
+
45
+ # Verificar si PostgreSQL está instalado
46
+ check_pg_installed() {
47
+ if ! command -v pg_ctl &>/dev/null; then
48
+ log_error "PostgreSQL is not installed"
49
+ log_info "Run: ${D_CYAN}omni install db${NC}"
50
+ return 1
51
+ fi
52
+ return 0
53
+ }
54
+
55
+ # Verificar si está inicializado (solo informativo)
56
+ check_pg_initialized() {
57
+ # Verificar múltiples rutas posibles
58
+ local data_dirs=(
59
+ "$PREFIX/var/lib/postgresql/data"
60
+ "$PG_DATA/data"
61
+ "$HOME/.termux/postgresql/data"
62
+ "$PREFIX/var/lib/postgresql/data"
63
+ )
64
+
65
+ for dir in "${data_dirs[@]}"; do
66
+ if [[ -d "$dir" ]] && [[ -f "$dir/PG_VERSION" ]]; then
67
+ # Actualizar PG_DATA a la ruta correcta
68
+ PG_DATA="$(dirname "$dir")"
69
+ return 0
70
+ fi
71
+ done
72
+
73
+ # También verificar si el servicio está corriendo
74
+ if pg_ctl status &>/dev/null; then
75
+ return 0
76
+ fi
77
+
78
+ return 1
79
+ }
80
+
81
+ # Inicializar PostgreSQL
82
+ pg_init() {
83
+ separator
84
+ box "Initializing PostgreSQL"
85
+ separator
86
+ echo
87
+
88
+ check_pg_installed || return 1
89
+
90
+ # Verificar si ya está inicializado
91
+ if check_pg_initialized; then
92
+ log_warn "PostgreSQL is already initialized"
93
+ echo
94
+ list_item "Data directory: $PG_DATA"
95
+ list_item "Run: ${D_CYAN}omni pg start${NC}"
96
+ echo
97
+ return 0
98
+ fi
99
+
100
+ mkdir -p "$PG_DATA"
101
+
102
+ log_info "Initializing PostgreSQL database..."
103
+ echo
104
+
105
+ # En Termux, initdb necesita ejecutarse como el usuario actual
106
+ if loading "Initializing database" _pg_init_db; then
107
+ log_success "PostgreSQL initialized successfully"
108
+ echo
109
+ list_item "Data directory: $PG_DATA"
110
+ list_item "Default user: $PG_USER"
111
+ echo
112
+ log_info "Start PostgreSQL with: ${D_CYAN}omni pg start${NC}"
113
+ else
114
+ log_error "Failed to initialize PostgreSQL"
115
+ log_warn "Check log: $PG_LOG"
116
+ return 1
117
+ fi
118
+
119
+ echo
120
+ }
121
+
122
+ _pg_init_db() {
123
+ initdb -D "$PG_DATA" &>"$PG_LOG" 2>&1
124
+ }
125
+
126
+ # Iniciar PostgreSQL
127
+ pg_start() {
128
+ separator
129
+ box "Starting PostgreSQL"
130
+ separator
131
+ echo
132
+
133
+ check_pg_installed || return 1
134
+
135
+ # Intentar detectar la ruta de datos antes de iniciar
136
+ local found_dir=""
137
+ local data_dirs=(
138
+ "$PREFIX/var/lib/postgresql"
139
+ "$PREFIX/var/lib/postgresql/data"
140
+ "$PG_DATA"
141
+ "$PG_DATA/data"
142
+ "$HOME/.termux/postgresql"
143
+ "$HOME/.termux/postgresql/data"
144
+ )
145
+
146
+ for dir in "${data_dirs[@]}"; do
147
+ if [[ -d "$dir" ]] && [[ -f "$dir/PG_VERSION" ]]; then
148
+ PG_DATA="$dir"
149
+ found_dir="$dir"
150
+ break
151
+ fi
152
+ done
153
+
154
+ # Si no encontramos datos, intentar init primero
155
+ if [[ -z "$found_dir" ]]; then
156
+ log_warn "PostgreSQL not initialized yet"
157
+ log_info "Initializing first..."
158
+ mkdir -p "$PG_DATA"
159
+ if ! loading "Initializing database" _pg_init_db; then
160
+ log_error "Failed to initialize PostgreSQL"
161
+ log_warn "Check log: $PG_LOG"
162
+ return 1
163
+ fi
164
+ fi
165
+
166
+ log_info "Starting PostgreSQL server..."
167
+
168
+ if loading "Starting PostgreSQL" _pg_start_server; then
169
+ log_success "PostgreSQL started successfully"
170
+ echo
171
+ list_item "Listening on: localhost:5432"
172
+ list_item "User: $PG_USER"
173
+ echo
174
+ else
175
+ log_error "Failed to start PostgreSQL"
176
+ log_warn "Check log: $PG_LOG"
177
+ return 1
178
+ fi
179
+
180
+ echo
181
+ }
182
+
183
+ _pg_start_server() {
184
+ pg_ctl -D "$PG_DATA" -l "$PG_LOG" start 2>&1
185
+ sleep 2
186
+ # Verificar que efectivamente arrancó
187
+ pg_ctl -D "$PG_DATA" status &>/dev/null
188
+ }
189
+
190
+ # Detener PostgreSQL
191
+ pg_stop() {
192
+ separator
193
+ box "Stopping PostgreSQL"
194
+ separator
195
+ echo
196
+
197
+ check_pg_installed || return 1
198
+
199
+ # Intentar detectar la ruta de datos
200
+ local found_dir=""
201
+ local data_dirs=(
202
+ "$PREFIX/var/lib/postgresql"
203
+ "$PREFIX/var/lib/postgresql/data"
204
+ "$PG_DATA"
205
+ "$PG_DATA/data"
206
+ "$HOME/.termux/postgresql"
207
+ "$HOME/.termux/postgresql/data"
208
+ )
209
+
210
+ for dir in "${data_dirs[@]}"; do
211
+ if [[ -d "$dir" ]] && [[ -f "$dir/PG_VERSION" ]]; then
212
+ PG_DATA="$dir"
213
+ found_dir="$dir"
214
+ break
215
+ fi
216
+ done
217
+
218
+ log_info "Stopping PostgreSQL server..."
219
+
220
+ if loading "Stopping PostgreSQL" _pg_stop_server; then
221
+ log_success "PostgreSQL stopped successfully"
222
+ else
223
+ log_error "Failed to stop PostgreSQL"
224
+ log_warn "PostgreSQL may not be running"
225
+ return 1
226
+ fi
227
+
228
+ echo
229
+ }
230
+
231
+ _pg_stop_server() {
232
+ pg_ctl -D "$PG_DATA" stop &>/dev/null
233
+ }
234
+
235
+ # Reiniciar PostgreSQL
236
+ pg_restart() {
237
+ separator
238
+ box "Restarting PostgreSQL"
239
+ separator
240
+ echo
241
+
242
+ check_pg_installed || return 1
243
+ check_pg_initialized || return 1
244
+
245
+ pg_stop
246
+ sleep 1
247
+ pg_start
248
+
249
+ echo
250
+ separator
251
+ log_success "PostgreSQL restarted"
252
+ separator
253
+ echo
254
+ }
255
+
256
+ # Estado de PostgreSQL
257
+ pg_status() {
258
+ separator
259
+ box "PostgreSQL Status"
260
+ separator
261
+ echo
262
+
263
+ check_pg_installed || return 1
264
+
265
+ # Intentar detectar la ruta de datos
266
+ local found_dir=""
267
+ local data_dirs=(
268
+ "$PREFIX/var/lib/postgresql"
269
+ "$PREFIX/var/lib/postgresql/data"
270
+ "$PG_DATA"
271
+ "$PG_DATA/data"
272
+ "$HOME/.termux/postgresql"
273
+ "$HOME/.termux/postgresql/data"
274
+ )
275
+
276
+ for dir in "${data_dirs[@]}"; do
277
+ if [[ -d "$dir" ]] && [[ -f "$dir/PG_VERSION" ]]; then
278
+ PG_DATA="$dir"
279
+ found_dir="$dir"
280
+ break
281
+ fi
282
+ done
283
+
284
+ log_info "Checking PostgreSQL status..."
285
+ echo
286
+
287
+ # Verificar estado
288
+ if [[ -n "$found_dir" ]]; then
289
+ if pg_ctl -D "$found_dir" status &>/dev/null; then
290
+ log_success "PostgreSQL is RUNNING"
291
+ echo
292
+ list_item "Data directory: $PG_DATA"
293
+ list_item "Port: 5432"
294
+ list_item "User: $PG_USER"
295
+ else
296
+ log_warn "PostgreSQL is STOPPED"
297
+ echo
298
+ list_item "Data directory: $PG_DATA"
299
+ list_item "Run: ${D_CYAN}omni pg start${NC}"
300
+ fi
301
+ else
302
+ log_warn "PostgreSQL is NOT INITIALIZED"
303
+ echo
304
+ list_item "Run: ${D_CYAN}omni pg init${NC} to initialize the database"
305
+ list_item "Then run: ${D_CYAN}omni pg start${NC} to start the server"
306
+ fi
307
+
308
+ echo
309
+ }
310
+
311
+ # Crear base de datos
312
+ pg_create() {
313
+ local db_name="$1"
314
+
315
+ if [[ -z "$db_name" ]]; then
316
+ log_error "Database name required"
317
+ log_info "Usage: omni pg create <database_name>"
318
+ return 1
319
+ fi
320
+
321
+ check_pg_installed || return 1
322
+
323
+ # Detectar ruta de datos
324
+ _detect_pg_data
325
+
326
+ log_info "Creating database: $db_name..."
327
+
328
+ if createdb "$db_name" &>/dev/null; then
329
+ log_success "Database '$db_name' created successfully"
330
+ else
331
+ log_error "Failed to create database '$db_name'"
332
+ log_warn "PostgreSQL may not be running. Run: omni pg start"
333
+ return 1
334
+ fi
335
+ }
336
+
337
+ # Eliminar base de datos
338
+ pg_drop() {
339
+ local db_name="$1"
340
+
341
+ if [[ -z "$db_name" ]]; then
342
+ log_error "Database name required"
343
+ log_info "Usage: omni pg drop <database_name>"
344
+ return 1
345
+ fi
346
+
347
+ check_pg_installed || return 1
348
+
349
+ log_warn "This will permanently delete database: $db_name"
350
+
351
+ read_confirm "Are you sure?" CONFIRM
352
+ if [[ "$CONFIRM" != "y" ]]; then
353
+ log_warn "Operation cancelled"
354
+ return 0
355
+ fi
356
+
357
+ # Detectar ruta de datos
358
+ _detect_pg_data
359
+
360
+ log_info "Dropping database: $db_name..."
361
+
362
+ if dropdb "$db_name" &>/dev/null; then
363
+ log_success "Database '$db_name' dropped successfully"
364
+ else
365
+ log_error "Failed to drop database '$db_name'"
366
+ return 1
367
+ fi
368
+ }
369
+
370
+ # Listar bases de datos
371
+ pg_list() {
372
+ separator
373
+ box "PostgreSQL Databases"
374
+ separator
375
+ echo
376
+
377
+ check_pg_installed || return 1
378
+
379
+ # Detectar ruta de datos
380
+ _detect_pg_data
381
+
382
+ log_info "Listing databases..."
383
+ echo
384
+
385
+ psql -c '\l' 2>/dev/null || {
386
+ log_error "Failed to list databases"
387
+ log_warn "PostgreSQL may not be running. Run: omni pg start"
388
+ return 1
389
+ }
390
+
391
+ echo
392
+ }
393
+
394
+ # Abrir shell psql
395
+ pg_shell() {
396
+ check_pg_installed || return 1
397
+
398
+ # Detectar ruta de datos
399
+ _detect_pg_data
400
+
401
+ log_info "Opening psql shell..."
402
+ echo
403
+
404
+ psql 2>/dev/null
405
+ }
406
+
407
+ # Función auxiliar para detectar ruta de datos
408
+ _detect_pg_data() {
409
+ local data_dirs=(
410
+ "$PREFIX/var/lib/postgresql"
411
+ "$PREFIX/var/lib/postgresql/data"
412
+ "$PG_DATA"
413
+ "$PG_DATA/data"
414
+ "$HOME/.termux/postgresql"
415
+ "$HOME/.termux/postgresql/data"
416
+ )
417
+
418
+ for dir in "${data_dirs[@]}"; do
419
+ if [[ -d "$dir" ]] && [[ -f "$dir/PG_VERSION" ]]; then
420
+ PG_DATA="$dir"
421
+ return 0
422
+ fi
423
+ done
424
+
425
+ return 1
426
+ }
427
+
428
+ _run_backup_cmd() {
429
+ local db_name="$1"
430
+ local file_path="$2"
431
+ pg_dump -d "$db_name" -F c -b 2>/dev/null | gzip > "$file_path"
432
+ return ${PIPESTATUS[0]}
433
+ }
434
+
435
+ _run_restore_cmd() {
436
+ local db_name="$1"
437
+ local file_path="$2"
438
+ gunzip -c "$file_path" | pg_restore -d "$db_name" -c 2>/dev/null || gunzip -c "$file_path" | pg_restore -d "$db_name" 2>/dev/null
439
+ }
440
+
441
+ _pg_cleanup_old_backups() {
442
+ local db_name="$1"
443
+ local backup_dir="$OMNI_DATA/pg_backups"
444
+ local retention_limit=10
445
+
446
+ local -a backups=()
447
+ while IFS= read -r f; do
448
+ if [[ -n "$f" ]]; then
449
+ backups+=("$f")
450
+ fi
451
+ done < <(find "$backup_dir" -name "${db_name}_*.backup.gz" -type f 2>/dev/null | sort)
452
+
453
+ local count=${#backups[@]}
454
+ if (( count > retention_limit )); then
455
+ local to_delete=$(( count - retention_limit ))
456
+ log_info "Cleaning up $to_delete old backup(s)..."
457
+ for ((i=0; i<to_delete; i++)); do
458
+ rm -f "${backups[$i]}" "${backups[$i]}.sha256"
459
+ list_item "Deleted: $(basename "${backups[$i]}")"
460
+ done
461
+ fi
462
+ }
463
+
464
+ pg_backup() {
465
+ check_pg_installed || return 1
466
+ local db_name="$1"
467
+ if [[ -z "$db_name" ]]; then
468
+ read_input "Enter database name to backup" db_name
469
+ fi
470
+ if [[ -z "$db_name" ]]; then
471
+ log_error "No database name provided"
472
+ return 1
473
+ fi
474
+
475
+ # check if db exists
476
+ if ! psql -lqt | cut -d \| -f 1 | grep -w "$db_name" &>/dev/null; then
477
+ log_error "Database '$db_name' does not exist"
478
+ return 1
479
+ fi
480
+
481
+ local backup_dir="$OMNI_DATA/pg_backups"
482
+ mkdir -p "$backup_dir"
483
+ local file_name="${db_name}_$(date +%Y%m%d_%H%M%S).backup.gz"
484
+ local file_path="$backup_dir/$file_name"
485
+
486
+ log_info "Creating compressed backup for '$db_name'..."
487
+ if loading "Running backup dump" _run_backup_cmd "$db_name" "$file_path"; then
488
+ # Integrity checksum
489
+ sha256sum "$file_path" | cut -d' ' -f1 > "${file_path}.sha256"
490
+
491
+ log_success "Backup created successfully:"
492
+ list_item "$file_path"
493
+ list_item "Checksum generated: $(cat "${file_path}.sha256")"
494
+
495
+ # Cleanup old backups
496
+ _pg_cleanup_old_backups "$db_name"
497
+ else
498
+ log_error "Failed to create backup"
499
+ rm -f "$file_path"
500
+ return 1
501
+ fi
502
+ }
503
+
504
+ pg_restore() {
505
+ check_pg_installed || return 1
506
+ local db_name="$1"
507
+ local backup_file="$2"
508
+
509
+ local backup_dir="$OMNI_DATA/pg_backups"
510
+ if [[ ! -d "$backup_dir" ]]; then
511
+ log_error "No backups directory found: $backup_dir"
512
+ return 1
513
+ fi
514
+
515
+ # List backups (both legacy .backup and new compressed .backup.gz)
516
+ local -a files=()
517
+ while IFS= read -r f; do
518
+ if [[ -n "$f" ]]; then
519
+ files+=("$f")
520
+ fi
521
+ done < <(find "$backup_dir" \( -name "*.backup" -o -name "*.backup.gz" \) 2>/dev/null | sort -r)
522
+
523
+ if [[ ${#files[@]} -eq 0 ]]; then
524
+ log_error "No backup files found in $backup_dir"
525
+ return 1
526
+ fi
527
+
528
+ if [[ -z "$backup_file" ]]; then
529
+ log_info "Select a backup file to restore:"
530
+ local idx=0
531
+ for f in "${files[@]}"; do
532
+ printf " ${D_GREEN}%2d.${D_NC} %s\n" $((idx + 1)) "$(basename "$f")"
533
+ ((idx++))
534
+ done
535
+ echo
536
+ local pick
537
+ read_input "Select backup file (#)" pick
538
+ if [[ "$pick" =~ ^[0-9]+$ ]] && ((pick >= 1 && pick <= ${#files[@]})); then
539
+ backup_file="${files[$((pick - 1))]}"
540
+ else
541
+ log_error "Invalid selection"
542
+ return 1
543
+ fi
544
+ fi
545
+
546
+ if [[ ! -f "$backup_file" ]]; then
547
+ log_error "Backup file not found: $backup_file"
548
+ return 1
549
+ fi
550
+
551
+ # Integrity verification
552
+ if [[ -f "${backup_file}.sha256" ]]; then
553
+ log_info "Verifying backup integrity..."
554
+ local current_hash
555
+ current_hash=$(sha256sum "$backup_file" | cut -d' ' -f1)
556
+ local expected_hash
557
+ expected_hash=$(cat "${backup_file}.sha256" 2>/dev/null)
558
+ if [[ "$current_hash" != "$expected_hash" ]]; then
559
+ log_warn "Integrity validation FAILED!"
560
+ log_warn "Current SHA256: $current_hash"
561
+ log_warn "Expected SHA256: $expected_hash"
562
+ local confirm
563
+ read_confirm "Continue restoring anyway?" confirm
564
+ if [[ "$confirm" != "y" ]]; then
565
+ log_error "Restoration aborted by user"
566
+ return 1
567
+ fi
568
+ else
569
+ log_success "Integrity check: OK"
570
+ fi
571
+ else
572
+ log_warn "No SHA256 checksum file found. Integrity check skipped."
573
+ fi
574
+
575
+ if [[ -z "$db_name" ]]; then
576
+ read_input "Enter target database name" db_name
577
+ fi
578
+
579
+ if [[ -z "$db_name" ]]; then
580
+ log_error "No target database provided"
581
+ return 1
582
+ fi
583
+
584
+ # Create db if not exists
585
+ if ! psql -lqt | cut -d \| -f 1 | grep -w "$db_name" &>/dev/null; then
586
+ log_info "Target database '$db_name' does not exist. Creating it..."
587
+ if ! createdb "$db_name" &>/dev/null; then
588
+ log_error "Failed to create target database '$db_name'"
589
+ return 1
590
+ fi
591
+ log_success "Database '$db_name' created"
592
+ fi
593
+
594
+ log_info "Restoring backup into '$db_name'..."
595
+
596
+ local success=false
597
+ if [[ "$backup_file" == *.gz ]]; then
598
+ if loading "Restoring compressed database" _run_restore_cmd "$db_name" "$backup_file"; then
599
+ success=true
600
+ fi
601
+ else
602
+ if loading "Restoring database" pg_restore -d "$db_name" -c "$backup_file" 2>/dev/null || pg_restore -d "$db_name" "$backup_file" 2>/dev/null; then
603
+ success=true
604
+ fi
605
+ fi
606
+
607
+ if [[ "$success" == "true" ]]; then
608
+ log_success "Database '$db_name' restored successfully"
609
+ else
610
+ log_error "Failed to restore database. Make sure the database is compatible."
611
+ return 1
612
+ fi
613
+ }
614
+
615
+ pg_list_backups() {
616
+ local backup_dir="$OMNI_DATA/pg_backups"
617
+ separator
618
+ box "Available PostgreSQL Backups"
619
+ separator
620
+ echo
621
+ if [[ ! -d "$backup_dir" ]]; then
622
+ log_info "No backups directory found"
623
+ return 0
624
+ fi
625
+
626
+ local -a backups=()
627
+ while IFS= read -r f; do
628
+ if [[ -n "$f" ]]; then
629
+ backups+=("$f")
630
+ fi
631
+ done < <(find "$backup_dir" \( -name "*.backup" -o -name "*.backup.gz" \) -type f 2>/dev/null | sort -r)
632
+
633
+ if [[ ${#backups[@]} -eq 0 ]]; then
634
+ log_info "No backup files found."
635
+ separator
636
+ return 0
637
+ fi
638
+
639
+ table_start "Database" "Backup File" "Size" "Date" "Integrity"
640
+ for f in "${backups[@]}"; do
641
+ local base=$(basename "$f")
642
+ local db=$(echo "$base" | cut -d'_' -f1)
643
+ local size=$(du -sh "$f" | awk '{print $1}')
644
+ local date_str
645
+ local date_part=$(echo "$base" | grep -oE '[0-9]{8}_[0-9]{6}')
646
+ if [[ -n "$date_part" ]]; then
647
+ date_str="${date_part:0:4}-${date_part:4:2}-${date_part:6:2} ${date_part:9:2}:${date_part:11:2}:${date_part:13:2}"
648
+ else
649
+ date_str="Unknown"
650
+ fi
651
+
652
+ local integrity="OK"
653
+ if [[ -f "${f}.sha256" ]]; then
654
+ local current_hash=$(sha256sum "$f" | cut -d' ' -f1)
655
+ local expected_hash=$(cat "${f}.sha256" 2>/dev/null)
656
+ if [[ "$current_hash" != "$expected_hash" ]]; then
657
+ integrity="Corrupted"
658
+ fi
659
+ else
660
+ integrity="No checksum"
661
+ fi
662
+ table_row "$db" "$base" "$size" "$date_str" "$integrity"
663
+ done
664
+ table_end
665
+ echo
666
+ }
667
+
668
+ pg_schedule() {
669
+ separator
670
+ box "Schedule Automated PostgreSQL Backups"
671
+ separator
672
+ echo
673
+ check_pg_installed || return 1
674
+
675
+ local db_name
676
+ read_input "Enter database name to schedule" db_name
677
+ if [[ -z "$db_name" ]]; then
678
+ log_error "Database name required"
679
+ return 1
680
+ fi
681
+
682
+ local interval
683
+ read_select "Select backup frequency" interval "Daily (2:00 AM)" "Weekly (Sundays 2:00 AM)" "Hourly" "Cancel"
684
+ [[ "$interval" == "Cancel" ]] && return 0
685
+
686
+ local cron_expr=""
687
+ case "$interval" in
688
+ "Daily (2:00 AM)") cron_expr="0 2 * * *" ;;
689
+ "Weekly (Sundays 2:00 AM)") cron_expr="0 2 * * 0" ;;
690
+ "Hourly") cron_expr="0 * * * *" ;;
691
+ esac
692
+
693
+ if [[ -n "$cron_expr" ]]; then
694
+ if ! command -v crontab &>/dev/null; then
695
+ log_warn "crontab command not found. Trying to install cronie..."
696
+ pkg install -y cronie >/dev/null 2>&1 || true
697
+ fi
698
+
699
+ local job="$cron_expr OMNI_PATH=$OMNI_PATH $OMNI_PATH/bin/omni pg backup $db_name >/dev/null 2>&1"
700
+ (crontab -l 2>/dev/null | grep -v "omni pg backup $db_name"; echo "$job") | crontab -
701
+ log_success "Backup scheduled successfully ($interval) for database '$db_name'!"
702
+ list_item "Ensure cron daemon is running (run: 'crond')"
703
+ fi
704
+ echo
705
+ }
706
+
707
+ # Función principal
708
+ pg_main() {
709
+ local cmd="$1"
710
+ shift || true
711
+
712
+ case "$cmd" in
713
+ start)
714
+ pg_start
715
+ ;;
716
+ stop)
717
+ pg_stop
718
+ ;;
719
+ restart)
720
+ pg_restart
721
+ ;;
722
+ status)
723
+ pg_status
724
+ ;;
725
+ init)
726
+ pg_init
727
+ ;;
728
+ create)
729
+ pg_create "$2"
730
+ ;;
731
+ drop)
732
+ pg_drop "$2"
733
+ ;;
734
+ backup)
735
+ pg_backup "$@"
736
+ ;;
737
+ restore)
738
+ pg_restore "$@"
739
+ ;;
740
+ list-backups | backups)
741
+ pg_list_backups
742
+ ;;
743
+ schedule)
744
+ pg_schedule
745
+ ;;
746
+ list | ls)
747
+ pg_list
748
+ ;;
749
+ shell | psql)
750
+ pg_shell
751
+ ;;
752
+ "")
753
+ pg_help
754
+ ;;
755
+ *)
756
+ log_error "Unknown command: $cmd"
757
+ pg_help
758
+ exit 1
759
+ ;;
760
+ esac
761
+ }