updated config logic
All checks were successful
Go / build (push) Successful in 20s

This commit is contained in:
Darius klein 2025-11-17 21:27:53 +01:00
parent 5205c2fb22
commit 2117fcc783

View File

@ -14,46 +14,56 @@ import (
var DefaultConfigFile []byte var DefaultConfigFile []byte
var File Config var File Config
// WARNING: this code is ai generated
func Read() error { func Read() error {
const SystemConfigPath = "/etc/wazuh-notify/wazuh-notify-config.toml" const SystemConfigPath = "/etc/wazuh-notify/wazuh-notify-config.toml"
execPath, _ := os.Executable() execPath, _ := os.Executable()
LocalConfigPath := path.Join(path.Dir(execPath), "wazuh-notify-config.toml") LocalConfigPath := path.Join(path.Dir(execPath), "wazuh-notify-config.toml")
var tomlFile []byte err := toml.Unmarshal(DefaultConfigFile, &File)
var err error
tomlFile, err = os.ReadFile(SystemConfigPath)
if err != nil { if err != nil {
tomlFile, err = os.ReadFile(LocalConfigPath) log.Log(fmt.Sprintf("CRITICAL: Failed to parse embedded default config: %v", err))
return err
} }
if err != nil { var userTomlFile []byte
log.Log("Config not found. Attempting to create default.") var readErr error
var configPath string
userTomlFile, readErr = os.ReadFile(SystemConfigPath)
if readErr == nil {
configPath = SystemConfigPath
} else {
userTomlFile, readErr = os.ReadFile(LocalConfigPath)
if readErr == nil {
configPath = LocalConfigPath
}
}
if readErr != nil {
log.Log("No user config file found. Attempting to create default on disk.")
errMkdir := os.MkdirAll(path.Dir(SystemConfigPath), os.ModePerm) errMkdir := os.MkdirAll(path.Dir(SystemConfigPath), os.ModePerm)
errWrite := os.WriteFile(SystemConfigPath, DefaultConfigFile, 0600) errWrite := os.WriteFile(SystemConfigPath, DefaultConfigFile, 0600)
if errMkdir != nil || errWrite != nil { if errMkdir != nil || errWrite != nil {
log.Log(fmt.Sprintf("Warning: Could not write config to disk (%v).", errWrite)) log.Log(fmt.Sprintf("Warning: Could not write default config to %s (%v).", SystemConfigPath, errWrite))
log.Log("Falling back to embedded memory config.") log.Log("Using embedded default configuration only.")
} else { } else {
log.Log(fmt.Sprintf("Successfully created default config at %s", SystemConfigPath)) log.Log(fmt.Sprintf("Successfully created default config at %s.", SystemConfigPath))
} }
tomlFile = DefaultConfigFile log.Log("TOML configuration loaded successfully from Embedded Default")
err = nil return nil
} }
parseErr := toml.Unmarshal(tomlFile, &File) overrideErr := toml.Unmarshal(userTomlFile, &File)
if parseErr != nil { if overrideErr != nil {
log.Log(parseErr.Error()) log.Log(fmt.Sprintf("Error parsing user configuration from %s: %v", configPath, overrideErr))
return parseErr return overrideErr
} }
log.Log("TOML configuration loaded successfully") log.Log(fmt.Sprintf("TOML configuration loaded successfully. Defaults merged with %s", configPath))
return nil return nil
} }