]> source.dussan.org Git - rspamd.git/commitdiff
Add lua pretty formatter for confighelp
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 Dec 2015 12:53:28 +0000 (12:53 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 Dec 2015 12:53:28 +0000 (12:53 +0000)
src/rspamadm/CMakeLists.txt
src/rspamadm/confighelp.c
src/rspamadm/confighelp.lua [new file with mode: 0644]

index 258ed454b84dae7c7a4bed8f0246f1b31e7961e4..3f8a3d0c3d4350da0c5afe6b4f4db04bc37d166a 100644 (file)
@@ -15,7 +15,9 @@ SET(RSPAMADMSRC rspamadm.c
         ${CMAKE_SOURCE_DIR}/src/smtp_proxy.c
         ${CMAKE_SOURCE_DIR}/src/worker.c
         ${CMAKE_SOURCE_DIR}/src/http_proxy.c)
-SET(RSPAMADMLUASRC ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_stat.lua)
+SET(RSPAMADMLUASRC
+        ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_stat.lua
+        ${CMAKE_CURRENT_SOURCE_DIR}/confighelp.lua)
 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
 ADD_CUSTOM_TARGET(rspamadm_lua_preprocess
         ${PERL_EXECUTABLE}
index 462d6ff5034745104d5c5ca4ded4982dd6118047..870676e9fc54e760cf148c2f6aa8fe4cdbfe45e3 100644 (file)
@@ -29,6 +29,7 @@
 #include "cfg_rcl.h"
 #include "rspamd.h"
 #include "lua/lua_common.h"
+#include "confighelp.lua.h"
 
 static gboolean json = FALSE;
 static gboolean compact = FALSE;
@@ -81,7 +82,8 @@ rspamadm_confighelp_help (gboolean full_help)
 }
 
 static void
-rspamadm_confighelp_show (const char *key, const ucl_object_t *obj)
+rspamadm_confighelp_show (struct rspamd_config *cfg, gint argc, gchar **argv,
+               const char *key, const ucl_object_t *obj)
 {
        rspamd_fstring_t *out;
 
@@ -103,7 +105,14 @@ rspamadm_confighelp_show (const char *key, const ucl_object_t *obj)
                        rspamd_fprintf (stdout, "Showing help for all options:\n");
                }
 
-               rspamd_ucl_emit_fstring (obj, UCL_EMIT_CONFIG, &out);
+               rspamadm_execute_lua_ucl_subr (cfg->lua_state,
+                               argc,
+                               argv,
+                               obj,
+                               rspamadm_script_confighelp);
+
+               rspamd_fstring_free (out);
+               return;
        }
 
        rspamd_fprintf (stdout, "%V", out);
@@ -245,7 +254,7 @@ rspamadm_confighelp (gint argc, gchar **argv)
                                }
 
                                if (doc_obj != NULL) {
-                                       rspamadm_confighelp_show (argv[i], doc_obj);
+                                       rspamadm_confighelp_show (cfg, argc, argv, argv[i], doc_obj);
 
                                        if (keyword) {
                                                ucl_object_unref ((ucl_object_t *)doc_obj);
@@ -265,7 +274,7 @@ rspamadm_confighelp (gint argc, gchar **argv)
        }
        else {
                /* Show all documentation strings */
-               rspamadm_confighelp_show (NULL, cfg->doc_strings);
+               rspamadm_confighelp_show (cfg, argc, argv, NULL, cfg->doc_strings);
        }
 
        rspamd_config_free (cfg);
diff --git a/src/rspamadm/confighelp.lua b/src/rspamadm/confighelp.lua
new file mode 100644 (file)
index 0000000..34f323f
--- /dev/null
@@ -0,0 +1,42 @@
+local util = require "rspamd_util"
+local opts = {}
+local known_attrs = {
+  data = 1,
+  example = 1,
+  type = 1
+}
+
+--.USE "getopt"
+
+local function print_help(key, value, tabs)
+  print(string.format('%sOption: %s', tabs, key))
+
+  if not opts['short'] then
+    if value['data'] then
+      print(string.format('%s\tDescription: %s', tabs, value['data']))
+    end
+    if value['type'] then
+      print(string.format('%s\tType: %s', tabs, value['type']))
+    end
+
+    if not opts['no-examples'] and value['example'] then
+      print(string.format('%s\tExample: %s', tabs, value['example']))
+    end
+  end
+  print('')
+
+  for k, v in pairs(value) do
+    if not known_attrs[k] then
+      -- We need to go deeper
+      print_help(k, v, tabs .. '\t')
+    end
+  end
+end
+
+return function(args, res)
+  opts = getopt(args, '')
+
+  for k,v in pairs(res) do
+    print_help(k, v, '');
+  end
+end