internal/config/config.go (view raw)
1package config
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7
8 "github.com/spf13/viper"
9
10 "llm_aggregator/internal/cli"
11 "llm_aggregator/internal/defaults"
12 "llm_aggregator/internal/progress"
13 "llm_aggregator/internal/runtime"
14 "llm_aggregator/internal/style"
15)
16
17// Config holds the application configuration loaded from TOML file, environment variables, and CLI arguments.
18type Config struct {
19 // Feed aggregation options
20 MaxArticlesPerFeed int `mapstructure:"max_articles_per_feed"`
21 MaxDaysOld int `mapstructure:"max_days_old"`
22 MaxTotalArticles int `mapstructure:"max_total_articles"`
23 IncludeKeywords string `mapstructure:"include_keywords"`
24 ExcludeKeywords string `mapstructure:"exclude_keywords"`
25
26 // Feed input
27 Stdin bool `mapstructure:"stdin"`
28
29 // LLM API options
30 APIKey string `mapstructure:"api_key"`
31 BaseURL string `mapstructure:"base_url"`
32 Model string `mapstructure:"model"`
33 MaxTokens int `mapstructure:"max_tokens"`
34 Temperature float64 `mapstructure:"temperature"`
35 Timeout int `mapstructure:"timeout"`
36
37 // System prompt
38 SystemPrompt string `mapstructure:"system_prompt"`
39
40 // Output options
41 Output string `mapstructure:"output"`
42 OutputFile string `mapstructure:"output_file"`
43 IncludeArticles bool `mapstructure:"include_articles"`
44 Plain bool `mapstructure:"plain"`
45}
46
47// GetViper returns the global viper instance with all configuration sources set up.
48// The precedence order is: CLI flags > Environment variables > Config file > Defaults.
49//
50// NOTE: Viper uses a singleton pattern. Subsequent calls to GetViper() return the SAME
51// instance with all previously set values. This means defaults are only set once,
52// environment vars are bound once, and the config file is read once. Do NOT call this
53// expecting a fresh state — if you need a fresh instance for testing, use viper.New() directly.
54func GetViper() *viper.Viper {
55 // Set defaults first (lowest priority)
56 setDefaults()
57 // Initialise or get existing Viper instance
58 v := viper.GetViper()
59 // Ensure defaults are set (for fresh instances)
60 setDefaultsOn(v)
61 // Set environment variable prefix
62 v.SetEnvPrefix("LLM_AGGREGATOR")
63 v.AutomaticEnv()
64 // Bind environment variables to config fields
65 bindEnvVars(v)
66 // Get config path from XDG
67 configPath, err := GetConfigPath()
68 if err == nil {
69 // Set config file path
70 v.SetConfigFile(configPath)
71 // Debug: show what config file is being used
72 // Try to read config file
73 if err := v.ReadInConfig(); err != nil {
74 // If config file doesn't exist, that's OK - we'll use defaults + env vars
75 if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
76 fmt.Fprintln(os.Stderr, style.Warningf("error reading config file: %v", err))
77 } else {
78 }
79 } else {
80 }
81 }
82 return v
83}
84
85// setDefaults sets default values on the global viper instance.
86func setDefaults() {
87 v := viper.GetViper()
88 setDefaultsOn(v)
89}
90
91// setDefaultsOn sets default values on the given viper instance.
92func setDefaultsOn(v *viper.Viper) {
93 // Feed aggregation defaults
94 v.SetDefault("max_articles_per_feed", defaults.DefaultMaxArticlesPerFeed)
95 v.SetDefault("max_days_old", defaults.DefaultMaxDaysOld)
96 v.SetDefault("max_total_articles", defaults.DefaultMaxTotalArticles)
97 // LLM API defaults
98 v.SetDefault("base_url", defaults.DefaultBaseURL)
99 v.SetDefault("model", defaults.DefaultModel)
100 v.SetDefault("max_tokens", defaults.DefaultMaxTokens)
101 v.SetDefault("temperature", defaults.DefaultTemperature)
102 v.SetDefault("timeout", defaults.DefaultLLMTimeout)
103 // System prompt default
104 v.SetDefault("system_prompt", defaults.DefaultSystemPrompt)
105 // Output defaults
106 v.SetDefault("output", defaults.DefaultOutput)
107 v.SetDefault("include_articles", defaults.DefaultIncludeArticles)
108}
109
110// BindCLIArgs binds CLI argument values to viper with highest precedence.
111// This should be called after parsing CLI arguments to override config file/env defaults.
112func BindCLIArgs(v *viper.Viper, args map[string]any) {
113 for key, value := range args {
114 if value != nil && !isZero(value) {
115 v.Set(key, value)
116 }
117 }
118}
119
120// isZero checks if a value is the zero value for its type.
121//
122// NOTE: This function is critical for CLI argument handling. Pointer types
123// (*string, *int, etc.) return true only when nil (not provided), NOT when
124// pointing to a zero value. This allows distinguishing between "flag not
125// provided" (nil) vs "flag explicitly set to zero" (e.g., --temperature 0
126// should override default, but no flag means preserve default).
127func isZero(v any) bool {
128 // Handle nil interface values first (before type switch)
129 if v == nil {
130 return true
131 }
132 switch val := v.(type) {
133 case string:
134 return val == ""
135 case int:
136 return val == 0
137 case int8:
138 return val == 0
139 case int16:
140 return val == 0
141 case int32:
142 return val == 0
143 case int64:
144 return val == 0
145 case float32:
146 return val == 0.0
147 case float64:
148 return val == 0.0
149 case bool:
150 return !val
151 case *string:
152 return val == nil
153 case *int:
154 return val == nil
155 case *int8:
156 return val == nil
157 case *int16:
158 return val == nil
159 case *int32:
160 return val == nil
161 case *int64:
162 return val == nil
163 case *float32:
164 return val == nil
165 case *float64:
166 return val == nil
167 case *bool:
168 return val == nil
169 default:
170 return false
171 }
172}
173
174// ViperToRuntime converts viper configuration directly to runtime.Runtime.
175func ViperToRuntime(v *viper.Viper, feedsFile, prompt string) *runtime.Runtime {
176 rt := &runtime.Runtime{
177 FeedsFile: feedsFile,
178 Prompt: prompt,
179 Progress: &progress.NoopLogger{},
180 }
181 // Feed aggregation options
182 rt.MaxArticlesPerFeed = v.GetInt("max_articles_per_feed")
183 rt.MaxDaysOld = v.GetInt("max_days_old")
184 rt.MaxTotalArticles = v.GetInt("max_total_articles")
185 // Keywords
186 includeKeywords := v.GetString("include_keywords")
187 excludeKeywords := v.GetString("exclude_keywords")
188 if includeKeywords != "" {
189 rt.IncludeKeywords = cli.ParseKeywords(includeKeywords)
190 }
191 if excludeKeywords != "" {
192 rt.ExcludeKeywords = cli.ParseKeywords(excludeKeywords)
193 }
194 // LLM API options
195 rt.APIKey = v.GetString("api_key")
196 rt.BaseURL = v.GetString("base_url")
197 rt.Model = v.GetString("model")
198 rt.MaxTokens = v.GetInt("max_tokens")
199 rt.Temperature = v.GetFloat64("temperature")
200 rt.LLMTimeout = v.GetInt("timeout")
201 // System prompt
202 rt.SystemPrompt = v.GetString("system_prompt")
203 // Output options
204 rt.Output = v.GetString("output")
205 rt.OutputFile = v.GetString("output_file")
206 rt.IncludeArticles = v.GetBool("include_articles")
207 rt.Plain = v.GetBool("plain")
208 rt.Stdin = v.GetBool("stdin")
209 return rt
210}
211
212// bindEnvVars binds environment variables to viper keys.
213func bindEnvVars(v *viper.Viper) {
214 // Feed aggregation options
215 v.BindEnv("max_articles_per_feed", "LLM_AGGREGATOR_MAX_ARTICLES_PER_FEED")
216 v.BindEnv("max_days_old", "LLM_AGGREGATOR_MAX_DAYS_OLD")
217 v.BindEnv("max_total_articles", "LLM_AGGREGATOR_MAX_TOTAL_ARTICLES")
218 v.BindEnv("include_keywords", "LLM_AGGREGATOR_INCLUDE_KEYWORDS")
219 v.BindEnv("exclude_keywords", "LLM_AGGREGATOR_EXCLUDE_KEYWORDS")
220 // LLM API options
221 v.BindEnv("api_key", "LLM_AGGREGATOR_API_KEY")
222 v.BindEnv("base_url", "LLM_AGGREGATOR_BASE_URL")
223 v.BindEnv("model", "LLM_AGGREGATOR_MODEL")
224 v.BindEnv("max_tokens", "LLM_AGGREGATOR_MAX_TOKENS")
225 v.BindEnv("temperature", "LLM_AGGREGATOR_TEMPERATURE")
226 v.BindEnv("timeout", "LLM_AGGREGATOR_TIMEOUT")
227 // System prompt
228 v.BindEnv("system_prompt", "LLM_AGGREGATOR_SYSTEM_PROMPT")
229 // Output options
230 v.BindEnv("output", "LLM_AGGREGATOR_OUTPUT")
231 v.BindEnv("output_file", "LLM_AGGREGATOR_OUTPUT_FILE")
232 v.BindEnv("include_articles", "LLM_AGGREGATOR_INCLUDE_ARTICLES")
233 v.BindEnv("plain", "LLM_AGGREGATOR_PLAIN")
234 // Feed input
235 v.BindEnv("stdin", "LLM_AGGREGATOR_STDIN")
236}
237
238// GetConfigPath returns the path to the config file.
239func GetConfigPath() (string, error) {
240 configDir, err := os.UserConfigDir()
241 if err != nil {
242 return "", fmt.Errorf("failed to get user config directory: %w", err)
243 }
244 return filepath.Join(configDir, "llm_aggregator", "config.toml"), nil
245}