Software /
code /
prosody
Annotate
core/configmanager.lua @ 5043:2856e1cfbe95
Merge with Zash
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 28 Jul 2012 20:59:03 +0100 |
parent | 5021:85b2689dbcfe |
child | 5124:a4a74a0e9b9c |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1504
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2862
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2862
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
466
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
466
diff
changeset
|
7 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
466
diff
changeset
|
8 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local _G = _G; |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
10 local setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table = |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
11 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table; |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
12 local format, math_max = string.format, math.max; |
2861
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
13 |
3384
b7600dd7cd42
configmanager: Remove dependency on eventmanager, and global 'prosody' object
Matthew Wild <mwild1@gmail.com>
parents:
3012
diff
changeset
|
14 local fire_event = prosody and prosody.events.fire_event or function () end; |
1000
a73715a9267f
core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents:
913
diff
changeset
|
15 |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4530
diff
changeset
|
16 local envload = require"util.envload".envload; |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
17 local lfs = require "lfs"; |
3609
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
18 local path_sep = package.config:sub(1,1); |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
19 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 module "configmanager" |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local parsers = {}; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
24 local config_mt = { __index = function (t, k) return rawget(t, "*"); end}; |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
25 local config = setmetatable({ ["*"] = { core = {} } }, config_mt); |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 -- When host not found, use global |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
28 local host_mt = { }; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 -- When key not found in section, check key in global's section |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 function section_mt(section_name) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 return { __index = function (t, k) |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
33 local section = rawget(config["*"], section_name); |
2974
f37dc6f038f3
configmanager: Fix some wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
34 if not section then return nil; end |
f37dc6f038f3
configmanager: Fix some wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
35 return section[k]; |
f37dc6f038f3
configmanager: Fix some wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
36 end |
f37dc6f038f3
configmanager: Fix some wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
37 }; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
40 function getconfig() |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
41 return config; |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
42 end |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
43 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 function get(host, section, key) |
4530
40905e7bf680
configmanager: get(): Make section (core) optional (hurrah)
Matthew Wild <mwild1@gmail.com>
parents:
4358
diff
changeset
|
45 if not key then |
40905e7bf680
configmanager: get(): Make section (core) optional (hurrah)
Matthew Wild <mwild1@gmail.com>
parents:
4358
diff
changeset
|
46 section, key = "core", section; |
40905e7bf680
configmanager: get(): Make section (core) optional (hurrah)
Matthew Wild <mwild1@gmail.com>
parents:
4358
diff
changeset
|
47 end |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local sec = config[host][section]; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 if sec then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 return sec[key]; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 return nil; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 end |
4001
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
54 function _M.rawget(host, section, key) |
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
55 local hostconfig = rawget(config, host); |
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
56 if hostconfig then |
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
57 local sectionconfig = rawget(hostconfig, section); |
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
58 if sectionconfig then |
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
59 return rawget(sectionconfig, key); |
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
60 end |
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
61 end |
2e8411f6cb14
configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents:
3929
diff
changeset
|
62 end |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
64 local function set(config, host, section, key, value) |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 if host and section and key then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 local hostconfig = rawget(config, host); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 if not hostconfig then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host]; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 if not rawget(hostconfig, section) then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 hostconfig[section] = setmetatable({}, section_mt(section)); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 hostconfig[section][key] = value; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 return true; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 return false; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
79 function _M.set(host, section, key, value) |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
80 return set(config, host, section, key, value); |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
81 end |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
82 |
3609
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
83 -- Helper function to resolve relative paths (needed by config) |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
84 do |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
85 local rel_path_start = ".."..path_sep; |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
86 function resolve_relative_path(parent_path, path) |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
87 if path then |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
88 -- Some normalization |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
89 parent_path = parent_path:gsub("%"..path_sep.."+$", ""); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
90 path = path:gsub("^%.%"..path_sep.."+", ""); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
91 |
3609
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
92 local is_relative; |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
93 if path_sep == "/" and path:sub(1,1) ~= "/" then |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
94 is_relative = true; |
4358
86be454168fb
configmanager: resolve_relative_path: Improved detection of absolute paths on Windows.
Waqas Hussain <waqas20@gmail.com>
parents:
4001
diff
changeset
|
95 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" or path:sub(2,3) ~= ":/")) then |
3609
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
96 is_relative = true; |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
97 end |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
98 if is_relative then |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
99 return parent_path..path_sep..path; |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
100 end |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
101 end |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
102 return path; |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
103 end |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
104 end |
954b1159f2f3
prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents:
3573
diff
changeset
|
105 |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
106 -- Helper function to convert a glob to a Lua pattern |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
107 local function glob_to_pattern(glob) |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
108 return "^"..glob:gsub("[%p*?]", function (c) |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
109 if c == "*" then |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
110 return ".*"; |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
111 elseif c == "?" then |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
112 return "."; |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
113 else |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
114 return "%"..c; |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
115 end |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
116 end).."$"; |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
117 end |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
118 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 function load(filename, format) |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
120 format = format or filename:match("%w+$"); |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
121 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 if parsers[format] and parsers[format].load then |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
123 local f, err = io.open(filename); |
2552
8dda55217e83
configmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2427
diff
changeset
|
124 if f then |
3613
f617718d2221
configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents:
3610
diff
changeset
|
125 local new_config = setmetatable({ ["*"] = { core = {} } }, config_mt); |
f617718d2221
configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents:
3610
diff
changeset
|
126 local ok, err = parsers[format].load(f:read("*a"), filename, new_config); |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 f:close(); |
3613
f617718d2221
configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents:
3610
diff
changeset
|
128 if ok then |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
129 config = new_config; |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
130 fire_event("config-reloaded", { |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
131 filename = filename, |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
132 format = format, |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
133 config = config |
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
134 }); |
1000
a73715a9267f
core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents:
913
diff
changeset
|
135 end |
3780
791aede977da
configmanager: Switch back to returning 'ok' to signal config load success - fixes config errors not being displayed
Matthew Wild <mwild1@gmail.com>
parents:
3613
diff
changeset
|
136 return ok, "parser", err; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 end |
793
55add3b87c01
Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
138 return f, "file", err; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 end |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
140 |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
141 if not format then |
793
55add3b87c01
Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
142 return nil, "file", "no parser specified"; |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
143 else |
793
55add3b87c01
Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
144 return nil, "file", "no parser for "..(format); |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
145 end |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 function save(filename, format) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 function addparser(format, parser) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 if format and parser then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 parsers[format] = parser; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 |
2427
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
157 -- _M needed to avoid name clash with local 'parsers' |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
158 function _M.parsers() |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
159 local p = {}; |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
160 for format in pairs(parsers) do |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
161 table.insert(p, format); |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
162 end |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
163 return p; |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
164 end |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
165 |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
166 -- Built-in Lua parser |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 do |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4530
diff
changeset
|
168 local pcall, setmetatable = _G.pcall, _G.setmetatable; |
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4530
diff
changeset
|
169 local rawget, tostring = _G.rawget, _G.tostring; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 parsers.lua = {}; |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
171 function parsers.lua.load(data, config_file, config) |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
172 local env; |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
173 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below |
2975
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
174 env = setmetatable({ |
3012 | 175 Host = true, host = true, VirtualHost = true, |
176 Component = true, component = true, | |
3610
2084959d4096
configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents:
3609
diff
changeset
|
177 Include = true, include = true, RunScript = true }, { |
2975
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
178 __index = function (t, k) |
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
179 return rawget(_G, k) or |
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
180 function (settings_table) |
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
181 config[__currenthost or "*"][k] = settings_table; |
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
182 end; |
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
183 end, |
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
184 __newindex = function (t, k, v) |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
185 set(config, env.__currenthost or "*", "core", k, v); |
2975
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
186 end |
c1a2e210f47e
configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents:
2974
diff
changeset
|
187 }); |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 |
1615
0e3eacf135f2
configmanager: Default options appearing before Host "*" to global (fixes potential traceback)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
189 rawset(env, "__currenthost", "*") -- Default is global |
3011
1189a29cd846
configmanager: Add VirtualHost as an alias for Host (re-applied in trunk due to previous bad merge with 0.7)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
190 function env.VirtualHost(name) |
2861
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
191 if rawget(config, name) and rawget(config[name].core, "component_module") then |
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
192 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s", |
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
193 name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0); |
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
194 end |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
195 rawset(env, "__currenthost", name); |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
196 -- Needs at least one setting to logically exist :) |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
197 set(config, name or "*", "core", "defined", true); |
3515
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
198 return function (config_options) |
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
199 rawset(env, "__currenthost", "*"); -- Return to global scope |
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
200 for option_name, option_value in pairs(config_options) do |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
201 set(config, name or "*", "core", option_name, option_value); |
3515
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
202 end |
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
203 end; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 end |
3011
1189a29cd846
configmanager: Add VirtualHost as an alias for Host (re-applied in trunk due to previous bad merge with 0.7)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
205 env.Host, env.host = env.VirtualHost, env.VirtualHost; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
207 function env.Component(name) |
2861
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
208 if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "component_module") then |
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
209 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s", |
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
210 name, name, name), 0); |
1402615b66f8
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
211 end |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
212 set(config, name, "core", "component_module", "component"); |
913
3e2dac84017d
core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
213 -- Don't load the global modules by default |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
214 set(config, name, "core", "load_global_modules", false); |
913
3e2dac84017d
core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
215 rawset(env, "__currenthost", name); |
3515
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
216 local function handle_config_options(config_options) |
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
217 rawset(env, "__currenthost", "*"); -- Return to global scope |
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
218 for option_name, option_value in pairs(config_options) do |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
219 set(config, name or "*", "core", option_name, option_value); |
3515
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
220 end |
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
221 end |
913
3e2dac84017d
core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
222 |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
223 return function (module) |
857
49298263f241
core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents:
795
diff
changeset
|
224 if type(module) == "string" then |
3573
f31fa6520a4b
configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents:
3515
diff
changeset
|
225 set(config, name, "core", "component_module", module); |
3515
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
226 return handle_config_options; |
857
49298263f241
core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents:
795
diff
changeset
|
227 end |
3515
bb494c3aa364
configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents:
3384
diff
changeset
|
228 return handle_config_options(module); |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
229 end |
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
230 end |
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
231 env.component = env.Component; |
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
232 |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
233 function env.Include(file, wildcard) |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
234 if file:match("[*?]") then |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
235 local path_pos, glob = file:match("()([^"..path_sep.."]+)$"); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
236 local path = file:sub(1, math_max(path_pos-2,0)); |
3929
7cb03d67101b
configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents:
3905
diff
changeset
|
237 local config_path = config_file:gsub("[^"..path_sep.."]+$", ""); |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
238 if #path > 0 then |
3929
7cb03d67101b
configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents:
3905
diff
changeset
|
239 path = resolve_relative_path(config_path, path); |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
240 else |
3929
7cb03d67101b
configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents:
3905
diff
changeset
|
241 path = config_path; |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
242 end |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
243 local patt = glob_to_pattern(glob); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
244 for f in lfs.dir(path) do |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
245 if f:sub(1,1) ~= "." and f:match(patt) then |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
246 env.Include(path..path_sep..f); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
247 end |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
248 end |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
249 else |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
250 local f, err = io.open(file); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
251 if f then |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
252 local data = f:read("*a"); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
253 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
254 local ret, err = parsers.lua.load(data, file, config); |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
255 if not ret then error(err:gsub("%[string.-%]", file), 0); end |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
256 end |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
257 if not f then error("Error loading included "..file..": "..err, 0); end |
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
258 return f, err; |
794
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
259 end |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
260 end |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
261 env.include = env.Include; |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
262 |
3610
2084959d4096
configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents:
3609
diff
changeset
|
263 function env.RunScript(file) |
3905
9222dad9e1e8
configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents:
3780
diff
changeset
|
264 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file)); |
3610
2084959d4096
configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents:
3609
diff
changeset
|
265 end |
2084959d4096
configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents:
3609
diff
changeset
|
266 |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4530
diff
changeset
|
267 local chunk, err = envload(data, "@"..config_file, env); |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 if not chunk then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 return nil, err; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 local ok, err = pcall(chunk); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 if not ok then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 return nil, err; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 |
3613
f617718d2221
configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents:
3610
diff
changeset
|
279 return true; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
284 return _M; |