From 4462ca51ee10363916c29c071942c15f424913e6 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 18 May 2015 14:35:19 -0300 Subject: Add `luarocks config` command for querying LuaRocks settings. --- src/bin/luarocks | 1 + src/luarocks/cfg.lua | 30 +++++++++++++++-------- src/luarocks/config_cmd.lua | 58 +++++++++++++++++++++++++++++++++++++++++++++ src/luarocks/fetch.lua | 3 +-- src/luarocks/help.lua | 12 ++++------ src/luarocks/util.lua | 6 +++++ 6 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 src/luarocks/config_cmd.lua (limited to 'src') diff --git a/src/bin/luarocks b/src/bin/luarocks index d2f3c220..be6c2b81 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks @@ -27,6 +27,7 @@ commands = { purge = "luarocks.purge", doc = "luarocks.doc", upload = "luarocks.upload", + config = "luarocks.config_cmd", } command_line.run_command(...) diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index e864c9fd..7aaacc76 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua @@ -146,16 +146,17 @@ cfg.variables = {} cfg.rocks_trees = {} sys_config_file = site_config.LUAROCKS_SYSCONFIG or sys_config_dir.."/config-"..cfg.lua_version..".lua" -local err, errcode -sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, cfg) - -if (not sys_config_ok) and errcode == "open" then -- file not found, so try alternate file - sys_config_file = sys_config_dir.."/config.lua" +do + local err, errcode sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, cfg) -end -if (not sys_config_ok) and errcode ~= "open" then -- either "load" or "run"; bad config file, bail out with error - io.stderr:write(err.."\n") - os.exit(cfg.errorcodes.CONFIGFILE) + if (not sys_config_ok) and errcode == "open" then -- file not found, so try alternate file + sys_config_file = sys_config_dir.."/config.lua" + sys_config_ok, err, errcode = persist.load_into_table(sys_config_file, cfg) + end + if (not sys_config_ok) and errcode ~= "open" then -- either "load" or "run"; bad config file, bail out with error + io.stderr:write(err.."\n") + os.exit(cfg.errorcodes.CONFIGFILE) + end end local env_for_config_file = { @@ -590,7 +591,16 @@ function cfg.init_package_paths() end function cfg.which_config() - return sys_config_file, sys_config_ok, home_config_file, home_config_ok + return { + system = { + file = sys_config_file, + ok = sys_config_ok, + }, + user = { + file = home_config_file, + ok = home_config_ok, + } + } end cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch diff --git a/src/luarocks/config_cmd.lua b/src/luarocks/config_cmd.lua new file mode 100644 index 00000000..c066cfec --- /dev/null +++ b/src/luarocks/config_cmd.lua @@ -0,0 +1,58 @@ +--- Module implementing the LuaRocks "config" command. +-- Queries information about the LuaRocks configuration. +local config_cmd = {} + +local cfg = require("luarocks.cfg") +local util = require("luarocks.util") +local dir = require("luarocks.dir") + +local function config_file(conf) + print(dir.normalize(conf.file)) + if conf.ok then + return true + else + return nil, "file not found" + end +end + +--- Driver function for "config" command. +-- @return boolean: True if succeeded, nil on errors. +function config_cmd.run(...) + local flags = util.parse_flags(...) + + if flags["lua-incdir"] then + print(cfg.variables.LUA_INCDIR) + return true + end + if flags["lua-libdir"] then + print(cfg.variables.LUA_LIBDIR) + return true + end + if flags["lua-ver"] then + print(cfg.lua_version) + return true + end + local conf = cfg.which_config() + if flags["system-config"] then + return config_file(conf.system) + end + if flags["user-config"] then + return config_file(conf.user) + end + + if flags["rock-trees"] then + for _, tree in ipairs(cfg.rocks_trees) do + if type(tree) == "string" then + util.printout(dir.normalize(tree)) + else + local name = tree.name and "\t"..tree.name or "" + util.printout(dir.normalize(tree.root)..name) + end + end + return true + end + + return nil, "Please provide a flag for querying configuration values. "..util.see_help("config") +end + +return config_cmd diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index 497ae84f..83c76c9c 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua @@ -193,9 +193,8 @@ function fetch.load_local_rockspec(filename, quick) end local globals = err - local ok, err = true, nil if not quick then - ok, err = type_check.type_check_rockspec(rockspec, globals) + local ok, err = type_check.type_check_rockspec(rockspec, globals) if not ok then return nil, filename..": "..err end diff --git a/src/luarocks/help.lua b/src/luarocks/help.lua index e6c214ff..92458b2b 100644 --- a/src/luarocks/help.lua +++ b/src/luarocks/help.lua @@ -31,10 +31,8 @@ end local function get_status(status) if status then return "ok" - elseif status == false then - return "not found" else - return "failed" + return "not found" end end @@ -47,7 +45,7 @@ function help.run(...) local flags, command = util.parse_flags(...) if not command then - local sys_file, sys_ok, home_file, home_ok = cfg.which_config() + local conf = cfg.which_config() print_banner() print_section("NAME") util.printout("\t"..program..[[ - ]]..program_description) @@ -83,9 +81,9 @@ function help.run(...) print_section("CONFIGURATION") util.printout("\tLua version: " .. cfg.lua_version) util.printout("\tConfiguration files:") - util.printout("\t\tSystem: ".. dir.normalize(sys_file) .. " (" .. get_status(sys_ok) ..")") - if home_file then - util.printout("\t\tUser : ".. dir.normalize(home_file) .. " (" .. get_status(home_ok) ..")\n") + util.printout("\t\tSystem: ".. dir.normalize(conf.system.file) .. " (" .. get_status(conf.system.ok) ..")") + if conf.user.file then + util.printout("\t\tUser : ".. dir.normalize(conf.user.file) .. " (" .. get_status(conf.user.ok) ..")\n") else util.printout("\t\tUser : disabled in this LuaRocks installation.\n") end diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 682c99e9..6a99b568 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -97,6 +97,9 @@ local supported_flags = { ["lr-cpath"] = true, ["lr-path"] = true, ["lua-version"] = "", + ["lua-ver"] = true, + ["lua-incdir"] = true, + ["lua-libdir"] = true, ["modules"] = true, ["mversion"] = true, ["no-refresh"] = true, @@ -114,15 +117,18 @@ local supported_flags = { ["quick"] = true, ["rock-dir"] = true, ["rock-tree"] = true, + ["rock-trees"] = true, ["rockspec"] = true, ["server"] = "", ["skip-pack"] = true, ["source"] = true, ["summary"] = "\"\"", + ["system-config"] = true, ["tag"] = "", ["timeout"] = "", ["to"] = "", ["tree"] = "", + ["user-config"] = true, ["verbose"] = true, ["version"] = true, } -- cgit v1.2.3-55-g6feb