* luci/libs: add uvl cli utility
[project/luci.git] / libs / uvl / root / usr / bin / uvl
1 --[[
2
3 UCI Validation Layer - Command Line Utility
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id: uvl.lua 2873 2008-08-17 21:43:56Z jow $
14
15 ]]--
16
17 require("luci.uvl")
18 require("luci.util")
19
20
21 function getopt( arg, options )
22         options = options or ""
23         local tab = {}
24         local args = {}
25         for k, v in ipairs(arg) do
26                 if v:sub(1, 2) == "--" then
27                         local x = v:find( "=", 1, true )
28                         if x then
29                                 tab[ v:sub( 3, x-1 ) ] = v:sub( x+1 )
30                         else
31                             tab[ v:sub( 3 ) ] = true
32                         end
33                 elseif v:sub( 1, 1 ) == "-" then
34                         local y = 2
35                         local l = #v
36                         local jopt
37                         while ( y <= l ) do
38                                 jopt = v:sub( y, y )
39                                 if options:find( jopt, 1, true ) then
40                                         if y < l then
41                                                 tab[ jopt ] = v:sub( y+1 )
42                                                 y = l
43                                         else
44                                                 tab[ jopt ] = arg[ k + 1 ]
45                                                 arg[ k + 1 ] = ""
46                                         end
47                                 else
48                                         tab[ jopt ] = true
49                                 end
50                                 y = y + 1
51                         end
52             elseif #v > 0 then
53                 table.insert(args, v)
54             end
55         end
56         return tab, args
57 end
58
59 local options, arguments = getopt( arg )
60
61 if #arguments == 0 or options.help then
62         print([=[
63 uvl - UCI Validation Layer
64 $Id: uvl.lua 2873 2008-08-17 21:43:56Z jow $
65 (c) 2008 Jo-Philipp Wich, Steven Barth
66
67 Usage:
68         uvl --help
69         uvl [--silent] [--no-strict-sections] [--no-strict-options]
70                 [--no-strict-validators] config[.section[.option]]
71
72 Options:
73         --help
74                 Display this help message.
75
76         --silent
77                 Don't produce any output.
78
79         --no-strict-sections
80                 Don't treat sections found in config but not in scheme as error.
81
82         --no-strict-options
83                 Don't treat options found in config but not in scheme as error.
84
85         --no-strict-validators
86                 Don't invalidate config if an external validators fails.
87
88         ]=])
89         os.exit(255)
90 else
91         luci.uvl.STRICT_UNKNOWN_SECTIONS =
92                 ( options['no-strict-sections'] and false or true )
93         luci.uvl.STRICT_UNKNOWN_OPTIONS =
94                 ( options['no-strict-options'] and false or true )
95         luci.uvl.STRICT_EXTERNAL_VALIDATORS =
96                 ( options['no-strict-validators'] and false or true )
97
98         local uvl = luci.uvl.UVL( options['schemedir'] )
99         local cso = luci.util.split( arguments[1], "." )
100         local ok, err = uvl:validate( unpack(cso) )
101
102         if ok then
103                 if not options.silent then
104                         print( string.format(
105                                 '%s "%s.%s.%s" validates fine!',
106                                         ( #cso == 1 and "Config" or
107                                                 ( #cso == 2 and "Section" or "Option" ) ),
108                                         cso[1], cso[2], cso[3]
109                         ) )
110                 end
111                 os.exit( 0 )
112         else
113                 if not options.silent then print( err ) end
114                 os.exit( 1 )
115         end
116 end