| 1 |
local opts |
| 2 |
local known_attrs = { |
| 3 |
data = 1, |
| 4 |
example = 1, |
| 5 |
type = 1, |
| 6 |
required = 1, |
| 7 |
default = 1, |
| 8 |
mixins = 1, |
| 9 |
} |
| 10 |
local argparse = require "argparse" |
| 11 |
local ansicolors = require "ansicolors" |
| 12 |
|
| 13 |
local parser = argparse() |
| 14 |
:name "rspamadm confighelp" |
| 15 |
:description "Shows help for the specified configuration options" |
| 16 |
:help_description_margin(32) |
| 17 |
parser:argument "path":args "*" |
| 18 |
:description('Optional config paths') |
| 19 |
parser:flag "--no-color" |
| 20 |
:description "Disable coloured output" |
| 21 |
parser:flag "--short" |
| 22 |
:description "Show only option names" |
| 23 |
parser:flag "--no-examples" |
| 24 |
:description "Do not show examples (implied by --short)" |
| 25 |
|
| 26 |
local function maybe_print_color(key) |
| 27 |
if not opts['no-color'] then |
| 28 |
return string.format('%s%s%s', ansicolors.white, key, ansicolors.reset) |
| 29 |
else |
| 30 |
return key |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
local function sort_values(tbl) |
| 35 |
local res = {} |
| 36 |
for k, v in pairs(tbl) do |
| 37 |
table.insert(res, { key = k, value = v }) |
| 38 |
end |
| 39 |
|
| 40 |
-- Sort order |
| 41 |
local order = { |
| 42 |
options = 1, |
| 43 |
dns = 2, |
| 44 |
upstream = 3, |
| 45 |
logging = 4, |
| 46 |
metric = 5, |
| 47 |
composite = 6, |
| 48 |
classifier = 7, |
| 49 |
modules = 8, |
| 50 |
lua = 9, |
| 51 |
worker = 10, |
| 52 |
workers = 11, |
| 53 |
} |
| 54 |
|
| 55 |
table.sort(res, function(a, b) |
| 56 |
local oa = order[a['key']] |
| 57 |
local ob = order[b['key']] |
| 58 |
|
| 59 |
if oa and ob then |
| 60 |
return oa < ob |
| 61 |
elseif oa then |
| 62 |
return -1 < 0 |
| 63 |
elseif ob then |
| 64 |
return 1 < 0 |
| 65 |
else |
| 66 |
return a['key'] < b['key'] |
| 67 |
end |
| 68 |
|
| 69 |
end) |
| 70 |
|
| 71 |
return res |
| 72 |
end |
| 73 |
|
| 74 |
local function print_help(key, value, tabs) |
| 75 |
print(string.format('%sConfiguration element: %s', tabs, maybe_print_color(key))) |
| 76 |
|
| 77 |
if not opts['short'] then |
| 78 |
if value['data'] then |
| 79 |
local data = value['data'] |
| 80 |
if type(data) == 'string' then |
| 81 |
local nv = string.match(data, '^#%s*(.*)%s*$') or data |
| 82 |
print(string.format('%s\tDescription: %s', tabs, nv)) |
| 83 |
elseif type(data) == 'table' and data.summary then |
| 84 |
print(string.format('%s\tDescription: %s', tabs, data.summary)) |
| 85 |
end |
| 86 |
end |
| 87 |
if type(value['type']) == 'string' then |
| 88 |
print(string.format('%s\tType: %s', tabs, value['type'])) |
| 89 |
end |
| 90 |
if type(value['required']) == 'boolean' then |
| 91 |
if value['required'] then |
| 92 |
print(string.format('%s\tRequired: %s', tabs, |
| 93 |
maybe_print_color(tostring(value['required'])))) |
| 94 |
else |
| 95 |
print(string.format('%s\tRequired: %s', tabs, |
| 96 |
tostring(value['required']))) |
| 97 |
end |
| 98 |
end |
| 99 |
if value['default'] then |
| 100 |
print(string.format('%s\tDefault: %s', tabs, value['default'])) |
| 101 |
end |
| 102 |
if value['mixins'] then |
| 103 |
local mixin_names = {} |
| 104 |
for _, mixin in ipairs(value['mixins']) do |
| 105 |
table.insert(mixin_names, mixin.name or mixin.schema_id or 'unknown') |
| 106 |
end |
| 107 |
print(string.format('%s\tMixins: %s', tabs, table.concat(mixin_names, ', '))) |
| 108 |
print(string.format('%s\t(Use `rspamadm confighelp <mixin>` for details)', tabs)) |
| 109 |
end |
| 110 |
if not opts['no-examples'] and value['example'] then |
| 111 |
local nv = string.match(value['example'], '^%s*(.*[^%s])%s*$') or value.example |
| 112 |
print(string.format('%s\tExample:\n%s', tabs, nv)) |
| 113 |
end |
| 114 |
if value.type and value.type == 'object' then |
| 115 |
print('') |
| 116 |
end |
| 117 |
end |
| 118 |
|
| 119 |
local sorted = sort_values(value) |
| 120 |
for _, v in ipairs(sorted) do |
| 121 |
if not known_attrs[v['key']] then |
| 122 |
-- We need to go deeper |
| 123 |
print_help(v['key'], v['value'], tabs .. '\t') |
| 124 |
end |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
return function(args, res) |
| 129 |
opts = parser:parse(args) |
| 130 |
|
| 131 |
local sorted = sort_values(res) |
| 132 |
|
| 133 |
for _, v in ipairs(sorted) do |
| 134 |
print_help(v['key'], v['value'], '') |
| 135 |
print('') |
| 136 |
end |
| 137 |
end |