Software /
code /
prosody
Annotate
tools/cfgdump.lua @ 11444:b3a903032611
util.jsonschema: Fix "uniqueItems" prematurely declaring a match
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 09 Mar 2021 02:38:51 +0100 |
parent | 11192:11f285a439a4 |
child | 11569:08dab7df152b |
rev | line source |
---|---|
11192
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 #!/usr/bin/env lua |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- cfgdump.lua prosody.cfg.lua [[host] option] |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local s_format, print = string.format, print; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local printf = function(fmt, ...) return print(s_format(fmt, ...)); end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local serialization = require"util.serialization"; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local serialize = serialization.new and serialization.new({ unquoted = true }) or serialization.serialize; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local configmanager = require"core.configmanager"; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local startup = require "util.startup"; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 startup.set_function_metatable(); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local config_filename, onlyhost, onlyoption = ...; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local ok, _, err = configmanager.load(config_filename or "./prosody.cfg.lua", "lua"); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 assert(ok, err); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 if onlyhost then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 if not onlyoption then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 onlyhost, onlyoption = "*", onlyhost; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 if onlyhost ~= "*" then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local component_module = configmanager.get(onlyhost, "component_module"); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 if component_module == "component" then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 printf("Component %q", onlyhost); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 elseif component_module then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 printf("Component %q %q", onlyhost, component_module); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 else |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 printf("VirtualHost %q", onlyhost); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 printf("%s = %s", onlyoption or "?", serialize(configmanager.get(onlyhost, onlyoption))); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 return; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 local config = configmanager.getconfig(); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 for host, hostcfg in pairs(config) do |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 local fixed = {}; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 for option, value in pairs(hostcfg) do |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 fixed[option] = value; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 if option:match("ports?$") or option:match("interfaces?$") then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 if option:match("s$") then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 if type(value) ~= "table" then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 fixed[option] = { value }; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 else |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 if type(value) == "table" and #value > 1 then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 fixed[option] = nil; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 fixed[option.."s"] = value; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 config[host] = fixed; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 local globals = config["*"]; config["*"] = nil; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 local function printsection(section) |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 local out, n = {}, 1; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 for k,v in pairs(section) do |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 out[n], n = s_format("%s = %s", k, serialize(v)), n + 1; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 table.sort(out); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 print(table.concat(out, "\n")); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 print("-------------- Prosody Exported Configuration File -------------"); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 print(); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 print("------------------------ Global section ------------------------"); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 print(); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 printsection(globals); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 print(); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 local has_components = nil; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 print("------------------------ Virtual hosts -------------------------"); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 for host, hostcfg in pairs(config) do |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 setmetatable(hostcfg, nil); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 hostcfg.defined = nil; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 if hostcfg.component_module == nil then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 print(); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 printf("VirtualHost %q", host); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 printsection(hostcfg); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 else |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 has_components = true |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 print(); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 if has_components then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 print("------------------------- Components ---------------------------"); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 for host, hostcfg in pairs(config) do |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 local component_module = hostcfg.component_module; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 hostcfg.component_module = nil; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 if component_module then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 print(); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 if component_module == "component" then |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 printf("Component %q", host); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 else |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 printf("Component %q %q", host, component_module); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 hostcfg.component_module = nil; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 hostcfg.load_global_modules = nil; |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 printsection(hostcfg); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 end |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 print() |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 print("------------------------- End of File --------------------------"); |
11f285a439a4
tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 |