Skip to content

fix(cli): deployment-scoped commands resolve --repo/--pg-connection from config (closes #12)#13

Merged
postgresql007 merged 4 commits into
mainfrom
fix/issue-12-backup-deployment-config
Jun 24, 2026
Merged

fix(cli): deployment-scoped commands resolve --repo/--pg-connection from config (closes #12)#13
postgresql007 merged 4 commits into
mainfrom
fix/issue-12-backup-deployment-config

Conversation

@postgresql007

Copy link
Copy Markdown
Contributor

Problem (#12)

pg_hardstorage backup mytest errored backup: --pg-connection, --repo are required even though mytest was fully configured in pg_hardstorage.yaml. The command never consulted the deployment catalogue.

It turned out to be systemic, not unique to backup: --repo (and --pg-connection on the wal commands) is enforced on ~40 deployment-scoped commands via two mechanisms — requireFlags() (backup/restore/verify) and cobra's MarkFlagRequired() (list, show, status, hold, rotate, recovery, repair, wal preflight/stream/list/audit/prune/gaps, partial, kms verify/shred, …) — and none loaded config.

Fix

  • Shared helper deploymentDefaults() for the requireFlags() path (backup/restore/verify/verify --full).
  • A root PersistentPreRunE resolver (resolveDeploymentDefaultsPreRun) that runs before cobra validates required flags and fills an unset --repo/--pg-connection from the deployment named as the command's first positional arg. One point, covers every MarkFlagRequired command.
  • Conservative by design: acts only when the flag exists, is unset, and args[0] names a known deployment. Explicit flags always win; non-deployment commands and genuinely missing flags still error exactly as before. Repo-only commands (fleet/capacity/audit/…) are correctly untouched.

Tests (proof)

  • TestDeploymentFlagResolution_Matrix — 14 deployment-scoped commands: configured deployment + no flags → no usage.missing_flag.
  • TestDeploymentFlagResolution_NoConfigStillRequires — negative control: no config → still usage.missing_flag.
  • TestResolveDeploymentDefaults — table-driven resolver unit test (precedence, partial, unknown/empty/nil).
  • Per-command regressions for backup/restore/verify.
  • main_test.go TestMain pins a clean HOME so the suite is deterministic regardless of the host's ~/.config/pg_hardstorage (an ambient-config leak this work surfaced).

Full internal/cli package, go build ./..., and go vet pass locally.

Scope notes

  • wal stream shares the fix via the root resolver but is intentionally excluded from the matrix (long-running; would block a unit test).
  • logical uses a separate config store and db_extension takes no <deployment> — both correctly out of scope.

Closes #12.

#12)

`pg_hardstorage backup <deployment>` demanded --pg-connection and --repo
even when the named deployment was fully configured in
pg_hardstorage.yaml — it never consulted the deployment catalogue.

Now, when those flags are omitted, backup loads the named deployment
from config and fills pg-connection/repo from it (explicit flags still
win); the required-flag check validates the resolved values, so a
configured deployment "just works" and an unknown one still errors with
usage.missing_flag.

Adds a regression test, and isolates TestBackup_Requires{PGConnection,Repo}
from any ambient ~/.config config now that backup reads it.

Closes #12.
Extract a shared, tested helper (deployment_defaults.go) and apply it to
the backup/restore lifecycle so a configured deployment no longer demands
flags it already declares:

- backup   → resolves --pg-connection and --repo from the deployment
- restore  → resolves --repo (--target stays CLI-only; not in config)
- verify / verify --full → resolve --repo

Tests:
- deployment_defaults_test.go: table-driven unit test for the resolver
  (fills both, flag-wins precedence, partial, unknown/empty deployment,
  nil deps).
- backup/restore/verify CLI regression tests for "configured deployment,
  no flags" and "no config, flag still required".
- Isolated pre-existing TestBackup_Requires* / TestRestore_Requires*
  from any ambient ~/.config config now that these commands read it.

NOTE — broader systemic gap (NOT yet fixed): --repo is MarkFlagRequired
on ~40 other deployment-scoped commands (list, show, hold, rotate,
status, recovery, repair, wal preflight/stream/list/audit/prune,
partial, kms verify/shred, ...) and pg-connection on the wal commands;
none consult the deployment config. Same bug class — tracked for a
follow-up sweep.
…ommands (#12)

The reported bug was systemic: --repo (and --pg-connection on the wal
commands) is MarkFlagRequired on ~40 deployment-scoped commands, none of
which consulted the deployment catalogue — so a configured deployment
still demanded the flags everywhere (list, show, status, hold, rotate,
recovery, repair, wal preflight/stream/list/audit/prune/gaps, partial,
kms verify/shred, …).

Fix at one point: a root PersistentPreRunE resolver
(resolveDeploymentDefaultsPreRun) runs before Cobra validates required
flags and fills an unset --repo / --pg-connection from the deployment
named as the command's first positional arg. It is conservative — acts
only when the flag exists, is unset, and the arg names a known deployment
— so non-deployment commands are unaffected and a genuine omission still
errors. Combined with the earlier requireFlags-path fix for
backup/restore/verify, every deployment-scoped command now honours
pg_hardstorage.yaml.

Tests proving it:
- deployment_flag_resolution_test.go: a 14-command matrix (configured
  deployment, no flags → no usage.missing_flag) + a negative control
  (no config → still usage.missing_flag).
- deployment_defaults_test.go: table-driven resolver unit test.
- per-command regression tests for backup/restore/verify.
- main_test.go: package TestMain pins a clean HOME so the suite is
  deterministic regardless of the host's ~/.config/pg_hardstorage (this
  ambient-config leak is what surfaced the breakage).

Full internal/cli package, go build ./..., and go vet pass.
@postgresql007 postgresql007 merged commit fa70315 into main Jun 24, 2026
56 of 57 checks passed
@postgresql007 postgresql007 deleted the fix/issue-12-backup-deployment-config branch June 24, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pg_hardstorage does not use configured deployment

1 participant