feat: bootstrap goaichat CLI and config system

This commit is contained in:
2025-10-01 14:47:32 +02:00
parent 12a65231ef
commit 0fd24a5cfb
14 changed files with 1082 additions and 3 deletions

127
internal/config/config.go Normal file
View File

@@ -0,0 +1,127 @@
package config
import (
"errors"
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
const (
envAPIKey = "GOAICHAT_API_KEY"
envAPIURL = "GOAICHAT_API_URL"
)
// Config captures runtime configuration for the Goaichat application.
type Config struct {
API APIConfig `yaml:"api"`
Model ModelConfig `yaml:"model"`
Logging LoggingConfig `yaml:"logging"`
UI UIConfig `yaml:"ui"`
}
// APIConfig holds settings for connecting to the OpenAI-compatible API.
type APIConfig struct {
URL string `yaml:"url"`
Key string `yaml:"key"`
}
// ModelConfig controls default model behaviour.
type ModelConfig struct {
Name string `yaml:"name"`
Temperature float64 `yaml:"temperature"`
Stream bool `yaml:"stream"`
}
// LoggingConfig encapsulates logging preferences.
type LoggingConfig struct {
Level string `yaml:"level"`
}
// UIConfig defines terminal rendering preferences.
type UIConfig struct {
ShowTimestamps bool `yaml:"show_timestamps"`
}
// Load reads configuration from the provided path, falling back to defaults and
// environment overrides.
func Load(path string) (*Config, error) {
cfg := defaultConfig()
if path != "" {
if err := loadFile(path, &cfg); err != nil {
return nil, err
}
} else {
if err := loadFile("config.yaml", &cfg); err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
}
applyEnvOverrides(&cfg)
if err := cfg.validate(); err != nil {
return nil, err
}
return &cfg, nil
}
func loadFile(path string, cfg *Config) error {
data, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return err
}
return fmt.Errorf("read config: %w", err)
}
if err := yaml.Unmarshal(data, cfg); err != nil {
return fmt.Errorf("parse config: %w", err)
}
return nil
}
func applyEnvOverrides(cfg *Config) {
if url := strings.TrimSpace(os.Getenv(envAPIURL)); url != "" {
cfg.API.URL = url
}
if key := strings.TrimSpace(os.Getenv(envAPIKey)); key != "" {
cfg.API.Key = key
}
}
func (c *Config) validate() error {
if strings.TrimSpace(c.API.URL) == "" {
return errors.New("api.url must be set")
}
if strings.TrimSpace(c.API.Key) == "" {
return errors.New("api.key must be set or GOAICHAT_API_KEY provided")
}
if c.Model.Temperature < 0 || c.Model.Temperature > 2 {
return fmt.Errorf("model.temperature must be between 0 and 2, got %f", c.Model.Temperature)
}
return nil
}
func defaultConfig() Config {
return Config{
API: APIConfig{
URL: "https://api.openai.com/v1",
},
Model: ModelConfig{
Name: "gpt-4o-mini",
Temperature: 0.7,
Stream: true,
},
Logging: LoggingConfig{
Level: "info",
},
UI: UIConfig{
ShowTimestamps: true,
},
}
}

View File

@@ -0,0 +1,97 @@
package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoad_DefaultConfigWithEnvOverrides(t *testing.T) {
t.Setenv(envAPIKey, "test-key")
t.Setenv(envAPIURL, "https://example.com")
cfg, err := Load("")
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if cfg.API.URL != "https://example.com" {
t.Fatalf("expected API URL override, got %q", cfg.API.URL)
}
if cfg.API.Key != "test-key" {
t.Fatalf("expected API key override, got %q", cfg.API.Key)
}
if cfg.Model.Name != "gpt-4o-mini" {
t.Fatalf("expected default model name, got %q", cfg.Model.Name)
}
}
func TestLoad_FromFile(t *testing.T) {
t.Setenv(envAPIKey, "")
t.Setenv(envAPIURL, "")
dir := t.TempDir()
configPath := filepath.Join(dir, "config.yaml")
content := []byte("api:\n url: https://api.test/v1\n key: test-token\nmodel:\n name: gpt-test\n temperature: 0.5\n stream: false\n")
if err := os.WriteFile(configPath, content, 0o600); err != nil {
t.Fatalf("failed to write config file: %v", err)
}
cfg, err := Load(configPath)
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if cfg.API.URL != "https://api.test/v1" {
t.Errorf("expected API URL %q, got %q", "https://api.test/v1", cfg.API.URL)
}
if cfg.API.Key != "test-token" {
t.Errorf("expected API key %q, got %q", "test-token", cfg.API.Key)
}
if cfg.Model.Name != "gpt-test" {
t.Errorf("expected model name %q, got %q", "gpt-test", cfg.Model.Name)
}
if cfg.Model.Temperature != 0.5 {
t.Errorf("expected temperature 0.5, got %f", cfg.Model.Temperature)
}
if cfg.Model.Stream != false {
t.Errorf("expected stream false, got %t", cfg.Model.Stream)
}
}
func TestLoad_InvalidTemperature(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "config.yaml")
content := []byte("api:\n url: https://api.test/v1\n key: test-token\nmodel:\n name: gpt-test\n temperature: 5\n")
if err := os.WriteFile(configPath, content, 0o600); err != nil {
t.Fatalf("failed to write config file: %v", err)
}
_, err := Load(configPath)
if err == nil {
t.Fatal("expected error for invalid temperature, got none")
}
}
func TestLoad_MissingAPIKey(t *testing.T) {
// Ensure no environment fallback is present.
t.Setenv(envAPIKey, "")
t.Setenv(envAPIURL, "")
dir := t.TempDir()
configPath := filepath.Join(dir, "config.yaml")
content := []byte("api:\n url: https://api.test/v1\nmodel:\n name: gpt-test\n temperature: 0.5\n")
if err := os.WriteFile(configPath, content, 0o600); err != nil {
t.Fatalf("failed to write config file: %v", err)
}
_, err := Load(configPath)
if err == nil {
t.Fatal("expected error for missing API key, got none")
}
}