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