karnel-termux 0.0.1

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