2024-07-24 17:36:01 +02:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AllDomainsApiEndpoint = "/api/v1/get/domain/all"
|
|
|
|
AllAliasesApiEndpoint = "/api/v1/get/alias/all"
|
|
|
|
AliasAddApiEndpoint = "/api/v1/add/alias"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ConfigFile string
|
|
|
|
DryRun bool
|
|
|
|
Quiet bool
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ApiEndpoint string `yaml:"api_endpoint"`
|
|
|
|
ApiKey string `yaml:"api_key"`
|
|
|
|
AdminEmail string `yaml:"admin_email"`
|
|
|
|
MailPrefixes []string `yaml:"mail_prefixes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) LoadFromDisk() error {
|
|
|
|
if ConfigFile == "" {
|
|
|
|
slog.Debug("no config file found, looking in working dir")
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get current working directory: %w", err)
|
|
|
|
}
|
|
|
|
ConfigFile = wd + "/config.yaml"
|
|
|
|
}
|
|
|
|
slog.Debug("looking for config file", "path", ConfigFile)
|
|
|
|
|
|
|
|
data, err := os.ReadFile(ConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read config file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(data, &c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal config file: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:54:50 +02:00
|
|
|
c.checkConfig()
|
|
|
|
|
2024-07-24 17:36:01 +02:00
|
|
|
slog.Debug("successfully read config file", "path", ConfigFile)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2024-07-26 10:54:50 +02:00
|
|
|
|
|
|
|
func (c Config) checkConfig() {
|
|
|
|
failed := false
|
|
|
|
if c.ApiEndpoint == "" {
|
|
|
|
slog.Error("[CONFIG] api_endpoint must not be empty")
|
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ApiKey == "" {
|
|
|
|
slog.Error("[CONFIG] api_key must not be empty")
|
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AdminEmail == "" {
|
|
|
|
slog.Error("[CONFIG] admin_email must not be empty")
|
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.MailPrefixes) < 1 {
|
|
|
|
slog.Error("[CONFIG] mail_prefixes must have at least one entry")
|
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if failed {
|
|
|
|
slog.Error("[CONFIG] Application can't start until above errors are fixed.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|