diff options
-rwxr-xr-x | awall-cli | 48 | ||||
-rw-r--r-- | awall/util.lua | 43 |
2 files changed, 69 insertions, 22 deletions
@@ -51,7 +51,9 @@ List optional policies: another policy which is in use. Dump variable and zone definitions: - awall dump + awall dump [level] + + Verbosity level is an integer in range 0-3 and defaults to 0. ]]) os.exit() @@ -121,22 +123,42 @@ end config = policyset:load() if mode == 'dump' then + level = 0 + (arg[opind] or 0) + require 'json' expconfig = config:expand() - for i, section in ipairs({'variable', 'zone'}) do - if config.data[section] then - print(string.upper(string.sub(section, 1, 1))..string.sub(section, 2, -1)..'s:') - lines = {} - for k, v in pairs(config.data[section]) do - def = json.encode(v) - exp = json.encode(expconfig[section][k]) - if exp ~= def then def = def..' = '..exp end - table.insert(lines, k..' = '..def..' ('..config.source[section][k]..')') + function capitalize(cls) + return string.upper(string.sub(cls, 1, 1))..string.sub(cls, 2, -1) + end + + for cls, objs in pairs(config.data) do + if level > 2 or (level == 2 and cls ~= 'service') or util.contains({'variable', + 'zone'}, + cls) then + if level == 0 then print(capitalize(cls)..'s:') end + + items = {} + for k, v in pairs(objs) do + exp = expconfig[cls][k] + expj = json.encode(exp) + src = config.source[cls][k] + if level == 0 then table.insert(items, {k, expj, src}) + else + table.insert(items, + {k, {{capitalize(cls)..' '..k, json.encode(v)}, + {'('..src..')', + util.compare(exp, v) and '' or '-> '..expj}}}) + end + end + table.sort(items, function(a, b) return a[1] < b[1] end) + + if level == 0 then util.printtabular(items) + else + util.printtabulars(util.map(items, + function(x) return x[2] end)) + print() end - table.sort(lines) - for i, line in ipairs(lines) do print(line) end - print() end end diff --git a/awall/util.lua b/awall/util.lua index 589ab77..55be6bd 100644 --- a/awall/util.lua +++ b/awall/util.lua @@ -38,19 +38,44 @@ function extend(tbl1, tbl2) for i, var in listpairs(tbl2) do table.insert(tbl1, var) end end -function printtabular(tbl) +function compare(a, b) + local t = type(a) + if t ~= type(b) then return false end + if t ~= 'table' then return a == b end + + local keys = {} + for k, v in pairs(a) do + if not compare(v, b[k]) then return false end + table.insert(keys, k) + end + for k, v in pairs(b) do + if not contains(keys, k) then return false end + end + return true +end + +function printtabulars(tables) local colwidth = {} - for i, row in ipairs(tbl) do - for j, col in ipairs(row) do - colwidth[j] = math.max(colwidth[j] or 0, string.len(col)) + for i, tbl in ipairs(tables) do + for j, row in ipairs(tbl) do + for k, col in ipairs(row) do + colwidth[k] = math.max(colwidth[k] or 0, string.len(col)) + end end end - for i, row in ipairs(tbl) do - for j, col in ipairs(row) do - if j > 1 then io.write(' ') end - io.write(col) - for k = 1,colwidth[j] - string.len(col) do io.write(' ') end + for i, tbl in ipairs(tables) do + for j, row in ipairs(tbl) do + for k = 1,#row do + if k > 1 then io.write(' ') end + io.write(row[k]) + if k < #row then + for l = 1,colwidth[k] - string.len(row[k]) do io.write(' ') end + end + end + io.write('\n') end io.write('\n') end end + +function printtabular(tbl) printtabulars({tbl}) end |