Rapid spam filtering system https://rspamd.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.5 KiB

  1. local opts = {}
  2. local known_attrs = {
  3. data = 1,
  4. example = 1,
  5. type = 1,
  6. required = 1,
  7. default = 1,
  8. }
  9. local getopt = require "rspamadm/getopt"
  10. local ansicolors = require "rspamadm/ansicolors"
  11. local function maybe_print_color(key)
  12. if not opts['no-color'] then
  13. return ansicolors.white .. key .. ansicolors.reset
  14. else
  15. return key
  16. end
  17. end
  18. local function sort_values(tbl)
  19. local res = {}
  20. for k, v in pairs(tbl) do
  21. table.insert(res, { key = k, value = v })
  22. end
  23. -- Sort order
  24. local order = {
  25. options = 1,
  26. dns = 2,
  27. upstream = 3,
  28. logging = 4,
  29. metric = 5,
  30. composite = 6,
  31. classifier = 7,
  32. modules = 8,
  33. lua = 9,
  34. worker = 10,
  35. workers = 11,
  36. }
  37. table.sort(res, function(a, b)
  38. local oa = order[a['key']]
  39. local ob = order[b['key']]
  40. if oa and ob then
  41. return oa < ob
  42. elseif oa then
  43. return -1 < 0
  44. elseif ob then
  45. return 1 < 0
  46. else
  47. return a['key'] < b['key']
  48. end
  49. end)
  50. return res
  51. end
  52. local function print_help(key, value, tabs)
  53. print(string.format('%sConfiguration element: %s', tabs, maybe_print_color(key)))
  54. if not opts['short'] then
  55. if value['data'] then
  56. local nv = string.match(value['data'], '^#%s*(.*)%s*$') or value.data
  57. print(string.format('%s\tDescription: %s', tabs, nv))
  58. end
  59. if value['type'] then
  60. print(string.format('%s\tType: %s', tabs, value['type']))
  61. end
  62. if type(value['required']) == 'boolean' then
  63. if value['required'] then
  64. print(string.format('%s\tRequired: %s', tabs,
  65. maybe_print_color(tostring(value['required']))))
  66. else
  67. print(string.format('%s\tRequired: %s', tabs,
  68. tostring(value['required'])))
  69. end
  70. end
  71. if value['default'] then
  72. print(string.format('%s\tDefault: %s', tabs, value['default']))
  73. end
  74. if not opts['no-examples'] and value['example'] then
  75. local nv = string.match(value['example'], '^%s*(.*[^%s])%s*$') or value.example
  76. print(string.format('%s\tExample:\n%s', tabs, nv))
  77. end
  78. if value.type and value.type == 'object' then
  79. print('')
  80. end
  81. end
  82. local sorted = sort_values(value)
  83. for _, v in ipairs(sorted) do
  84. if not known_attrs[v['key']] then
  85. -- We need to go deeper
  86. print_help(v['key'], v['value'], tabs .. '\t')
  87. end
  88. end
  89. end
  90. return function(args, res)
  91. opts = getopt.getopt(args, '')
  92. local sorted = sort_values(res)
  93. for _,v in ipairs(sorted) do
  94. print_help(v['key'], v['value'], '')
  95. print('')
  96. end
  97. end