- go mod init github.com/dal/kaos - Config paket sa .env učitavanjem i validacijom - Supervisor skeleton paket - Entry point (cmd/kaos-supervisor/main.go) - Makefile (build, test, vet, clean, all) - .env.example, .gitignore - 6 config testova — svi prolaze Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
78 lines
1.8 KiB
Go
78 lines
1.8 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
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
|
|
return &Config{
|
|
Timeout: timeout,
|
|
ProjectPath: projectPath,
|
|
}, 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)
|
|
}
|
|
}
|
|
}
|