summaryrefslogtreecommitdiff
path: root/config.go
blob: eaf9d50a5bc0925d547af9e97845cd8f60ec9ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"git.rnrd.eu/ddnnss/provider"
	"git.rnrd.eu/ddnnss/source"
)

// a record target which we will try to update
// via the provider!
type Record struct {
	Zone     string              `json:"zone"`
	Name     string              `json:"name"`
	Type     provider.RecordType `json:"type"`
	Provider string              `json:"provider"`
	Source   string              `json:"source"`
}

type Config struct {
	Records   []Record                   `json:"records"`
	Providers map[string]provider.Config `json:"providers"`
	Sources   map[string]source.Config   `json:"sources"`
}

func LoadConfig(path string) (*Config, error) {
	raw, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	var cfg Config
	if err := json.Unmarshal(raw, &cfg); err != nil {
		return nil, fmt.Errorf("%s: %w", path, err)
	}

	for i, r := range cfg.Records {
		switch r.Type {
		case provider.TypeA, provider.TypeAAAA:
		default:
			return nil, fmt.Errorf("record %d: type must be A or AAAA", i)
		}
		if _, ok := cfg.Providers[r.Provider]; !ok {
			return nil, fmt.Errorf("record %d: unknown provider '%s'", i, r.Provider)
		}
		if _, ok := cfg.Sources[r.Source]; !ok {
			return nil, fmt.Errorf("record %d: unknown source '%s'", i, r.Source)
		}
	}
	return &cfg, nil
}