summaryrefslogtreecommitdiffstats
path: root/debian/get_user
blob: 1e5b6565aeada51f5afb863f64fe4e8e6462fcc7 (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
#!/usr/bin/python3

import yaml, sys

conf_file = '/etc/knot/knot.conf' if len(sys.argv) < 2 else sys.argv[1]
ip_fields = ['listen', 'address', 'via', 'whitelist', 'network']

try:
    conf = yaml.load(open(conf_file, 'r'), Loader=yaml.FullLoader)
except (yaml.scanner.ScannerError, yaml.parser.ParserError):
    conf = False

if not conf:
    import io
    conf_io = io.StringIO()
    with open(conf_file) as f:
        for line in f:
            if line.split(':')[0].strip() not in ip_fields:
                conf_io.write(line)
    conf_io.seek(0)
    try:
        conf = yaml.load(conf_io, Loader=yaml.FullLoader)
    except (yaml.scanner.ScannerError, yaml.parser.ParserError):
        sys.exit(1)

if "server" in conf and conf["server"]:
    if "user" in conf["server"] and conf["server"]["user"]:
        print(conf["server"]["user"].split(":")[0].split(".")[0])