KAOS/code/internal/config/config.go
djuka 098ed13705 T22: Reorganizacija testova + logs handler + konzola fix
- Razbijen monolitni server_test.go na fokusirane test fajlove:
  api_test.go, dashboard_test.go, docs_test.go, search_test.go,
  submit_test.go, task_detail_test.go, console_test.go, sse_test.go,
  timestamp_test.go, ui_test.go, test_helpers_test.go
- Dodat logs.go handler (handleLogsTail) koji je nedostajao
- Dodat LogFile u config
- Fix konzola: prompt se šalje preko fajla umesto direktno u PTY
- 192 testova prolazi

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 04:45:50 +00:00

99 lines
2.3 KiB
Go

// Package config handles loading and validating configuration
// for the KAOS supervisor from environment variables.
package config
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
// Config holds the supervisor configuration values.
type Config struct {
// Timeout is the maximum duration for supervisor operations.
Timeout time.Duration
// ProjectPath is the root path of the project being supervised.
ProjectPath string
// TasksDir is the path to the TASKS directory.
TasksDir string
// Port is the HTTP server port.
Port string
// LogFile is the path to the server log file (optional).
LogFile string
}
// Load reads configuration from environment variables.
// It first attempts to load a .env file from the current directory.
// Required variables: KAOS_TIMEOUT, KAOS_PROJECT_PATH.
func Load() (*Config, error) {
loadEnvFile(".env")
timeoutStr := os.Getenv("KAOS_TIMEOUT")
if timeoutStr == "" {
return nil, fmt.Errorf("KAOS_TIMEOUT environment variable is required")
}
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
return nil, fmt.Errorf("invalid KAOS_TIMEOUT value %q: %w", timeoutStr, err)
}
projectPath := os.Getenv("KAOS_PROJECT_PATH")
if projectPath == "" {
return nil, fmt.Errorf("KAOS_PROJECT_PATH environment variable is required")
}
tasksDir := os.Getenv("KAOS_TASKS_DIR")
if tasksDir == "" {
return nil, fmt.Errorf("KAOS_TASKS_DIR environment variable is required")
}
port := os.Getenv("KAOS_PORT")
if port == "" {
port = "8080"
}
logFile := os.Getenv("KAOS_LOG_FILE")
return &Config{
Timeout: timeout,
ProjectPath: projectPath,
TasksDir: tasksDir,
Port: port,
LogFile: logFile,
}, nil
}
// loadEnvFile reads a .env file and sets environment variables
// that are not already set. Lines starting with # are ignored.
func loadEnvFile(path string) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, value, ok := strings.Cut(line, "=")
if !ok {
continue
}
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
// Don't override existing environment variables
if _, exists := os.LookupEnv(key); !exists {
os.Setenv(key, value)
}
}
}