alrm/config/validate.go

64 lines
1.2 KiB
Go

package config
import (
"fmt"
"os"
)
var ERR_DUP_API error = fmt.Errorf(
"api key and api key file cannot both be defined",
)
var ERR_NO_API error = fmt.Errorf(
"no api key or api key file defined",
)
var ERR_NO_GROUPHOST error = fmt.Errorf(
"no groups or hosts configured",
)
var ERR_MISSING_KEYFILE error = fmt.Errorf(
"api key file not found",
)
func (cfg *Config) Validate() error {
if cfg.APIKey != "" && cfg.APIKeyFile != "" {
return ERR_DUP_API
}
if cfg.APIKey == "" && cfg.APIKeyFile == "" {
return ERR_NO_API
}
if cfg.APIKeyFile != "" {
if _, err := os.Stat(cfg.APIKeyFile); err != nil {
return ERR_MISSING_KEYFILE
}
}
if len(cfg.Groups) == 0 && len(cfg.Hosts) == 0 {
return ERR_NO_GROUPHOST
}
set := make(map[string]bool)
for _, host := range cfg.Hosts {
name := host.NameOrAddress()
if _, exists := set[name]; exists {
return fmt.Errorf("duplicate host \"%s\"", name)
}
set[name] = true
}
for group, hosts := range cfg.Groups {
set = make(map[string]bool)
for _, host := range hosts {
name := host.NameOrAddress()
if _, exists := set[name]; exists {
return fmt.Errorf(
"duplicate host \"%s\" in group \"%s\"", group, name,
)
}
set[name] = true
}
}
return nil
}