-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
58 lines (47 loc) · 1.4 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/json"
"flag"
"os"
)
type Config struct {
DNSConfigs map[string]interface{}
CacheExpiration int64
UseOutbound bool
LogLevel string
}
func InitConfig() (string, bool, string, string, error) {
logLevel := flag.String("log-level", "info", "log level")
useOutbound := flag.Bool("use-outbound", false, "use outbound address")
configDir := flag.String("config-dir", "/root/aweful-dns-confs", "directory containing config files")
dnsmasqConfig := flag.String("dnsmasq-file", "/etc/dnsmasq.d/aweful-dns.conf", "dnsmasq config file to write (this will be overwritten)")
flag.Parse()
err := clearDnsmasqConfig(*dnsmasqConfig)
if err != nil {
return "", false, "", "", err
}
return *logLevel, *useOutbound, *configDir, *dnsmasqConfig, nil
}
func clearDnsmasqConfig(filePath string) error {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString("#Aweful dnsmasq config file\n#Warning: this file will be overwritten\n")
if err != nil {
return err
}
return nil
}
func ParseFile(filePath string) (map[string]interface{}, error) {
fileContents := make(map[string]interface{})
body, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &fileContents); err != nil {
return nil, err
}
return fileContents, nil
}