config/env.go
Claude 59b968b1ef
feat: upgrade to core v0.8.0-alpha.1, replace banned stdlib imports
Migrate from forge.lthn.ai deps to dappco.re. Replace os, strings,
path/filepath with Core primitives. OnStartup returns core.Result.
Service factory returns core.Result.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 14:06:13 +00:00

53 lines
1.1 KiB
Go

package config
import (
"iter"
"os"
core "dappco.re/go/core"
)
// Env returns an iterator over environment variables with the given prefix,
// providing them as dot-notation keys and values.
//
// For example, with prefix "CORE_CONFIG_":
//
// CORE_CONFIG_FOO_BAR=baz -> yields ("foo.bar", "baz")
func Env(prefix string) iter.Seq2[string, any] {
return func(yield func(string, any) bool) {
for _, env := range os.Environ() {
if !core.HasPrefix(env, prefix) {
continue
}
parts := core.SplitN(env, "=", 2)
if len(parts) != 2 {
continue
}
name := parts[0]
value := parts[1]
// Strip prefix and convert to dot notation
key := core.TrimPrefix(name, prefix)
key = core.Lower(key)
key = core.Replace(key, "_", ".")
if !yield(key, value) {
return
}
}
}
}
// LoadEnv parses environment variables with the given prefix and returns
// them as a flat map with dot-notation keys.
//
// Deprecated: Use Env for iterative access or collect into a map manually.
func LoadEnv(prefix string) map[string]any {
result := make(map[string]any)
for k, v := range Env(prefix) {
result[k] = v
}
return result
}