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.
Reviewed-on: #3
Co-authored-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
Co-committed-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
83 lines
2.5 KiB
Go
83 lines
2.5 KiB
Go
package rules
|
|
|
|
import (
|
|
"go/ast"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
// knownEnvVars is the set of every real framework env-var name, injected once at
|
|
// server startup from the index (see rules.SetKnownEnvVars). When empty — e.g.
|
|
// in unit tests that exercise rules without an index — config.unknown-env-var is
|
|
// a no-op, so it never fires on incomplete knowledge.
|
|
var knownEnvVars map[string]struct{}
|
|
|
|
// SetKnownEnvVars installs the authoritative set of framework env-var names that
|
|
// backs config.unknown-env-var. Call once before serving; the server derives the
|
|
// set from the index so the rule can never drift from the real struct tags.
|
|
func SetKnownEnvVars(names map[string]struct{}) {
|
|
knownEnvVars = names
|
|
}
|
|
|
|
func init() {
|
|
registered = append(registered,
|
|
Rule{
|
|
ID: "config.unknown-env-var",
|
|
Severity: SeverityError,
|
|
Module: "wire",
|
|
Check: checkUnknownEnvVar,
|
|
},
|
|
)
|
|
}
|
|
|
|
// checkUnknownEnvVar flags any struct field tagged with an EINHERJAR_* env var
|
|
// that the framework does not actually declare — the exact class of drift that
|
|
// shipped EINHERJAR_PG_DATABASE (real name: _PG_NAME) and EINHERJAR_SERVER_ADDR
|
|
// (real: _SERVER_HOST/_PORT). App-owned prefixes (APP_*) are never flagged.
|
|
func checkUnknownEnvVar(c *Context) []Finding {
|
|
if len(knownEnvVars) == 0 {
|
|
return nil
|
|
}
|
|
var hits []Finding
|
|
ast.Inspect(c.File, func(n ast.Node) bool {
|
|
st, ok := n.(*ast.StructType)
|
|
if !ok || st.Fields == nil {
|
|
return true
|
|
}
|
|
for _, field := range st.Fields.List {
|
|
if field.Tag == nil {
|
|
continue
|
|
}
|
|
name, ok := envName(strings.Trim(field.Tag.Value, "`"))
|
|
if !ok || !strings.HasPrefix(name, "EINHERJAR_") {
|
|
continue
|
|
}
|
|
if _, real := knownEnvVars[name]; real {
|
|
continue
|
|
}
|
|
hits = append(hits, Finding{
|
|
Message: name + " is not a real Einherjar env var (invented or misspelled)",
|
|
Hint: "Verify the exact tag with get_config_env. The framework never invents EINHERJAR_* names; compose the component's real Config or fix the tag.",
|
|
Line: c.Fset.Position(field.Tag.Pos()).Line,
|
|
})
|
|
}
|
|
return true
|
|
})
|
|
return hits
|
|
}
|
|
|
|
// envName extracts the env-var name from a raw (backtick-stripped) struct tag,
|
|
// dropping the ,required / ,unset options. ok is false when there is no env key
|
|
// or it is "-".
|
|
func envName(tag string) (string, bool) {
|
|
raw, present := reflect.StructTag(tag).Lookup("env")
|
|
if !present {
|
|
return "", false
|
|
}
|
|
name := strings.TrimSpace(strings.Split(raw, ",")[0])
|
|
if name == "" || name == "-" {
|
|
return "", false
|
|
}
|
|
return name, true
|
|
}
|