6119
My
mysis / files
mysis / internal / config / config.go
3.8 KB
1// Package config handles configuration loading from TOML files and environment variables. 2package config 3 4import ( 5 "errors" 6 "fmt" 7 "net/url" 8 "os" 9 "path/filepath" 10 11 "github.com/BurntSushi/toml" 12) 13 14// Config is the root configuration structure. 15type Config struct { 16 DefaultProvider string `toml:"default_provider"` 17 Providers map[string]ProviderConfig `toml:"providers"` 18 MCP MCPConfig `toml:"mcp"` 19} 20 21// ProviderConfig holds LLM provider settings. 22type ProviderConfig struct { 23 Endpoint string `toml:"endpoint"` 24 Model string `toml:"model"` 25 APIKeyName string `toml:"api_key_name"` 26 Temperature float64 `toml:"temperature"` 27} 28 29// MCPConfig holds MCP proxy settings. 30type MCPConfig struct { 31 Upstream string `toml:"upstream"` 32} 33 34// Load reads configuration from a TOML file and applies environment variable overrides. 35func Load(path string) (*Config, error) { 36 cfg := &Config{ 37 Providers: make(map[string]ProviderConfig), 38 } 39 40 // Config file is required 41 if path == "" { 42 return nil, fmt.Errorf("config path is required") 43 } 44 45 // File must exist 46 if _, err := os.Stat(path); err != nil { 47 return nil, fmt.Errorf("config file not found: %s", path) 48 } 49 50 // Load from file 51 _, err := toml.DecodeFile(path, cfg) 52 if err != nil { 53 return nil, fmt.Errorf("failed to parse config: %w", err) 54 } 55 56 // Apply environment variable overrides 57 applyEnvOverrides(cfg) 58 59 if err := cfg.Validate(); err != nil { 60 return nil, err 61 } 62 63 return cfg, nil 64} 65 66// Validate returns an error if the configuration is invalid. 67func (c *Config) Validate() error { 68 var errs []error 69 70 if len(c.Providers) == 0 { 71 errs = append(errs, errors.New("providers: at least one provider must be configured")) 72 } else { 73 for name, providerCfg := range c.Providers { 74 errs = append(errs, validateProviderConfig(name, providerCfg)...) 75 } 76 } 77 78 // Validate default provider if specified 79 if c.DefaultProvider != "" { 80 if _, ok := c.Providers[c.DefaultProvider]; !ok { 81 errs = append(errs, fmt.Errorf("default_provider=%q does not exist in providers", c.DefaultProvider)) 82 } 83 } 84 85 if len(errs) > 0 { 86 return errors.Join(errs...) 87 } 88 89 return nil 90} 91 92func validateProviderConfig(name string, cfg ProviderConfig) []error { 93 var errs []error 94 if cfg.Endpoint == "" { 95 errs = append(errs, fmt.Errorf("providers.%s.endpoint is required", name)) 96 } else if err := validateEndpoint(cfg.Endpoint); err != nil { 97 errs = append(errs, fmt.Errorf("providers.%s.endpoint=%q is invalid: %v", name, cfg.Endpoint, err)) 98 } 99 100 if cfg.Model == "" { 101 errs = append(errs, fmt.Errorf("providers.%s.model is required", name)) 102 } 103 104 if cfg.Temperature < 0.0 || cfg.Temperature > 2.0 { 105 errs = append(errs, fmt.Errorf("providers.%s.temperature=%v must be between 0.0 and 2.0", name, cfg.Temperature)) 106 } 107 108 return errs 109} 110 111func validateEndpoint(value string) error { 112 parsed, err := url.Parse(value) 113 if err != nil { 114 return err 115 } 116 if parsed.Scheme == "" || parsed.Host == "" { 117 return errors.New("missing scheme or host") 118 } 119 return nil 120} 121 122// applyEnvOverrides applies environment variable overrides to the configuration. 123func applyEnvOverrides(cfg *Config) { 124 for _, setter := range []struct { 125 env string 126 apply func(string) 127 }{ 128 {"MYSIS_MCP_ENDPOINT", func(v string) { 129 if v != "" { 130 cfg.MCP.Upstream = v 131 } 132 }}, 133 } { 134 setter.apply(os.Getenv(setter.env)) 135 } 136} 137 138// DataDir returns the path to the Mysis data directory (~/.config/mysis). 139func DataDir() (string, error) { 140 home, err := os.UserHomeDir() 141 if err != nil { 142 return "", err 143 } 144 return filepath.Join(home, ".config", "mysis"), nil 145} 146 147// EnsureDataDir creates the data directory if it doesn't exist. 148func EnsureDataDir() (string, error) { 149 dir, err := DataDir() 150 if err != nil { 151 return "", err 152 } 153 if err := os.MkdirAll(dir, 0750); err != nil { 154 return "", err 155 } 156 return dir, nil 157}