aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2012-05-20 23:29:07 -0300
committerHisham Muhammad <hisham@gobolinux.org>2012-05-20 23:29:07 -0300
commit2f654a1eeb0ea567867149951f9f72ad96070372 (patch)
tree7bf17a6ca2576dbe18e9b23fd526c345b98e89e4 /src
parentd0aa4e236883fc7e118cab32207e8d585a935023 (diff)
downloadluarocks-2f654a1eeb0ea567867149951f9f72ad96070372.tar.gz
luarocks-2f654a1eeb0ea567867149951f9f72ad96070372.tar.bz2
luarocks-2f654a1eeb0ea567867149951f9f72ad96070372.zip
Refactor the download operation so it can be used by other modules.
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/download.lua72
1 files 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 = [[
18--arch=<arch> Download rock for a specific architecture. 18--arch=<arch> Download rock for a specific architecture.
19]] 19]]
20 20
21local function download(rock_file) 21function download(arch, name, version, all)
22 local rock = fetch.fetch_url(rock_file)
23 return rock ~= nil
24end
25
26--- Driver function for the "download" command.
27-- @param name string: a rock name.
28-- @param version string or nil: if the name of a package is given, a
29-- version may also be passed.
30-- @return boolean or (nil, string): true if successful or nil followed
31-- by an error message.
32function run(...)
33 local flags, name, version = util.parse_flags(...)
34
35 assert(type(version) == "string" or not version)
36 if type(name) ~= "string" and not flags["all"] then
37 return nil, "Argument missing, see help."
38 end
39 if not name then name, version = "", "" end
40
41 local query = search.make_query(name, version)
42 if flags["source"] then
43 query.arch = "src"
44 elseif flags["rockspec"] then
45 query.arch = "rockspec"
46 elseif flags["arch"] then
47 query.arch = flags["arch"]
48 end
49 local results, err 22 local results, err
50 if flags["all"] then 23 local query = search.make_query(name, version)
24 if arch then query.arch = arch end
25 if all then
51 if name == "" then query.exact_name = false end 26 if name == "" then query.exact_name = false end
52 results, err = search.search_repos(query) 27 results, err = search.search_repos(query)
53 else 28 else
54 results, err = search.find_suitable_rock(query) 29 results, err = search.find_suitable_rock(query)
55 end 30 end
56 if type(results) == "string" then 31 if type(results) == "string" then
57 return download(results) 32 local file = fetch.fetch_url(results)
33 return file
58 elseif type(results) == "table" and next(results) then 34 elseif type(results) == "table" and next(results) then
59 if flags["all"] then 35 if all then
60 local all_ok = true 36 local all_ok = true
61 local any_err = "" 37 local any_err = ""
62 for name, result in pairs(results) do 38 for name, result in pairs(results) do
63 for version, versions in pairs(result) do 39 for version, versions in pairs(result) do
64 for _,items in pairs(versions) do 40 for _,items in pairs(versions) do
65 local filename = path.make_url(items.repo, name, version, items.arch) 41 local filename = path.make_url(items.repo, name, version, items.arch)
66 local ok, err = download(filename) 42 local ok, err = fetch.fetch_url(filename)
67 if not ok then 43 if not ok then
68 all_ok = false 44 all_ok = false
69 any_err = any_err .. "\n" .. err 45 any_err = any_err .. "\n" .. err
@@ -80,7 +56,35 @@ function run(...)
80 search.print_results(results) 56 search.print_results(results)
81 return nil, "Please narrow your query or use --all." 57 return nil, "Please narrow your query or use --all."
82 end 58 end
83 else
84 return nil, "Could not find a result named "..name..(version and " "..version or "").."."
85 end 59 end
60 return nil, "Could not find a result named "..name..(version and " "..version or "").."."
61end
62
63--- Driver function for the "download" command.
64-- @param name string: a rock name.
65-- @param version string or nil: if the name of a package is given, a
66-- version may also be passed.
67-- @return boolean or (nil, string): true if successful or nil followed
68-- by an error message.
69function run(...)
70 local flags, name, version = util.parse_flags(...)
71
72 assert(type(version) == "string" or not version)
73 if type(name) ~= "string" and not flags["all"] then
74 return nil, "Argument missing, see help."
75 end
76 if not name then name, version = "", "" end
77
78 local arch
79
80 if flags["source"] then
81 arch = "src"
82 elseif flags["rockspec"] then
83 arch = "rockspec"
84 elseif flags["arch"] then
85 arch = flags["arch"]
86 end
87
88 local dl, err = download(arch, name, version, flags["all"])
89 return dl and true, err
86end 90end