Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

confighelp.lua 2.9KB

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