This commit is contained in:
parent
5205c2fb22
commit
2117fcc783
@ -14,46 +14,56 @@ import (
|
||||
var DefaultConfigFile []byte
|
||||
var File Config
|
||||
|
||||
// WARNING: this code is ai generated
|
||||
func Read() error {
|
||||
const SystemConfigPath = "/etc/wazuh-notify/wazuh-notify-config.toml"
|
||||
|
||||
execPath, _ := os.Executable()
|
||||
LocalConfigPath := path.Join(path.Dir(execPath), "wazuh-notify-config.toml")
|
||||
|
||||
var tomlFile []byte
|
||||
var err error
|
||||
|
||||
tomlFile, err = os.ReadFile(SystemConfigPath)
|
||||
|
||||
err := toml.Unmarshal(DefaultConfigFile, &File)
|
||||
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 {
|
||||
log.Log("Config not found. Attempting to create default.")
|
||||
var userTomlFile []byte
|
||||
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)
|
||||
|
||||
errWrite := os.WriteFile(SystemConfigPath, DefaultConfigFile, 0600)
|
||||
|
||||
if errMkdir != nil || errWrite != nil {
|
||||
log.Log(fmt.Sprintf("Warning: Could not write config to disk (%v).", errWrite))
|
||||
log.Log("Falling back to embedded memory config.")
|
||||
log.Log(fmt.Sprintf("Warning: Could not write default config to %s (%v).", SystemConfigPath, errWrite))
|
||||
log.Log("Using embedded default configuration only.")
|
||||
} 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
|
||||
err = nil
|
||||
log.Log("TOML configuration loaded successfully from Embedded Default")
|
||||
return nil
|
||||
}
|
||||
|
||||
parseErr := toml.Unmarshal(tomlFile, &File)
|
||||
if parseErr != nil {
|
||||
log.Log(parseErr.Error())
|
||||
return parseErr
|
||||
overrideErr := toml.Unmarshal(userTomlFile, &File)
|
||||
if overrideErr != nil {
|
||||
log.Log(fmt.Sprintf("Error parsing user configuration from %s: %v", configPath, overrideErr))
|
||||
return overrideErr
|
||||
}
|
||||
|
||||
log.Log("TOML configuration loaded successfully")
|
||||
log.Log(fmt.Sprintf("TOML configuration loaded successfully. Defaults merged with %s", configPath))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user