Files
ddns-updater/internal/config.go

93 lines
2.7 KiB
Go
Raw Normal View History

2023-04-28 14:57:21 +02:00
package internal
import (
"fmt"
"log"
"os"
"strconv"
"time"
)
const (
defaultCheckInterval = 5 * 60
envKeyDnsToCheck = "DNS_NAMES"
2023-04-28 14:57:21 +02:00
envKeyPublicIpResolverTag = "PUBLIC_IP_RESOLVER"
envKeyCloudflareApiKey = "CLOUDFLARE_API_KEY"
envKeyCloudflareZone = "CLOUDFLARE_ZONE"
envKeyDirectadminUser = "DA_USER"
envKeyDirectadminKey = "DA_LOGIN_KEY"
envKeyDirectadminUrl = "DA_LOGIN_URL"
2023-04-28 14:57:21 +02:00
envKeyOnChangeComment = "ON_CHANGE_COMMENT"
envKeyCheckIntervalSeconds = "CHECK_INTERVAL_SECONDS"
2023-05-04 11:44:27 +02:00
envKeyNotifiers = "NOTIFIERS"
2023-04-28 14:57:21 +02:00
)
type Config struct {
DnsRecordsToCheck []string
PublicIpResolverTag string
DNSProviderTag string
DirectadminUsername string
DirectadminKey string
DirectadminUrl string
2023-04-28 14:57:21 +02:00
ApiToken string
WebhookToken string
2023-04-28 14:57:21 +02:00
CloudflareZone string
OnChangeComment string
2023-05-04 11:44:27 +02:00
Notifiers []string
2023-04-28 14:57:21 +02:00
CheckInterval time.Duration
}
func (c Config) Validate() error {
switch c.DNSProviderTag {
case "cloudflare":
if c.ApiToken == "" {
return fmt.Errorf("empty api token env key %s", envKeyCloudflareApiKey)
}
// if c.CloudflareZone == "" {
// return fmt.Errorf("empty zone in env key %s", envKeyCloudflareZone)
// }
case "directadmin":
if c.DirectadminUrl == "" {
return fmt.Errorf("empty DirectAdmin URL env key %s", envKeyDirectadminUrl)
}
if c.DirectadminUsername == "" {
return fmt.Errorf("empty Username in env key %s", envKeyDirectadminUser)
}
2023-04-28 14:57:21 +02:00
if c.DirectadminKey == "" {
return fmt.Errorf("empty Login Key in env key %s", envKeyDirectadminKey)
}
2023-04-28 14:57:21 +02:00
}
if len(c.DnsRecordsToCheck) == 0 {
return fmt.Errorf("no dns to check defined in env key %s", envKeyDnsToCheck)
}
return nil
}
func NewConfig() Config {
checkInterval, err := strconv.ParseInt(os.Getenv(envKeyCheckIntervalSeconds), 10, 64)
if err != nil {
log.Printf("wrong `%s` value. Check interval set default(%ds)", envKeyCheckIntervalSeconds, defaultCheckInterval)
checkInterval = defaultCheckInterval
}
return Config{
2023-05-04 11:44:27 +02:00
DnsRecordsToCheck: parseCommaDelimited(os.Getenv(envKeyDnsToCheck)),
2023-04-28 14:57:21 +02:00
PublicIpResolverTag: os.Getenv(envKeyPublicIpResolverTag),
ApiToken: os.Getenv(envKeyCloudflareApiKey),
DirectadminUsername: os.Getenv(envKeyDirectadminUser),
DirectadminKey: os.Getenv(envKeyDirectadminKey),
DirectadminUrl: os.Getenv(envKeyDirectadminUrl),
2023-04-28 14:57:21 +02:00
CloudflareZone: os.Getenv(envKeyCloudflareZone),
OnChangeComment: os.Getenv(envKeyOnChangeComment),
2023-05-04 11:44:27 +02:00
Notifiers: parseCommaDelimited(os.Getenv(envKeyNotifiers)),
2023-04-28 14:57:21 +02:00
CheckInterval: time.Duration(checkInterval) * time.Second,
WebhookToken: os.Getenv("WEBHOOK_TOKEN"),
2023-04-28 14:57:21 +02:00
}
}