Abort startup on an unknown PIXA_ environment variable (closes #133)
check / check (push) Successful in 3m15s
check / check (push) Successful in 3m15s
A variable whose name starts with PIXA_ but is neither a setting's variable, from the list pairing each config key with its variable, nor PIXA_CONFIG_PATH now aborts startup naming it, as an unknown config key does. PIXA_PORT is named with a pointer to PORT. The check runs before the config file loads, so the variables the file's env section sets are not checked. README.md says so under Configuration. Model: opus-5-5
This commit is contained in:
@@ -58,6 +58,7 @@ var (
|
||||
errValueRequired = errors.New("a value is required")
|
||||
errValueEmpty = errors.New("value must not be empty")
|
||||
errUnknownConfigKeys = errors.New("unknown config keys")
|
||||
errUnknownEnvVars = errors.New("unknown environment variables")
|
||||
errNotAString = errors.New("not a string")
|
||||
errNotAnInteger = errors.New("not an integer")
|
||||
errNotABoolean = errors.New("not a boolean")
|
||||
@@ -146,6 +147,11 @@ type Config struct {
|
||||
func New(_ fx.Lifecycle, params Params) (*Config, error) {
|
||||
log := params.Logger.Get()
|
||||
|
||||
err := validateKnownEnvVars()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Look for the config file under the project name (/etc/pixa/,
|
||||
// ~/.config/pixa/), matching the /var/lib/pixa state directory,
|
||||
// not under the daemon name pixad.
|
||||
@@ -377,6 +383,44 @@ func envVarNames() map[string]string {
|
||||
}
|
||||
}
|
||||
|
||||
// validateKnownEnvVars rejects environment variables whose names start
|
||||
// with PIXA_ but that are neither a setting's variable nor
|
||||
// PIXA_CONFIG_PATH, so a misspelled variable fails at startup instead of
|
||||
// being silently ignored, as validateKnownKeys does for config file keys.
|
||||
// New calls it before loading the config file, so the variables the
|
||||
// file's env section sets are not checked, just as validateKnownKeys
|
||||
// leaves that section's contents alone.
|
||||
func validateKnownEnvVars() error {
|
||||
known := map[string]bool{"PIXA_CONFIG_PATH": true}
|
||||
|
||||
for _, name := range envVarNames() {
|
||||
known[name] = true
|
||||
}
|
||||
|
||||
var unknown []string
|
||||
|
||||
for _, entry := range os.Environ() {
|
||||
name, _, _ := strings.Cut(entry, "=")
|
||||
|
||||
switch {
|
||||
case !strings.HasPrefix(name, "PIXA_") || known[name]:
|
||||
continue
|
||||
case name == "PIXA_PORT":
|
||||
unknown = append(unknown, name+" (use PORT for the port)")
|
||||
default:
|
||||
unknown = append(unknown, name)
|
||||
}
|
||||
}
|
||||
|
||||
if len(unknown) > 0 {
|
||||
sort.Strings(unknown)
|
||||
|
||||
return fmt.Errorf("%w: %s", errUnknownEnvVars, strings.Join(unknown, ", "))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// lookupValue returns the value set for key and whether one is set. The
|
||||
// key's environment variable wins when it is present, even when empty;
|
||||
// its value is a string, read exactly as the same text quoted in the
|
||||
|
||||
Reference in New Issue
Block a user