mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-09-11 14:00:29 +00:00
93c62e691a
Six small refactors aggregated from a simplify-review pass over this session's commits: 1. internal/config/persistence_relay.go — LoadRelayConfig had two ApplyEnvOverrides call sites (one inside the not-exist branch, one on the happy path) and a redundant cfg = DefaultConfig() reassignment. Collapse to a single ApplyEnvOverrides call after the load attempt; the file-absent branch already has the default cfg from line 1. 2. internal/relay/config_env.go — swap two strings.TrimSpace(os.Getenv(...)) calls for utils.GetenvTrim, matching the 30+ existing call sites in internal/config/config.go. Trim narrating comments back to the product-behavior sentences that aren't obvious from the code. 3. internal/relay/config_env_test.go — collapse seven near-identical ApplyEnvOverrides scenarios into a single table-driven test (TestApplyEnvOverridesTable). Reduces ~85 lines to ~60 and gives each subcase a named t.Run for clearer failure output. Keeps the nil-config-safe and parseEnvBool tests separate since they exercise different surfaces. 4. .github/workflows/install-sh-smoke.yml — replace the /api/health bash for-loop (sleep 2; curl; loop 30x) with a single curl --retry 30 --retry-delay 2 --retry-connrefused --retry-all-errors invocation. Curl already implements the same polling behaviour natively; the bash loop was 13 lines of redundant scaffolding. 5. scripts/installtests/build_release_assets_test.go — extract the repeated "read file, iterate required substrings, fail on first miss" boilerplate into assertFileContainsAll(t, path, required...). Migrate the four tests I added in this session; existing tests in the file follow the same shape and can adopt the helper incrementally without churning unrelated code in this commit. Also updated the pinned curl string for the /api/health retry change. Contract-neutral: every change preserves identical user-visible behavior. PULSE_ALLOW_CONTRACT_NEUTRAL_COMMIT applied for the canonical-shape-guard bypass; sensitivity, gitleaks, governance-stage, control-plane, status, registry, contract, and pre-commit hooks still run. Verified locally: - go test ./internal/relay/ ./internal/config/ → all pass - go test ./scripts/installtests/ → all pass - ruby -ryaml install-sh-smoke.yml → parses clean
70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package relay
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/utils"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// Env vars for headless / container deployments that bootstrap relay without
|
|
// going through Settings → Relay. Saving from the UI after an override is
|
|
// active persists the env-effective state to disk, so clearing the env alone
|
|
// does not revert.
|
|
const (
|
|
EnvRelayEnabled = "PULSE_RELAY_ENABLED"
|
|
EnvRelayServerURL = "PULSE_RELAY_SERVER"
|
|
)
|
|
|
|
// ApplyEnvOverrides mutates cfg in place to reflect PULSE_RELAY_* environment
|
|
// overrides. Unset / empty / unparseable env vars leave the file value
|
|
// untouched; invalid server URLs are logged and ignored.
|
|
func ApplyEnvOverrides(cfg *Config) {
|
|
if cfg == nil {
|
|
return
|
|
}
|
|
|
|
if rawEnabled := utils.GetenvTrim(EnvRelayEnabled); rawEnabled != "" {
|
|
if parsed, ok := parseEnvBool(rawEnabled); ok {
|
|
cfg.Enabled = parsed
|
|
log.Info().
|
|
Str("env_var", EnvRelayEnabled).
|
|
Bool("enabled", parsed).
|
|
Msg("relay configuration overridden by environment variable")
|
|
} else {
|
|
log.Warn().
|
|
Str("env_var", EnvRelayEnabled).
|
|
Str("value", rawEnabled).
|
|
Msg("relay env override is not a recognized boolean; ignoring")
|
|
}
|
|
}
|
|
|
|
if rawURL := utils.GetenvTrim(EnvRelayServerURL); rawURL != "" {
|
|
if err := validateRelayServerURL(rawURL); err != nil {
|
|
log.Warn().
|
|
Str("env_var", EnvRelayServerURL).
|
|
Str("value", rawURL).
|
|
Err(err).
|
|
Msg("relay env override is not a valid ws/wss URL; keeping persisted value")
|
|
} else {
|
|
cfg.ServerURL = rawURL
|
|
log.Info().
|
|
Str("env_var", EnvRelayServerURL).
|
|
Str("server_url", rawURL).
|
|
Msg("relay configuration overridden by environment variable")
|
|
}
|
|
}
|
|
}
|
|
|
|
// parseEnvBool distinguishes "unset" from "explicit false"; utils.ParseBool
|
|
// can't (it coerces everything unrecognized to false).
|
|
func parseEnvBool(rawValue string) (value bool, ok bool) {
|
|
switch strings.ToLower(strings.TrimSpace(rawValue)) {
|
|
case "1", "true", "yes", "y", "on":
|
|
return true, true
|
|
case "0", "false", "no", "n", "off":
|
|
return false, true
|
|
}
|
|
return false, false
|
|
}
|