ytdl_hook.lua: make exec return a single value

This refactors exec to only return the result of the subprocess command,
and makes the rest of run_ytdl_hook use the fields of this result,
because specifying all those return values multiple times is unwieldy
now that exec is called several times, and this is easier to read
anyway.

I removed the line
err = string.format("%s returned '%d'", err, es)
altogether instead of updating the es variable, because there was no
chance of it being executed since it would only happen when
result.killed_by_us is true, but run_ytdl_hook returns early when it is.
This commit is contained in:
Guido Cella 2021-10-15 23:04:54 +02:00 committed by Dudemanguy
parent 0772d0263c
commit 42fd6f5f6f
1 changed files with 15 additions and 15 deletions

View File

@ -96,11 +96,12 @@ end
local function exec(args) local function exec(args)
msg.debug("Running: " .. table.concat(args, " ")) msg.debug("Running: " .. table.concat(args, " "))
local ret = mp.command_native({name = "subprocess", return mp.command_native({
args = args, name = "subprocess",
capture_stdout = true, args = args,
capture_stderr = true}) capture_stdout = true,
return ret.status, ret.stdout, ret, ret.killed_by_us capture_stderr = true,
})
end end
-- return true if it was explicitly set on the command line -- return true if it was explicitly set on the command line
@ -805,9 +806,9 @@ function run_ytdl_hook(url)
table.insert(command, "--") table.insert(command, "--")
table.insert(command, url) table.insert(command, url)
local es, json, result, aborted local result
if ytdl.searched then if ytdl.searched then
es, json, result, aborted = exec(command) result = exec(command)
else else
local separator = platform_is_windows() and ";" or ":" local separator = platform_is_windows() and ";" or ":"
if o.ytdl_path:match("[^" .. separator .. "]") then if o.ytdl_path:match("[^" .. separator .. "]") then
@ -825,12 +826,12 @@ function run_ytdl_hook(url)
msg.verbose("Found youtube-dl at: " .. ytdl_cmd) msg.verbose("Found youtube-dl at: " .. ytdl_cmd)
ytdl.path = ytdl_cmd ytdl.path = ytdl_cmd
command[1] = ytdl.path command[1] = ytdl.path
es, json, result, aborted = exec(command) result = exec(command)
break break
else else
msg.verbose("No youtube-dl found with path " .. path .. exesuf .. " in config directories") msg.verbose("No youtube-dl found with path " .. path .. exesuf .. " in config directories")
command[1] = path command[1] = path
es, json, result, aborted = exec(command) result = exec(command)
if result.error_string == "init" then if result.error_string == "init" then
msg.verbose("youtube-dl with path " .. path .. exesuf .. " not found in PATH or not enough permissions") msg.verbose("youtube-dl with path " .. path .. exesuf .. " not found in PATH or not enough permissions")
else else
@ -844,20 +845,21 @@ function run_ytdl_hook(url)
ytdl.searched = true ytdl.searched = true
end end
if aborted then if result.killed_by_us then
return return
end end
local json = result.stdout
local parse_err = nil local parse_err = nil
if (es ~= 0) or (json == "") then if result.status ~= 0 or json == "" then
json = nil json = nil
elseif json then elseif json then
json, parse_err = utils.parse_json(json) json, parse_err = utils.parse_json(json)
end end
if (json == nil) then if (json == nil) then
msg.verbose("status:", es) msg.verbose("status:", result.status)
msg.verbose("reason:", result.error_string) msg.verbose("reason:", result.error_string)
msg.verbose("stdout:", result.stdout) msg.verbose("stdout:", result.stdout)
msg.verbose("stderr:", result.stderr) msg.verbose("stderr:", result.stderr)
@ -870,10 +872,8 @@ function run_ytdl_hook(url)
err = err .. "not found or not enough permissions" err = err .. "not found or not enough permissions"
elseif parse_err then elseif parse_err then
err = err .. "failed to parse JSON data: " .. parse_err err = err .. "failed to parse JSON data: " .. parse_err
elseif not result.killed_by_us then
err = err .. "unexpected error occurred"
else else
err = string.format("%s returned '%d'", err, es) err = err .. "unexpected error occurred"
end end
msg.error(err) msg.error(err)
if parse_err or string.find(ytdl_err, "yt%-dl%.org/bug") then if parse_err or string.find(ytdl_err, "yt%-dl%.org/bug") then