Enhance configuration to support loading IMAP password and OpenRouter API key from files, and update README with new settings

This commit is contained in:
2025-03-01 23:47:45 +01:00
parent 7698656c6e
commit be9f2fea8f
3 changed files with 37 additions and 4 deletions

View File

@@ -49,6 +49,14 @@ type LoggingConfig struct {
FilePath string `yaml:"file_path"`
}
func readFileContent(path string) (string, error) {
content, err := os.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(content)), nil
}
func Load(path string) (*Config, error) {
logger.WithField("path", path).Debug("Loading configuration file")
@@ -65,17 +73,36 @@ func Load(path string) (*Config, error) {
return nil, err
}
// Resolve environment variables in configuration
// Handle IMAP password from environment or file
if strings.HasPrefix(config.IMAP.Password, "${") && strings.HasSuffix(config.IMAP.Password, "}") {
envVar := strings.TrimSuffix(strings.TrimPrefix(config.IMAP.Password, "${"), "}")
config.IMAP.Password = os.Getenv(envVar)
logger.WithField("envVar", envVar).Debug("Resolved IMAP password from environment")
} else if strings.HasPrefix(config.IMAP.Password, "file://") {
filePath := strings.TrimPrefix(config.IMAP.Password, "file://")
password, err := readFileContent(filePath)
if err != nil {
logger.WithError(err).Error("Failed to read IMAP password from file")
return nil, err
}
config.IMAP.Password = password
logger.WithField("path", filePath).Debug("Read IMAP password from file")
}
// Handle OpenRouter API key from environment or file
if strings.HasPrefix(config.AI.OpenRouterAPIKey, "${") && strings.HasSuffix(config.AI.OpenRouterAPIKey, "}") {
envVar := strings.TrimSuffix(strings.TrimPrefix(config.AI.OpenRouterAPIKey, "${"), "}")
config.AI.OpenRouterAPIKey = os.Getenv(envVar)
logger.WithField("envVar", envVar).Debug("Resolved OpenRouter API key from environment")
} else if strings.HasPrefix(config.AI.OpenRouterAPIKey, "file://") {
filePath := strings.TrimPrefix(config.AI.OpenRouterAPIKey, "file://")
apiKey, err := readFileContent(filePath)
if err != nil {
logger.WithError(err).Error("Failed to read OpenRouter API key from file")
return nil, err
}
config.AI.OpenRouterAPIKey = apiKey
logger.WithField("path", filePath).Debug("Read OpenRouter API key from file")
}
logger.WithFields(logrus.Fields{