diff options
author | Kim Alvefur <zash@zash.se> | 2020-10-28 22:48:31 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2020-10-28 22:48:31 +0100 |
commit | 12fc380f8da7b9b57eb806533925bb868a9be01d (patch) | |
tree | 676a40ce669dc935ef8914756c74c65df032e2a5 /tools | |
parent | 1e8f53864feaf660df3b64ff510d644636ef532a (diff) | |
download | prosody-12fc380f8da7b9b57eb806533925bb868a9be01d.tar.gz prosody-12fc380f8da7b9b57eb806533925bb868a9be01d.zip |
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Useful for comparing what you think you have in your config with what
Prosody sees, e.g. wrt (lack of) significance of indentation, order of
options vs scope etc. (global options do not go at the end!)
Could probably be turned into a prosodyctl command, especially if it
learns to redact secrets and passwords.
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/cfgdump.lua | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tools/cfgdump.lua b/tools/cfgdump.lua new file mode 100755 index 00000000..b7ca0716 --- /dev/null +++ b/tools/cfgdump.lua @@ -0,0 +1,120 @@ +#!/usr/bin/env lua + +-- cfgdump.lua prosody.cfg.lua [[host] option] + +local s_format, print = string.format, print; +local printf = function(fmt, ...) return print(s_format(fmt, ...)); end +local serialization = require"util.serialization"; +local serialize = serialization.new and serialization.new({ unquoted = true }) or serialization.serialize; +local configmanager = require"core.configmanager"; +local startup = require "util.startup"; + +startup.set_function_metatable(); +local config_filename, onlyhost, onlyoption = ...; + +local ok, _, err = configmanager.load(config_filename or "./prosody.cfg.lua", "lua"); +assert(ok, err); + +if onlyhost then + if not onlyoption then + onlyhost, onlyoption = "*", onlyhost; + end + if onlyhost ~= "*" then + local component_module = configmanager.get(onlyhost, "component_module"); + + if component_module == "component" then + printf("Component %q", onlyhost); + elseif component_module then + printf("Component %q %q", onlyhost, component_module); + else + printf("VirtualHost %q", onlyhost); + end + end + printf("%s = %s", onlyoption or "?", serialize(configmanager.get(onlyhost, onlyoption))); + return; +end + +local config = configmanager.getconfig(); + + +for host, hostcfg in pairs(config) do + local fixed = {}; + for option, value in pairs(hostcfg) do + fixed[option] = value; + if option:match("ports?$") or option:match("interfaces?$") then + if option:match("s$") then + if type(value) ~= "table" then + fixed[option] = { value }; + end + else + if type(value) == "table" and #value > 1 then + fixed[option] = nil; + fixed[option.."s"] = value; + end + end + end + end + config[host] = fixed; +end + +local globals = config["*"]; config["*"] = nil; + +local function printsection(section) + local out, n = {}, 1; + for k,v in pairs(section) do + out[n], n = s_format("%s = %s", k, serialize(v)), n + 1; + end + table.sort(out); + print(table.concat(out, "\n")); +end + +print("-------------- Prosody Exported Configuration File -------------"); +print(); +print("------------------------ Global section ------------------------"); +print(); +printsection(globals); +print(); + +local has_components = nil; + +print("------------------------ Virtual hosts -------------------------"); + +for host, hostcfg in pairs(config) do + setmetatable(hostcfg, nil); + hostcfg.defined = nil; + + if hostcfg.component_module == nil then + print(); + printf("VirtualHost %q", host); + printsection(hostcfg); + else + has_components = true + end +end + +print(); + +if has_components then +print("------------------------- Components ---------------------------"); + + for host, hostcfg in pairs(config) do + local component_module = hostcfg.component_module; + hostcfg.component_module = nil; + + if component_module then + print(); + if component_module == "component" then + printf("Component %q", host); + else + printf("Component %q %q", host, component_module); + hostcfg.component_module = nil; + hostcfg.load_global_modules = nil; + end + printsection(hostcfg); + end + end +end + +print() +print("------------------------- End of File --------------------------"); + |