summaryrefslogtreecommitdiff
path: root/src/helpers/commands-pipe.py
blob: 3ffc64545a802c531901175e675136b2bc3c8d03 (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
#!/usr/bin/python3

import sys
import re

from signal import signal, SIGPIPE, SIG_DFL
from vyos.configtree import ConfigTree

signal(SIGPIPE,SIG_DFL)


config_string = sys.stdin.read().strip()

if not config_string:
    sys.exit(0)

# When used in conf mode pipe, the config given to the script is likely incomplete
# and breaks the "all top level nodes are neither tag nor leaf"
# invariant, so we wrap it into a fake node.
# Since nodes don't normally start with an underscore,
# __root__ is hygienic enough.
config_string = "__root__ {{ {0} \n }}".format(config_string)

config_re = re.compile("(set|comment)\s+__root__\s+(.*)")

config = ConfigTree(config_string)
commands = config.to_commands()
commands = config_re.sub("\\1 \\2", commands)

print(commands)