aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Smedstad <carl.smedstad@protonmail.com>2021-04-05 16:03:59 +0200
committerHisham Muhammad <hisham@gobolinux.org>2021-04-06 17:24:57 -0300
commit2360071b5a723b02ec80251b569dd3c5e2f9feaa (patch)
treef6add433cd036650ba878ecab8164e485afdc0a8
parentcc0b526646600a94d09e48c4a2f5b3da092b831e (diff)
downloadluarocks-2360071b5a723b02ec80251b569dd3c5e2f9feaa.tar.gz
luarocks-2360071b5a723b02ec80251b569dd3c5e2f9feaa.tar.bz2
luarocks-2360071b5a723b02ec80251b569dd3c5e2f9feaa.zip
Support XDG_CONFIG_HOME
The loading of the config file has been changed to support the XDG Base Directory specification. More info here: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html If in a UNIX environment, LuaRocks will try loading a config file in XDG_CONFIG_HOME. If this fails, try loading one in the old location ~/.luarocks. This method clashed a bit with the old code that separated the setting of confdirs and the loading of the config files. As we now change cfg.homeconfdir depending on where we find the config file I found it necessary to remove the function set_confdirs() and move some pieces into the config loading logic.
-rw-r--r--src/luarocks/core/cfg.lua48
1 files changed, 27 insertions, 21 deletions
diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua
index 7ea86542..e24fa4f6 100644
--- a/src/luarocks/core/cfg.lua
+++ b/src/luarocks/core/cfg.lua
@@ -70,21 +70,6 @@ local function detect_sysconfdir()
70 return basedir 70 return basedir
71end 71end
72 72
73local function set_confdirs(cfg, platforms, hardcoded_sysconfdir)
74 local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded_sysconfdir
75 if platforms.windows and not platforms.msys2_mingw_w64 then
76 cfg.home = os.getenv("APPDATA") or "c:"
77 cfg.home_tree = cfg.home.."/luarocks"
78 cfg.homeconfdir = cfg.home_tree
79 cfg.sysconfdir = sysconfdir or ((os.getenv("PROGRAMFILES") or "c:") .. "/luarocks")
80 else
81 cfg.home = os.getenv("HOME") or ""
82 cfg.home_tree = cfg.home.."/.luarocks"
83 cfg.homeconfdir = cfg.home_tree
84 cfg.sysconfdir = sysconfdir or detect_sysconfdir() or "/etc/luarocks"
85 end
86end
87
88local load_config_file 73local load_config_file
89do 74do
90 -- Create global environment for the config files; 75 -- Create global environment for the config files;
@@ -653,17 +638,24 @@ function cfg.init(detected, warning)
653 local sys_config_file 638 local sys_config_file
654 local home_config_file 639 local home_config_file
655 local project_config_file 640 local project_config_file
641
642 local config_file_name = "config-"..cfg.lua_version..".lua"
643
656 do 644 do
657 set_confdirs(cfg, platforms, hardcoded.SYSCONFDIR) 645 local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded.SYSCONFDIR
658 local name = "config-"..cfg.lua_version..".lua" 646 if platforms.windows and not platforms.msys2_mingw_w64 then
659 sys_config_file = (cfg.sysconfdir .. "/" .. name):gsub("\\", "/") 647 cfg.home = os.getenv("APPDATA") or "c:"
660 home_config_file = (cfg.homeconfdir .. "/" .. name):gsub("\\", "/") 648 cfg.home_tree = cfg.home.."/luarocks"
661 if cfg.project_dir then 649 cfg.sysconfdir = sysconfdir or ((os.getenv("PROGRAMFILES") or "c:") .. "/luarocks")
662 project_config_file = cfg.project_dir .. "/.luarocks/" .. name 650 else
651 cfg.home = os.getenv("HOME") or ""
652 cfg.home_tree = cfg.home.."/.luarocks"
653 cfg.sysconfdir = sysconfdir or detect_sysconfdir() or "/etc/luarocks"
663 end 654 end
664 end 655 end
665 656
666 -- Load system configuration file 657 -- Load system configuration file
658 sys_config_file = (cfg.sysconfdir .. "/" .. config_file_name):gsub("\\", "/")
667 local sys_config_ok, err = load_config_file(cfg, platforms, sys_config_file) 659 local sys_config_ok, err = load_config_file(cfg, platforms, sys_config_file)
668 if err then 660 if err then
669 return nil, err, "config" 661 return nil, err, "config"
@@ -693,8 +685,21 @@ function cfg.init(detected, warning)
693 end 685 end
694 end 686 end
695 687
688 -- try XDG config home
689 if platforms.unix and not home_config_ok then
690 local xdg_config_home = os.getenv("XDG_CONFIG_HOME") or cfg.home .. "/.config"
691 cfg.homeconfdir = xdg_config_home .. "/luarocks"
692 home_config_file = (cfg.homeconfdir .. "/" .. config_file_name):gsub("\\", "/")
693 home_config_ok, err = load_config_file(cfg, platforms, home_config_file)
694 if err then
695 return nil, err, "config"
696 end
697 end
698
696 -- try the alternative defaults if there was no environment specified file or it didn't work 699 -- try the alternative defaults if there was no environment specified file or it didn't work
697 if not home_config_ok then 700 if not home_config_ok then
701 cfg.homeconfdir = cfg.home_tree
702 home_config_file = (cfg.homeconfdir .. "/" .. config_file_name):gsub("\\", "/")
698 home_config_ok, err = load_config_file(cfg, platforms, home_config_file) 703 home_config_ok, err = load_config_file(cfg, platforms, home_config_file)
699 if err then 704 if err then
700 return nil, err, "config" 705 return nil, err, "config"
@@ -703,6 +708,7 @@ function cfg.init(detected, warning)
703 708
704 -- finally, use the project-specific config file if any 709 -- finally, use the project-specific config file if any
705 if cfg.project_dir then 710 if cfg.project_dir then
711 project_config_file = cfg.project_dir .. "/.luarocks/" .. config_file_name
706 project_config_ok, err = load_config_file(cfg, platforms, project_config_file) 712 project_config_ok, err = load_config_file(cfg, platforms, project_config_file)
707 if err then 713 if err then
708 return nil, err, "config" 714 return nil, err, "config"