feat(mcp): derive env vars from the framework's real struct tags
Minor to v1.1.0. Make the whole environment-variable surface derive from the
indexed component-config tags instead of a hand-maintained list that drifts.
Also folds in the scaffold-symbol fixes staged as v1.0.1 (never tagged); the
generated scaffold compiles clean against einherjar v1.0.0.
internal/envspec (new):
- Parse index struct tags into env vars {name, module, struct, field, required,
default}. One source of truth: ParseTag, ForModule, FindStruct, All, KnownNames.
internal/tools:
- get_config_env: list the real env vars a component config reads (or all).
- check_env: flag unknown EINHERJAR_* names, required vars missing for the
composed modules, and dead vars (set for an uncomposed module).
- get_scaffold: .env.example is now DERIVED from the index — required vars
uncommented with a dev value, defaulted vars commented with their default.
The scaffold now composes logz.Config (Log logz.Config) so EINHERJAR_LOG_*
are live and documented, not hardcoded/ignored (log format is env-driven).
- validate_snippet: inject the real env-var name set into the rules package.
internal/index/builtins (wire conventions):
- Route the incremental "compose a component later" flow to get_config_env /
check_env; distinguish framework EINHERJAR_* from app-owned APP_* (JWT).
- Compose logz.Config in the config + Run() examples, to match the scaffold.
internal/rules:
- config.unknown-env-var (twelfth rule): reject an env:"EINHERJAR_*" struct tag
the framework doesn't declare. No-op until the server injects the name set, so
it never fires on incomplete knowledge.
Fixed (was v1.0.1): scaffold health hook + wire builtin used logz.Logger (real:
contracts/logging.Logger) and postgres.Component (hooks take Provider); env tags
were EINHERJAR_SERVER_ADDR / EINHERJAR_PG_DATABASE (real: _HOST/_PORT / _PG_NAME).
Tests: envspec unit tests; env tools against the real data/index.json; the rule.
Verified by generating the scaffold, building it against local einherjar v1.0.0
(exit 0), and runtime-loading the composed logz.Config (EINHERJAR_LOG_LEVEL=DEBUG
-> slog.LevelDebug, EINHERJAR_LOG_JSON=true). Version bumped to v1.1.0 (badge +
serverVersion). No dependency changes.
This commit is contained in:
@@ -84,6 +84,7 @@ import (
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
|
||||
"code.nochebuena.dev/einherjar/core/logz"
|
||||
"code.nochebuena.dev/einherjar/db-postgres"
|
||||
"code.nochebuena.dev/einherjar/web/server"
|
||||
)
|
||||
@@ -108,6 +109,7 @@ type Config struct {
|
||||
|
||||
// Framework component configs — composed verbatim. Their own EINHERJAR_* tags
|
||||
// load through this one env.Parse call.
|
||||
Log logz.Config // EINHERJAR_LOG_*
|
||||
Server server.Config // EINHERJAR_SERVER_*
|
||||
PG postgres.Config // EINHERJAR_PG_*
|
||||
}
|
||||
@@ -133,6 +135,21 @@ var, the same change adds its `env:"..."` tag to `config` **and** a documented l
|
||||
`.env.example`. This is not optional bookkeeping — it is what stops a long feature from shipping
|
||||
and then failing at boot because nobody knew which variables to set.
|
||||
|
||||
**Two kinds of var, two ways to keep them honest:**
|
||||
|
||||
- **Framework component vars (`EINHERJAR_*`)** — when you compose a new component later (e.g.
|
||||
`cachevalkey.Config`, `minio.Config`, `smtp.Config`), the MCP knows its real vars: call
|
||||
`get_config_env("<module>")` for the exact set (name, required, default) and add each to
|
||||
`.env.example`. The `config.unknown-env-var` rule rejects any `EINHERJAR_*` tag the framework
|
||||
doesn't declare, so a typo like `EINHERJAR_PG_DATABASE` is caught at `validate_snippet` time.
|
||||
- **App-owned vars (`APP_*`)** — like `APP_JWT_SECRET` above: the framework can't know these, so
|
||||
keeping them in `.env.example` is your discipline, not something it can name-check.
|
||||
|
||||
After you compose a component, run **`check_env`** with the modules the app composes: it flags
|
||||
`EINHERJAR_*` names that don't exist, required vars you forgot to document, and vars set for a
|
||||
module you don't actually compose (dead vars). `get_scaffold` already emits a `.env.example`
|
||||
derived from these same tags, so the starting point is correct by construction.
|
||||
|
||||
```bash
|
||||
# .env.example — copy to .env for local dev. Every var the app reads lives here.
|
||||
|
||||
@@ -142,19 +159,25 @@ APP_CORS_ORIGINS=*
|
||||
APP_JWT_SECRET=change-me
|
||||
APP_JWT_ISSUER=myapp
|
||||
|
||||
# ── Einherjar: logging (EINHERJAR_LOG_*) ──────────────────────────────────
|
||||
# EINHERJAR_LOG_LEVEL=INFO
|
||||
# EINHERJAR_LOG_JSON=false
|
||||
|
||||
# ── Einherjar: HTTP server (EINHERJAR_SERVER_*) ───────────────────────────
|
||||
EINHERJAR_SERVER_ADDR=:8080
|
||||
EINHERJAR_SERVER_HOST=0.0.0.0
|
||||
EINHERJAR_SERVER_PORT=8080
|
||||
|
||||
# ── Einherjar: PostgreSQL (EINHERJAR_PG_*) ────────────────────────────────
|
||||
EINHERJAR_PG_HOST=localhost
|
||||
EINHERJAR_PG_PORT=5432
|
||||
EINHERJAR_PG_USER=postgres
|
||||
EINHERJAR_PG_PASSWORD=postgres
|
||||
EINHERJAR_PG_DATABASE=myapp
|
||||
EINHERJAR_PG_NAME=myapp
|
||||
```
|
||||
|
||||
To discover the full set, walk every `env:"..."` tag reachable from `config.Config` (including the
|
||||
nested framework configs) — every one of them belongs in `.env.example`.
|
||||
To discover the full set for any component you compose, use `get_config_env("<module>")` rather
|
||||
than reading source by hand; every var it returns belongs in `.env.example`, and `check_env`
|
||||
confirms none are missing, misspelled, or dead.
|
||||
|
||||
## wire.go — Run()
|
||||
|
||||
@@ -166,8 +189,6 @@ appended, then feature hooks, then `lc.Run()`.
|
||||
package wire
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
authjwt "code.nochebuena.dev/einherjar/auth-jwt"
|
||||
@@ -189,10 +210,11 @@ func Run() error {
|
||||
return err
|
||||
}
|
||||
|
||||
logger := logz.New(logz.Config{
|
||||
JSON: !strings.EqualFold(cfg.AppEnv, "local"),
|
||||
StaticArgs: []any{"service", "myapp", "env", cfg.AppEnv},
|
||||
})
|
||||
// logz.Config is composed in config, so EINHERJAR_LOG_LEVEL / _JSON load from
|
||||
// the environment; StaticArgs are set here (they carry no env tag).
|
||||
logCfg := cfg.Log
|
||||
logCfg.StaticArgs = []any{"service", "myapp", "env", cfg.AppEnv}
|
||||
logger := logz.New(logCfg)
|
||||
|
||||
signer := authjwt.NewHMACSigner([]byte(cfg.JWT.Secret))
|
||||
|
||||
@@ -239,9 +261,9 @@ registration — lives inside the closure.
|
||||
package wire
|
||||
|
||||
import (
|
||||
"code.nochebuena.dev/einherjar/contracts/logging"
|
||||
"code.nochebuena.dev/einherjar/contracts/security"
|
||||
"code.nochebuena.dev/einherjar/core/launcher"
|
||||
"code.nochebuena.dev/einherjar/core/logz"
|
||||
"code.nochebuena.dev/einherjar/core/valid"
|
||||
"code.nochebuena.dev/einherjar/db-postgres"
|
||||
"code.nochebuena.dev/einherjar/web/server"
|
||||
@@ -255,8 +277,8 @@ import (
|
||||
func withUsers(
|
||||
lc launcher.Launcher,
|
||||
srv server.Server,
|
||||
db postgres.Component,
|
||||
logger logz.Logger,
|
||||
db postgres.Provider,
|
||||
logger logging.Logger,
|
||||
provider security.PermissionProvider,
|
||||
v valid.Validator,
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user