summaryrefslogtreecommitdiff
path: root/src/utils/vyos-config-file-query
blob: a10c7e9b3cc915afa68748eb1756b19379605aea (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
#
# Copyright (C) 2019 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import re
import os
import sys
import json
import argparse

import vyos.configtree


arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-p', '--path', type=str,
    help="VyOS config node, e.g. \"system config-management commit-revisions\"", required=True)
arg_parser.add_argument('-f', '--file', type=str, help="VyOS config file, e.g. /config/config.boot", required=True)

arg_parser.add_argument('-s', '--separator', type=str, default=' ', help="Value separator for the plain format")
arg_parser.add_argument('-j', '--json', action='store_true')

op_group = arg_parser.add_mutually_exclusive_group(required=True)
op_group.add_argument('--return-value', action='store_true', help="Return a single node value")
op_group.add_argument('--return-values', action='store_true', help="Return all values of a multi-value node")
op_group.add_argument('--list-nodes', action='store_true', help="List children of a node")
op_group.add_argument('--exists', action='store_true', help="Check if a node exists")

args = arg_parser.parse_args()


try:
    with open(args.file, 'r') as f:
        config_file = f.read()
except OSError as e:
    print("Could not read the config file: {0}".format(e))
    sys.exit(1)

try:
    config = vyos.configtree.ConfigTree(config_file)
except Exception as e:
    print(e)
    sys.exit(1)


path = re.split(r'\s+', args.path)
values = None

if args.exists:
    if config.exists(path):
        sys.exit(0)
    else:
        sys.exit(1)
elif args.return_value:
    try:
        values = [config.return_value(path)]
    except vyos.configtree.ConfigTreeError as e:
        print(e)
        sys.exit(1)
elif args.return_values:
    try:
        values = config.return_values(path)
    except vyos.configtree.ConfigTreeError as e:
        print(e)
        sys.exit(1)
elif args.list_nodes:
    values = config.list_nodes(path)
    if not values:
        values = []
else:
    # Can't happen
    print("Operation required")
    sys.exit(1)


if values:
    if args.json:
        print(json.dumps(values))
    else:
        if len(values) == 1:
            print(values[0])
        else:
            # XXX: assuming values never contain quotes
            values = list(map(lambda s: "\'{0}\'".format(s), values))
            values_str = args.separator.join(values)
            print(values_str)

sys.exit(0)