From 2f654a1eeb0ea567867149951f9f72ad96070372 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sun, 20 May 2012 23:29:07 -0300 Subject: Refactor the download operation so it can be used by other modules. --- src/luarocks/download.lua | 72 +++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/src/luarocks/download.lua b/src/luarocks/download.lua index 268dc113..474e4d39 100644 --- a/src/luarocks/download.lua +++ b/src/luarocks/download.lua @@ -18,52 +18,28 @@ help = [[ --arch= Download rock for a specific architecture. ]] -local function download(rock_file) - local rock = fetch.fetch_url(rock_file) - return rock ~= nil -end - ---- Driver function for the "download" command. --- @param name string: a rock name. --- @param version string or nil: if the name of a package is given, a --- version may also be passed. --- @return boolean or (nil, string): true if successful or nil followed --- by an error message. -function run(...) - local flags, name, version = util.parse_flags(...) - - assert(type(version) == "string" or not version) - if type(name) ~= "string" and not flags["all"] then - return nil, "Argument missing, see help." - end - if not name then name, version = "", "" end - - local query = search.make_query(name, version) - if flags["source"] then - query.arch = "src" - elseif flags["rockspec"] then - query.arch = "rockspec" - elseif flags["arch"] then - query.arch = flags["arch"] - end +function download(arch, name, version, all) local results, err - if flags["all"] then + local query = search.make_query(name, version) + if arch then query.arch = arch end + if all then if name == "" then query.exact_name = false end results, err = search.search_repos(query) else results, err = search.find_suitable_rock(query) end if type(results) == "string" then - return download(results) + local file = fetch.fetch_url(results) + return file elseif type(results) == "table" and next(results) then - if flags["all"] then + if all then local all_ok = true local any_err = "" for name, result in pairs(results) do for version, versions in pairs(result) do for _,items in pairs(versions) do local filename = path.make_url(items.repo, name, version, items.arch) - local ok, err = download(filename) + local ok, err = fetch.fetch_url(filename) if not ok then all_ok = false any_err = any_err .. "\n" .. err @@ -80,7 +56,35 @@ function run(...) search.print_results(results) return nil, "Please narrow your query or use --all." end - else - return nil, "Could not find a result named "..name..(version and " "..version or "").."." end + return nil, "Could not find a result named "..name..(version and " "..version or "").."." +end + +--- Driver function for the "download" command. +-- @param name string: a rock name. +-- @param version string or nil: if the name of a package is given, a +-- version may also be passed. +-- @return boolean or (nil, string): true if successful or nil followed +-- by an error message. +function run(...) + local flags, name, version = util.parse_flags(...) + + assert(type(version) == "string" or not version) + if type(name) ~= "string" and not flags["all"] then + return nil, "Argument missing, see help." + end + if not name then name, version = "", "" end + + local arch + + if flags["source"] then + arch = "src" + elseif flags["rockspec"] then + arch = "rockspec" + elseif flags["arch"] then + arch = flags["arch"] + end + + local dl, err = download(arch, name, version, flags["all"]) + return dl and true, err end -- cgit v1.2.3-55-g6feb