feat(config): add yaml config with generic env override
Adds internal/config package using gopkg.in/yaml.v3 and reflection for generic environment variable overrides. Updates main.go to load config and set log level. Adds tests and example config.
This commit is contained in:
43
internal/config/config_test.go
Normal file
43
internal/config/config_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
// Create a temporary config file
|
||||
tmpDir := t.TempDir()
|
||||
configFile := filepath.Join(tmpDir, "config.yaml")
|
||||
|
||||
yamlContent := []byte(`
|
||||
log_level: "info"
|
||||
`)
|
||||
if err := os.WriteFile(configFile, yamlContent, 0644); err != nil {
|
||||
t.Fatalf("failed to write temp config file: %v", err)
|
||||
}
|
||||
|
||||
t.Run("Load from YAML", func(t *testing.T) {
|
||||
cfg, err := Load(configFile, "TEST")
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error = %v", err)
|
||||
}
|
||||
if cfg.LogLevel != "info" {
|
||||
t.Errorf("expected LogLevel 'info', got '%s'", cfg.LogLevel)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Override from Env", func(t *testing.T) {
|
||||
os.Setenv("TEST_LOG_LEVEL", "debug")
|
||||
defer os.Unsetenv("TEST_LOG_LEVEL")
|
||||
|
||||
cfg, err := Load(configFile, "TEST")
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error = %v", err)
|
||||
}
|
||||
if cfg.LogLevel != "debug" {
|
||||
t.Errorf("expected LogLevel 'debug', got '%s'", cfg.LogLevel)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user