Annotate

tools/cfgdump.lua @ 11569:08dab7df152b

tools/cfgdump: Iterate in sort order to give stable output Should allow using this tool for comparing configs without hash table order messing things up.
author Kim Alvefur <zash@zash.se>
date Mon, 17 May 2021 16:33:44 +0200
parent 11192:11f285a439a4
child 11570:c3896c714a83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
11569
08dab7df152b tools/cfgdump: Iterate in sort order to give stable output
Kim Alvefur <zash@zash.se>
parents: 11192
diff changeset
7 local it = require "util.iterators";
11192
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 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
9 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
10 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
11 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
12
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 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
14 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
15
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 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
17 assert(ok, err);
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 if onlyhost then
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 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
21 onlyhost, onlyoption = "*", onlyhost;
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 if onlyhost ~= "*" then
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 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
25
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 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
27 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
28 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
29 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
30 else
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 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
32 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 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
35 return;
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 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
39
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
11569
08dab7df152b tools/cfgdump: Iterate in sort order to give stable output
Kim Alvefur <zash@zash.se>
parents: 11192
diff changeset
41 for host, hostcfg in it.sorted_pairs(config) do
11192
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local fixed = {};
11569
08dab7df152b tools/cfgdump: Iterate in sort order to give stable output
Kim Alvefur <zash@zash.se>
parents: 11192
diff changeset
43 for option, value in it.sorted_pairs(hostcfg) do
11192
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 fixed[option] = value;
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("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
46 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
47 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
48 fixed[option] = { value };
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 else
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 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
52 fixed[option] = nil;
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
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 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 config[host] = fixed;
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 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
62
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 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
64 local out, n = {}, 1;
11569
08dab7df152b tools/cfgdump: Iterate in sort order to give stable output
Kim Alvefur <zash@zash.se>
parents: 11192
diff changeset
65 for k,v in it.sorted_pairs(section) do
11192
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 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
67 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 table.sort(out);
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 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
70 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 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
73 print();
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 print("------------------------ Global section ------------------------");
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 print();
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 printsection(globals);
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 print();
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 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
80
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 print("------------------------ Virtual hosts -------------------------");
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
11569
08dab7df152b tools/cfgdump: Iterate in sort order to give stable output
Kim Alvefur <zash@zash.se>
parents: 11192
diff changeset
83 for host, hostcfg in it.sorted_pairs(config) do
11192
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 setmetatable(hostcfg, nil);
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 hostcfg.defined = nil;
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 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
88 print();
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 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
90 printsection(hostcfg);
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 else
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 has_components = true
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 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 print();
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 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
99 print("------------------------- Components ---------------------------");
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100
11569
08dab7df152b tools/cfgdump: Iterate in sort order to give stable output
Kim Alvefur <zash@zash.se>
parents: 11192
diff changeset
101 for host, hostcfg in it.sorted_pairs(config) do
11192
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 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
103 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
104
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 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
106 print();
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 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
108 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
109 else
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 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
111 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
112 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
113 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 printsection(hostcfg);
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 end
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 print()
11f285a439a4 tools/cfgdump: Reads Prosody config file and pretty-prints it back out
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 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
121