From e4760c86b1a0a90e13017a1f3057216d87595ccf Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Wed, 25 Dec 2013 00:30:20 +0100 Subject: specify error exitcodes, specifically 'permission denied'. --- src/luarocks/build.lua | 2 +- src/luarocks/cfg.lua | 17 ++++++++++++++++- src/luarocks/command_line.lua | 11 ++++++----- src/luarocks/install.lua | 2 +- src/luarocks/make.lua | 2 +- src/luarocks/remove.lua | 2 +- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index 5555d94a..cee65781 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -364,7 +364,7 @@ function run(...) return pack.pack_binary_rock(name, version, do_build, name, version, deps.get_deps_mode(flags)) else local ok, err = fs.check_command_permissions(flags) - if not ok then return nil, err end + if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end ok, err = do_build(name, version, deps.get_deps_mode(flags)) if not ok then return nil, err end local name, version = ok, err diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index 2904146b..87777b86 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua @@ -35,6 +35,21 @@ major_version = program_version:match("([^.]%.[^.])") local persist = require("luarocks.persist") +_M.errorcodes = setmetatable({ + OK = 0, + UNSPECIFIED = 1, + PERMISSIONDENIED = 2, +},{ + __index = function(t, key) + local val = rawget(t, key) + if not val then + error("'"..tostring(key).."' is not a valid errorcode", 2) + end + return val + end +}) + + local popen_ok, popen_result = pcall(io.popen, "") if popen_ok then if popen_result then @@ -43,7 +58,7 @@ if popen_ok then else io.stderr:write("Your version of Lua does not support io.popen,\n") io.stderr:write("which is required by LuaRocks. Please check your Lua installation.\n") - os.exit(1) + os.exit(_M.errorcodes.UNSPECIFIED) end -- System detection: diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index b98084e0..e79b1442 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua @@ -12,7 +12,8 @@ local program = util.this_program("luarocks") --- Display an error message and exit. -- @param message string: The error message. -local function die(message) +-- @param exitcode number: the exitcode to use +local function die(message, exitcode) assert(type(message) == "string") local ok, err = pcall(util.run_scheduled_functions) @@ -20,7 +21,7 @@ local function die(message) util.printerr("\nLuaRocks "..cfg.program_version.." internal bug (please report at luarocks-developers@lists.sourceforge.net):\n"..err) end util.printerr("\nError: "..message) - os.exit(1) + os.exit(exitcode or cfg.errorcodes.UNSPECIFIED) end local function replace_tree(flags, args, tree) @@ -83,7 +84,7 @@ function run_command(...) util.printout(program.." "..cfg.program_version) util.printout(program_description) util.printout() - os.exit(0) + os.exit(cfg.errorcodes.OK) elseif flags["help"] or #nonflags == 0 then command = "help" args = nonflags @@ -184,13 +185,13 @@ function run_command(...) -- I'm not changing this now to avoid messing with the run() -- interface, which I know some people use (even though -- I never published it as a public API...) - local xp, ok, err = xpcall(function() return commands[command].run(unpack(args)) end, function(err) + local xp, ok, err, exitcode = xpcall(function() return commands[command].run(unpack(args)) end, function(err) die(debug.traceback("LuaRocks "..cfg.program_version .." bug (please report at luarocks-developers@lists.sourceforge.net).\n" ..err, 2)) end) if xp and (not ok) then - die(err) + die(err, exitcode) end else die("Unknown command: "..command) diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua index c181d612..041d8ca2 100644 --- a/src/luarocks/install.lua +++ b/src/luarocks/install.lua @@ -129,7 +129,7 @@ function run(...) end local ok, err = fs.check_command_permissions(flags) - if not ok then return nil, err end + if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end if name:match("%.rockspec$") or name:match("%.src%.rock$") then util.printout("Using "..name.."... switching to 'build' mode") diff --git a/src/luarocks/make.lua b/src/luarocks/make.lua index eef49d0c..aff4f93c 100644 --- a/src/luarocks/make.lua +++ b/src/luarocks/make.lua @@ -73,7 +73,7 @@ function run(...) return pack.pack_binary_rock(rspec.name, rspec.version, build.build_rockspec, rockspec, false, true, deps.get_deps_mode(flags)) else local ok, err = fs.check_command_permissions(flags) - if not ok then return nil, err end + if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end ok, err = build.build_rockspec(rockspec, false, true, deps.get_deps_mode(flags)) if not ok then return nil, err end local name, version = ok, err diff --git a/src/luarocks/remove.lua b/src/luarocks/remove.lua index 5a6e4dc5..9ab6d11f 100644 --- a/src/luarocks/remove.lua +++ b/src/luarocks/remove.lua @@ -144,7 +144,7 @@ function run(...) local deps_mode = flags["deps-mode"] or cfg.deps_mode local ok, err = fs.check_command_permissions(flags) - if not ok then return nil, err end + if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end local rock_type = name:match("%.(rock)$") or name:match("%.(rockspec)$") local filename = name -- cgit v1.2.3-55-g6feb