aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/luarocks/cfg.lua84
1 files changed, 45 insertions, 39 deletions
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua
index ff18e0a3..6ff2fcf0 100644
--- a/src/luarocks/cfg.lua
+++ b/src/luarocks/cfg.lua
@@ -124,8 +124,8 @@ else
124end 124end
125 125
126-- Path configuration: 126-- Path configuration:
127
128local sys_config_file, home_config_file 127local sys_config_file, home_config_file
128local sys_config_file_default, home_config_file_default
129local sys_config_dir, home_config_dir 129local sys_config_dir, home_config_dir
130local sys_config_ok, home_config_ok = false, false 130local sys_config_ok, home_config_ok = false, false
131local extra_luarocks_module_dir 131local extra_luarocks_module_dir
@@ -173,49 +173,55 @@ local merge_overrides = function(overrides)
173 util.deep_merge(cfg, overrides) 173 util.deep_merge(cfg, overrides)
174end 174end
175 175
176sys_config_file = site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..cfg.lua_version..".lua" 176-- load config file from a list until first succesful one. Info is
177do 177-- added to `cfg` module table, returns filepath of succesfully loaded
178 local err, errcode 178-- file or nil if it failed
179 sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, env_for_config_file()) 179local load_config_file = function(list)
180 if (not sys_config_ok) and errcode == "open" then -- file not found, so try alternate file 180 for _, filepath in ipairs(list) do
181 sys_config_file = sys_config_dir.."/config.lua" 181 local result, err, errcode = persist.load_into_table(filepath, env_for_config_file())
182 sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, env_for_config_file()) 182 if (not result) and errcode ~= "open" then
183 end 183 -- errcode is either "load" or "run"; bad config file, so error out
184 if (not sys_config_ok) and errcode ~= "open" then -- either "load" or "run"; bad config file, bail out with error 184 io.stderr:write(err.."\n")
185 io.stderr:write(err.."\n") 185 os.exit(cfg.errorcodes.CONFIGFILE)
186 os.exit(cfg.errorcodes.CONFIGFILE) 186 end
187 end 187 if result then
188 if sys_config_ok then 188 -- succes in loading and running, merge contents and exit
189 merge_overrides(sys_config_ok) 189 merge_overrides(result)
190 return filepath
191 end
190 end 192 end
193 return nil -- nothing was loaded
194end
195
196
197-- Load system configuration file
198do
199 sys_config_file_default = sys_config_dir.."/config.lua"
200 sys_config_file = load_config_file({
201 site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..cfg.lua_version..".lua",
202 sys_config_file_default,
203 })
204 sys_config_ok = (sys_config_file ~= nil)
191end 205end
192 206
207-- Load user configuration file (if allowed)
193if not site_config.LUAROCKS_FORCE_CONFIG then 208if not site_config.LUAROCKS_FORCE_CONFIG then
209
210 home_config_file_default = home_config_dir.."/config.lua"
211 local list = {
212 os.getenv("LUAROCKS_CONFIG_" .. version_suffix) or os.getenv("LUAROCKS_CONFIG"),
213 home_config_dir.."/config-"..cfg.lua_version..".lua",
214 home_config_file_default,
215 }
216 -- first entry might be a silent nil, check and remove if so
217 if not list[1] then table.remove(list, 1) end
218
219 home_config_file = load_config_file(list)
220 home_config_ok = (home_config_file ~= nil)
194 221
195 local home_overrides, err, errcode
196 home_config_file = os.getenv("LUAROCKS_CONFIG_" .. version_suffix) or os.getenv("LUAROCKS_CONFIG")
197 if home_config_file then
198 home_overrides, err, errcode = persist.load_into_table(home_config_file, env_for_config_file())
199 else
200 home_config_file = home_config_dir.."/config-"..cfg.lua_version..".lua"
201 home_overrides, err, errcode = persist.load_into_table(home_config_file, env_for_config_file())
202 if (not home_overrides) and (not errcode == "run") then
203 home_config_file = home_config_dir.."/config.lua"
204 home_overrides, err, errcode = persist.load_into_table(home_config_file, env_for_config_file())
205 end
206 end
207 if home_overrides then
208 home_config_ok = true
209 merge_overrides(home_overrides)
210 else
211 home_config_ok = home_overrides
212 if errcode ~= "open" then
213 io.stderr:write(err.."\n")
214 os.exit(cfg.errorcodes.CONFIGFILE)
215 end
216 end
217end 222end
218 223
224
219if not next(cfg.rocks_trees) then 225if not next(cfg.rocks_trees) then
220 if cfg.home_tree then 226 if cfg.home_tree then
221 table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } ) 227 table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } )
@@ -610,11 +616,11 @@ end
610function cfg.which_config() 616function cfg.which_config()
611 return { 617 return {
612 system = { 618 system = {
613 file = sys_config_file, 619 file = sys_config_file or sys_config_file_default,
614 ok = sys_config_ok, 620 ok = sys_config_ok,
615 }, 621 },
616 user = { 622 user = {
617 file = home_config_file, 623 file = home_config_file or home_config_file_default,
618 ok = home_config_ok, 624 ok = home_config_ok,
619 } 625 }
620 } 626 }