feat: add mode selecting (#15)
All checks were successful
CD / test (push) Successful in 44s
CD / Build and push (push) Successful in 3m5s

Co-authored-by: Timo Behrendt <t.behrendt@t00n.de>
Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
This commit was merged in pull request #15.
This commit is contained in:
2024-12-23 14:17:46 +01:00
committed by t.behrendt
parent e84a409d82
commit ac786f533d
14 changed files with 150 additions and 112 deletions

View File

@@ -1,17 +1,15 @@
package config
import (
"errors"
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type DomainConfig struct {
TLD string `yaml:"tld"`
Subdomains []string `yaml:"subdomains"`
}
type Config struct {
Mode string `yaml:"mode"`
ExternalIPProvider ExternalIpProviderConfig `yaml:"ip_provider"`
DNSProvider DNSProviderConfig `yaml:"dns_provider"`
NotificationProvider NotificationProviderConfig `yaml:"notification_provider,omitempty"`
@@ -19,6 +17,16 @@ type Config struct {
CheckInterval string `yaml:"check_interval"`
}
const (
RunOnceMode = "RunOnce"
ScheduledMode = "Scheduled"
)
type DomainConfig struct {
TLD string `yaml:"tld"`
Subdomains []string `yaml:"subdomains"`
}
type ExternalIpProviderConfig struct {
Type string `yaml:"type"`
ProviderConfig yaml.Node `yaml:"config"`
@@ -35,15 +43,30 @@ type NotificationProviderConfig struct {
}
func (c *Config) Load(filePath string) error {
err := yaml.Unmarshal([]byte(filePath), c)
inputConfig, err := os.ReadFile(filePath)
if err != nil {
inputConfig, err := os.ReadFile(filePath)
if err != nil {
return err
}
return yaml.Unmarshal(inputConfig, c)
return fmt.Errorf("failed to read config file: %w", err)
}
return err
if err := yaml.Unmarshal(inputConfig, c); err != nil {
return fmt.Errorf("failed to unmarshal config file: %w", err)
}
if err := c.validate(); err != nil {
return fmt.Errorf("failed to validate config: %w", err)
}
return nil;
}
func (c *Config) validate() error {
if c.Mode != RunOnceMode && c.Mode != ScheduledMode {
return errors.New("mode must be one of 'RunOnce' or 'Scheduled'")
}
if c.Mode == ScheduledMode && c.CheckInterval == "" {
return errors.New("check interval must be set when mode is 'Scheduled'")
}
return nil;
}