feat(config): support cli config path and nested config
All checks were successful
CI / Lint (pull_request) Successful in 5s
CI / Test (pull_request) Successful in 8s
CI / Build (pull_request) Successful in 7s
CI / Docker Build (pull_request) Successful in 23s

Adds -config CLI flag to main.go. Moves ParseLogLevel to internal/config. Adds ServerConfig nested struct to Config and corresponding tests for yaml/env overrides.
This commit is contained in:
Gemini CLI
2026-01-15 18:37:01 +00:00
parent 6623ced227
commit ec689c6152
3 changed files with 79 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
package config
import (
"log/slog"
"os"
"path/filepath"
"testing"
@@ -13,6 +14,9 @@ func TestLoad(t *testing.T) {
yamlContent := []byte(`
log_level: "info"
server:
host: "localhost"
port: 8080
`)
if err := os.WriteFile(configFile, yamlContent, 0644); err != nil {
t.Fatalf("failed to write temp config file: %v", err)
@@ -26,11 +30,23 @@ log_level: "info"
if cfg.LogLevel != "info" {
t.Errorf("expected LogLevel 'info', got '%s'", cfg.LogLevel)
}
if cfg.Server.Host != "localhost" {
t.Errorf("expected Server.Host 'localhost', got '%s'", cfg.Server.Host)
}
if cfg.Server.Port != 8080 {
t.Errorf("expected Server.Port 8080, got %d", cfg.Server.Port)
}
})
t.Run("Override from Env", func(t *testing.T) {
os.Setenv("TEST_LOG_LEVEL", "debug")
defer os.Unsetenv("TEST_LOG_LEVEL")
os.Setenv("TEST_SERVER_PORT", "9090")
os.Setenv("TEST_SERVER_HOST", "0.0.0.0")
defer func() {
os.Unsetenv("TEST_LOG_LEVEL")
os.Unsetenv("TEST_SERVER_PORT")
os.Unsetenv("TEST_SERVER_HOST")
}()
cfg, err := Load(configFile, "TEST")
if err != nil {
@@ -39,5 +55,34 @@ log_level: "info"
if cfg.LogLevel != "debug" {
t.Errorf("expected LogLevel 'debug', got '%s'", cfg.LogLevel)
}
if cfg.Server.Port != 9090 {
t.Errorf("expected Server.Port 9090, got %d", cfg.Server.Port)
}
if cfg.Server.Host != "0.0.0.0" {
t.Errorf("expected Server.Host '0.0.0.0', got '%s'", cfg.Server.Host)
}
})
}
func TestParseLogLevel(t *testing.T) {
tests := []struct {
input string
expected slog.Level
}{
{"debug", slog.LevelDebug},
{"DEBUG", slog.LevelDebug},
{"info", slog.LevelInfo},
{"warn", slog.LevelWarn},
{"error", slog.LevelError},
{"unknown", slog.LevelInfo},
{"", slog.LevelInfo},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
if got := ParseLogLevel(tt.input); got != tt.expected {
t.Errorf("ParseLogLevel(%q) = %v, want %v", tt.input, got, tt.expected)
}
})
}
}