]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Add sorting for configuration helper
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 Dec 2015 17:48:59 +0000 (17:48 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 Dec 2015 17:48:59 +0000 (17:48 +0000)
src/rspamadm/confighelp.lua

index 28c0e289774918083b6a45a36af3ffefa67560ad..223de294a8a18677293ea93505b3f7ae3d0788d6 100644 (file)
@@ -18,6 +18,46 @@ local function maybe_print_color(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)))
 
@@ -34,10 +74,11 @@ local function print_help(key, value, tabs)
     end
   end
 
-  for k, v in pairs(value) do
-    if not known_attrs[k] then
+  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(k, v, tabs .. '\t')
+      print_help(v['key'], v['value'], tabs .. '\t')
     end
   end
 end
@@ -45,8 +86,10 @@ end
 return function(args, res)
   opts = getopt(args, '')
 
-  for k,v in pairs(res) do
-    print_help(k, v, '')
+  local sorted = sort_values(res)
+
+  for i,v in ipairs(sorted) do
+    print_help(v['key'], v['value'], '')
     print('')
   end
 end