* luci/libs: fix typo in uvl utility
[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: uvl.lua 2873 2008-08-17 21:43:56Z jow $
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: uvl.lua 2873 2008-08-17 21:43:56Z jow $
67 (c) 2008 Jo-Philipp Wich, Steven Barth
68
69 Usage:
70         uvl --help
71         uvl [--silent] [--no-strict-sections] [--no-strict-options]
72                 [--no-strict-validators] config[.section[.option]]
73
74 Options:
75         --help
76                 Display this help message.
77
78         --silent
79                 Don't produce any output.
80
81         --no-strict-sections
82                 Don't treat sections found in config but not in scheme as error.
83
84         --no-strict-options
85                 Don't treat options found in config but not in scheme as error.
86
87         --no-strict-validators
88                 Don't invalidate config if an external validator fails.
89         ]=])
90         os.exit(255)
91 else
92         luci.uvl.STRICT_UNKNOWN_SECTIONS =
93                 ( options['no-strict-sections'] and false or true )
94         luci.uvl.STRICT_UNKNOWN_OPTIONS =
95                 ( options['no-strict-options'] and false or true )
96         luci.uvl.STRICT_EXTERNAL_VALIDATORS =
97                 ( options['no-strict-validators'] and false or true )
98
99         local uvl = luci.uvl.UVL( options['schemedir'] )
100         local cso = luci.util.split( arguments[1], "." )
101         local ok, err = uvl:validate( unpack(cso) )
102
103         if ok then
104                 if not options.silent then
105                         print( string.format(
106                                 '%s "%s.%s.%s" validates fine!',
107                                         ( #cso == 1 and "Config" or
108                                                 ( #cso == 2 and "Section" or "Option" ) ),
109                                         cso[1], cso[2], cso[3]
110                         ) )
111                 end
112                 os.exit( 0 )
113         else
114                 if not options.silent then print( err ) end
115                 os.exit( 1 )
116         end
117 end