-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
44 lines (36 loc) · 835 Bytes
/
config.go
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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
)
type Entry struct {
A string
AAAA string
}
type Config map[string]Entry
func (c Config) Lookup(domain string) (Entry, bool) {
entry, ok := c[domain]
if !ok {
// Check if we have a wildcard match
for configDomain, entry := range c {
if strings.HasPrefix(configDomain, "*.") && strings.HasSuffix(domain, configDomain[2:]) {
return entry, true
}
}
}
return entry, ok
}
func ReadConfig(fileName string) (Config, error) {
file, err := os.OpenFile(fileName, os.O_RDONLY, 0)
if err != nil {
return Config{}, fmt.Errorf("failed to read config: %w", err)
}
defer file.Close()
var cfg Config
if err := json.NewDecoder(file).Decode(&cfg); err != nil {
return Config{}, fmt.Errorf("failed to decode config: %w", err)
}
return cfg, nil
}