From 58b40007fa31be489e7173674cfc2bfbe53c981a Mon Sep 17 00:00:00 2001 From: Asim Atasert Date: Mon, 6 Jul 2026 16:27:43 +0300 Subject: [PATCH 1/2] feat: add 'doctor' and 'explain' commands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two terminal-native features distilled from the (abandoned) web-console exploration — the genuinely useful parts, with no server/attack surface. - 'doctor': local, read-only health checks with ✓/!/✗ output and a non-zero exit on problems — required/optional toolchain, output dir + free disk, per-database backup freshness (age of the newest dump), newest-archive readability, and the jobs file. With --from/--to it also tests connectivity. Good as a cron pre-flight or CI gate. - 'explain [topic]': built-in, offline reference for what each command is for and when to use it (dump/restore/clone/stream/upgrade/fetch/batch/bot/ list/meta/clean/doctor + masking/retention), with key options and examples. Wired into dispatch, help, README, and bash/zsh/fish completions (also adds the previously-missing 'bot' completion). New modules src/71-explain.sh and src/73-doctor.sh; regenerated the bundled t-pgsql (build --check passes). --- README.md | 23 ++ completions/_t-pgsql | 3 + completions/t-pgsql.bash | 2 +- completions/t-pgsql.fish | 2 +- src/01-globals.sh | 1 + src/30-help.sh | 2 + src/71-explain.sh | 279 +++++++++++++++++++++++ src/73-doctor.sh | 186 +++++++++++++++ src/90-args.sh | 6 + src/99-main.sh | 2 + src/build.manifest | 2 + t-pgsql | 476 +++++++++++++++++++++++++++++++++++++++ 12 files changed, 982 insertions(+), 2 deletions(-) create mode 100644 src/71-explain.sh create mode 100644 src/73-doctor.sh diff --git a/README.md b/README.md index 2f62f4d..82f7217 100644 --- a/README.md +++ b/README.md @@ -494,6 +494,29 @@ Cleans old dump files. --- +### doctor + +Runs local, read-only health checks and prints `✓ / ! / ✗` findings: required and optional tools, the output directory and free disk space, how fresh each database's newest dump is, whether the newest archives are readable, and the jobs file. Add `--from`/`--to` to also test connectivity. Exits non-zero if it finds a problem — handy in CI or a cron pre-flight. + +```bash +./t-pgsql doctor +./t-pgsql doctor --from "ssh://user@host/postgres@localhost/appdb" +``` + +--- + +### explain + +Explains what a command is for and when to use it, with key options and examples — a built-in, offline reference. + +```bash +./t-pgsql explain # list all topics +./t-pgsql explain clone # focused help for a command +./t-pgsql explain masking # also: stream, retention, doctor, ... +``` + +--- + ### jobs Lists saved batch jobs. diff --git a/completions/_t-pgsql b/completions/_t-pgsql index c20be24..ebb77b5 100644 --- a/completions/_t-pgsql +++ b/completions/_t-pgsql @@ -18,10 +18,13 @@ _t-pgsql() { 'upgrade:Logical major-version migration' 'fetch:Fetch existing dump from remote' 'batch:Run multiple jobs' + 'bot:Telegram bot (listen for commands)' 'jobs:Manage saved jobs' 'list:List dump files' 'meta:Show metadata' 'clean:Clean old dumps' + 'doctor:Check environment and backup health' + 'explain:Explain what a command is for' 'version:Show version' 'help:Show help' ) diff --git a/completions/t-pgsql.bash b/completions/t-pgsql.bash index bb487fd..520d19d 100644 --- a/completions/t-pgsql.bash +++ b/completions/t-pgsql.bash @@ -4,7 +4,7 @@ _t_pgsql() { local cur prev words cword _init_completion || return - local commands="dump restore clone upgrade fetch batch jobs list meta clean version help" + local commands="dump restore clone upgrade fetch batch bot jobs list meta clean doctor explain version help" local opts=" --from --to diff --git a/completions/t-pgsql.fish b/completions/t-pgsql.fish index dd717ef..a121258 100644 --- a/completions/t-pgsql.fish +++ b/completions/t-pgsql.fish @@ -1,7 +1,7 @@ # Fish completion for t-pgsql # Commands -set -l commands dump restore clone upgrade fetch batch jobs list meta clean version help +set -l commands dump restore clone upgrade fetch batch bot jobs list meta clean doctor explain version help # Disable file completion by default complete -c t-pgsql -f diff --git a/src/01-globals.sh b/src/01-globals.sh index d5114f3..efd5c96 100644 --- a/src/01-globals.sh +++ b/src/01-globals.sh @@ -135,6 +135,7 @@ FILE="" # Clean (optional positional database/base-name for the "clean" command) CLEAN_DB="" +EXPLAIN_TARGET="" # Custom dump base name (also usable by "clean") DUMP_NAME="" diff --git a/src/30-help.sh b/src/30-help.sh index b7b75bf..392edab 100644 --- a/src/30-help.sh +++ b/src/30-help.sh @@ -27,6 +27,8 @@ COMMANDS: clean --keep Clean by database name clean --from --keep clean --dump-name --retention + doctor Check the environment and backup health (add --from/--to to test connectivity) + explain Explain what a command is for: explain [dump|restore|clone|upgrade|...] version Show version CONNECTION: diff --git a/src/71-explain.sh b/src/71-explain.sh new file mode 100644 index 0000000..2085316 --- /dev/null +++ b/src/71-explain.sh @@ -0,0 +1,279 @@ +# ============================================================================== +# EXPLAIN — teach what each command is for and when to use it +# ============================================================================== +# `t-pgsql explain` lists the topics +# `t-pgsql explain ` prints a focused, example-driven explanation + +_ex_title() { echo -e "${BOLD}t-pgsql $1${NC} — $2"; echo; } +_ex_sec() { echo -e "${CYAN}$1${NC}"; } +_ex_see() { echo; echo -e "${MAGENTA}See also:${NC} $1"; } + +_explain_overview() { + echo -e "${BOLD}t-pgsql — what each command is for${NC}" + echo + echo -e " ${CYAN}dump${NC} Back up one database to a compressed -Fc file" + echo -e " ${CYAN}restore${NC} Load a dump back into a database (safe on --force)" + echo -e " ${CYAN}clone${NC} Dump + restore in one step (full sync)" + echo -e " ${CYAN}upgrade${NC} Logical major-version migration (e.g. 16 → 18) with globals" + echo -e " ${CYAN}fetch${NC} Download an existing remote dump (no new dump)" + echo -e " ${CYAN}batch${NC} Run saved jobs from a jobs.yaml" + echo -e " ${CYAN}bot${NC} Telegram bot to trigger/monitor backups from a chat" + echo -e " ${CYAN}list${NC} / ${CYAN}meta${NC} / ${CYAN}clean${NC} Inspect dumps, show metadata, prune old dumps" + echo -e " ${CYAN}doctor${NC} Check the environment and backup health" + echo + echo -e " Extra topics: ${CYAN}stream${NC}, ${CYAN}masking${NC}, ${CYAN}retention${NC}" + echo + echo -e "Run ${BOLD}t-pgsql explain ${NC} for details, e.g. ${BOLD}t-pgsql explain clone${NC}." +} + +_explain_dump() { + _ex_title "dump" "point-in-time backup of one database" + _ex_sec "What it does" + echo " Runs pg_dump in the custom format (-Fc) and writes a timestamped file" + echo " to the output directory. Works against a local/TCP database or, with an" + echo " ssh:// source, dumps on the remote host and transfers the file back." + echo + _ex_sec "When to use" + echo " • Scheduled backups (cron / batch jobs)" + echo " • A safety snapshot before a risky migration or deploy" + echo " • Archiving a database for later" + echo + _ex_sec "Key options" + echo " --from source database" + echo " --output where to write (default: