aboutsummaryrefslogtreecommitdiffstats
path: root/src/rspamadm/confighelp.lua
blob: 3af3c5b1fad3693cd5ff166b995e73bd0591f61c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
local util = require "rspamd_util"
local opts = {}
local known_attrs = {
  data = 1,
  example = 1,
  type = 1,
  required = 1,
  default = 1,
}

--.USE "getopt"
--.USE "ansicolors"


local function maybe_print_color(key)
  if opts['color'] then
    return ansicolors.white .. key .. ansicolors.reset
  else
    return key
  end
end

local function sort_values(tbl, opts)
  local res = {}
  for k, v in pairs(tbl) do
    table.insert(res, { key = k, value = v })
  end

  -- Sort order
  local order = {
    options = 1,
    dns = 2,
    upstream = 3,
    logging = 4,
    metric = 5,
    composite = 6,
    classifier = 7,
    modules = 8,
    lua = 9,
    worker = 10,
    workers = 11,
  }

  table.sort(res, function(a, b)
    local oa = order[a['key']]
    local ob = order[b['key']]

    if oa and ob then
      return oa < ob
    elseif oa then
      return -1 < 0
    elseif ob then
      return 1 < 0
    else
      return a['key'] < b['key']
    end

  end)

  return res
end

local function print_help(key, value, tabs)
  print(string.format('%sConfiguration element: %s', tabs, maybe_print_color(key)))

  if not opts['short'] then
    if value['data'] then
      print(string.format('%s\tDescription: %s', tabs, value['data']))
    end
    if not opts['no-examples'] and value['example'] then
      print(string.format('%s\tExample: %s', tabs, value['example']))
    end
    if value['type'] then
      print(string.format('%s\tType: %s', tabs, value['type']))
      if value['type'] == 'object' then
        print('')
      end
    end
    if type(value['required']) == 'boolean' then
      if value['required'] then
        print(string.format('%s\tRequired: %s', tabs,
          maybe_print_color(tostring(value['required']))))
      else
        print(string.format('%s\tRequired: %s', tabs,
          tostring(value['required'])))
      end
    end
    if value['default'] then
      print(string.format('%s\tDefault: %s', tabs, value['default']))
    end
  end

  local sorted = sort_values(value)
  for i, v in ipairs(sorted) do
    if not known_attrs[v['key']] then
      -- We need to go deeper
      print_help(v['key'], v['value'], tabs .. '\t')
    end
  end
end

return function(args, res)
  opts = getopt(args, '')

  local sorted = sort_values(res)

  for i,v in ipairs(sorted) do
    print_help(v['key'], v['value'], '')
    print('')
  end
end