Skip to content

Commit 410d0b0

Browse files
hrodrigcursoragent
andcommitted
feat(cli): add --print-sample-config flag
Print contrib/pgwd.conf.example to stdout before config load so operators without the example file on disk can bootstrap /etc/pgwd/pgwd.conf. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent b6f4c73 commit 410d0b0

6 files changed

Lines changed: 58 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Format based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Histor
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- **`pgwd --print-sample-config`** — writes annotated sample config to stdout (same as `contrib/pgwd.conf.example`).
12+
913
## [0.6.8] - 2026-06-10
1014

1115
### Security

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pgwd loads settings from (in order): **config file** → **environment variables
127127
| Environment | `PGWD_*` |
128128
| CLI | `-flag` |
129129

130-
**Config file** (YAML) — keys match `-flag` and `PGWD_*` env vars. See `contrib/pgwd.conf.example`. Use `databases:` for one or more Postgres (canonical). Legacy `db:` is deprecated and will be removed in v1.0. For kube.postgres, use `db:` until per-db kube support exists.
130+
**Config file** (YAML) — keys match `-flag` and `PGWD_*` env vars. See `contrib/pgwd.conf.example` (or **`pgwd --print-sample-config > /etc/pgwd/pgwd.conf`** when the example file is not on disk). Use `databases:` for one or more Postgres (canonical). Legacy `db:` is deprecated and will be removed in v1.0. For kube.postgres, use `db:` until per-db kube support exists.
131131

132132
```bash
133133
# Use default path /etc/pgwd/pgwd.conf
@@ -522,6 +522,7 @@ All parameters can be set via **config file**, **CLI**, or **environment variabl
522522

523523
| CLI | Env | Description |
524524
|-----|-----|-------------|
525+
| `--print-sample-config` | — | Print annotated sample config to **stdout** and exit (same as `contrib/pgwd.conf.example`). |
525526
| `-config` | `PGWD_CONFIG` | Config file path (YAML). Default `/etc/pgwd/pgwd.conf`. See `contrib/pgwd.conf.example`. |
526527
| `-db-url` | `PGWD_DB_URL` | PostgreSQL connection URL (required). With `-kube-postgres`, use host localhost and port matching `-kube-local-port`. |
527528
| `-kube-postgres` | `PGWD_KUBE_POSTGRES` | Connect via port-forward (client-go): `namespace/type/name` (e.g. `default/svc/postgres`). Requires kubeconfig. |

cmd/pgwd/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"syscall"
1919
"time"
2020

21+
"github.com/hrodrig/pgwd/contrib"
2122
"github.com/hrodrig/pgwd/internal/checker"
2223
"github.com/hrodrig/pgwd/internal/config"
2324
"github.com/hrodrig/pgwd/internal/httpsrv"
@@ -69,6 +70,16 @@ func handleVersion() {
6970
}
7071
}
7172

73+
// handlePrintSampleConfig writes the annotated example config to stdout and exits when requested.
74+
func handlePrintSampleConfig() {
75+
for _, arg := range os.Args[1:] {
76+
if arg == "--print-sample-config" || arg == "-print-sample-config" {
77+
fmt.Print(contrib.SampleConf())
78+
os.Exit(0)
79+
}
80+
}
81+
}
82+
7283
// parseFlags registers pgwd CLI flags on cfg, parses os.Args, and returns whether
7384
// -version was set. Call after loading config from file/env so flag values override
7485
// those sources (see config.FinalizeAfterFlags). When true, the caller should
@@ -724,6 +735,7 @@ func logConfigTrace(path string, configLoaded bool, hasCLIArgs bool) {
724735
// once or on a ticker, and serve HTTP /metrics when configured.
725736
func main() {
726737
handleVersion()
738+
handlePrintSampleConfig()
727739

728740
cfg, loaded, configPath := loadAndParseConfig()
729741
applyDBURLOverride(&cfg)

cmd/pgwd/main_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ func runBinary(args ...string) (stdout, stderr string, exitCode int) {
5656
return stdout, stderr, exitCode
5757
}
5858

59+
func TestMain_PrintSampleConfig(t *testing.T) {
60+
stdout, stderr, code := runBinary("--print-sample-config")
61+
if code != 0 {
62+
t.Errorf("pgwd --print-sample-config: exit code %d, want 0", code)
63+
}
64+
if stdout == "" || !strings.Contains(stdout, "client:") {
65+
t.Errorf("pgwd --print-sample-config: stdout %q does not contain sample config", stdout)
66+
}
67+
if stderr != "" {
68+
t.Errorf("pgwd --print-sample-config: expected empty stderr, got %q", stderr)
69+
}
70+
}
71+
5972
func TestMain_Version(t *testing.T) {
6073
stdout, _, code := runBinary("-version")
6174
if code != 0 {

contrib/sample.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package contrib
2+
3+
import _ "embed"
4+
5+
//go:embed pgwd.conf.example
6+
var sampleConf []byte
7+
8+
// SampleConf returns the annotated example configuration (same as contrib/pgwd.conf.example).
9+
func SampleConf() string {
10+
return string(sampleConf)
11+
}

contrib/sample_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package contrib
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestSampleConf(t *testing.T) {
9+
s := SampleConf()
10+
if !strings.Contains(s, "client:") {
11+
t.Fatal("missing client key")
12+
}
13+
if !strings.Contains(s, "databases:") {
14+
t.Fatal("missing databases block")
15+
}
16+
}

0 commit comments

Comments
 (0)