Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix refresh_cache and remove code redundancy

  • Loading branch information...
commit be344afda0cf2cadcee7cfec0e8c7512da5b9ee0 1 parent 0cdc12f
@hishamhm hishamhm authored
View
26 src/luarocks/add.lua
@@ -28,23 +28,11 @@ local function add_files_to_server(refresh, rockfiles, server, upload_server)
assert(type(rockfiles) == "table")
assert(type(server) == "string")
assert(type(upload_server) == "table" or not upload_server)
-
- local download_url = server
- local login_url = nil
- if upload_server then
- if upload_server.rsync then download_url = "rsync://"..upload_server.rsync
- elseif upload_server.http then download_url = "http://"..upload_server.http
- elseif upload_server.ftp then download_url = "ftp://"..upload_server.ftp
- end
-
- if upload_server.ftp then login_url = "ftp://"..upload_server.ftp
- elseif upload_server.sftp then login_url = "sftp://"..upload_server.sftp
- end
- end
+ local download_url, login_url = cache.get_server_urls(server, upload_server)
local at = fs.current_dir()
-
local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url
+
local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password)
if not local_cache then
return nil, protocol
@@ -113,12 +101,8 @@ function run(...)
if #files < 1 then
return nil, "Argument missing, see help."
end
- local server = flags["to"]
- if not server then server = cfg.upload_server end
- if not server then
- return nil, "No server specified with --to and no default configured with upload_server."
- end
-
- return add_files_to_server(not flags["no-refresh"], files, server, cfg.upload_servers and cfg.upload_servers[server])
+ local server, server_table = cache.get_upload_server(flags["to"])
+ if not server then return nil, server_table end
+ return add_files_to_server(not flags["no-refresh"], files, server, server_table)
end
View
22 src/luarocks/admin_remove.lua
@@ -29,18 +29,10 @@ local function remove_files_from_server(refresh, rockfiles, server, upload_serve
assert(type(server) == "string")
assert(type(upload_server) == "table" or not upload_server)
- local download_url = server
- if upload_server then
- if upload_server.rsync then
- download_url = "rsync://"..upload_server.rsync
- else
- return nil, "This command requires 'rsync', check your configuration."
- end
- end
-
+ local download_url, login_url = cache.get_server_urls(server, upload_server)
local at = fs.current_dir()
-
local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url
+
local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password)
if not local_cache then
return nil, protocol
@@ -88,12 +80,8 @@ function run(...)
if #files < 1 then
return nil, "Argument missing, see help."
end
- local server = flags["from"]
- if not server then server = cfg.upload_server end
- if not server then
- return nil, "No server specified with --to and no default configured with upload_server."
- end
-
- return remove_files_from_server(not flags["no-refresh"], files, server, cfg.upload_servers and cfg.upload_servers[server])
+ local server, server_table = cache.get_upload_server(flags["from"])
+ if not server then return nil, server_table end
+ return remove_files_from_server(not flags["no-refresh"], files, server, server_table)
end
View
24 src/luarocks/cache.lua
@@ -8,6 +8,30 @@ local cfg = require("luarocks.cfg")
local dir = require("luarocks.dir")
local util = require("luarocks.util")
+function get_upload_server(server)
+ if not server then server = cfg.upload_server end
+ if not server then
+ return nil, "No server specified and no default configured with upload_server."
+ end
+ return server, cfg.upload_servers and cfg.upload_servers[server]
+end
+
+function get_server_urls(server, upload_server)
+ local download_url = server
+ local login_url = nil
+ if upload_server then
+ if upload_server.rsync then download_url = "rsync://"..upload_server.rsync
+ elseif upload_server.http then download_url = "http://"..upload_server.http
+ elseif upload_server.ftp then download_url = "ftp://"..upload_server.ftp
+ end
+
+ if upload_server.ftp then login_url = "ftp://"..upload_server.ftp
+ elseif upload_server.sftp then login_url = "sftp://"..upload_server.sftp
+ end
+ end
+ return download_url, login_url
+end
+
function split_server_url(server, url, user, password)
local protocol, server_path = dir.split_url(url)
if server_path:match("@") then
View
14 src/luarocks/refresh_cache.lua
@@ -15,15 +15,11 @@ from the configuration file is used instead.
function run(...)
local flags = util.parse_flags(...)
- local server = flags["from"]
- if not server then server = cfg.upload_server end
- if not server then
- return nil, "No server specified with --from and no default configured with upload_server."
- end
- if cfg.upload_servers and cfg.upload_servers[server] and cfg.upload_servers[server].http then
- server = "http://"..cfg.upload_servers[server].http
- end
- local ok, err = cache.refresh_local_cache(server, cfg.upload_user, cfg.upload_password)
+ local server, upload_server = cache.get_upload_server(flags["from"])
+ if not server then return nil, upload_server end
+ local download_url = cache.get_server_urls(server, upload_server)
+
+ local ok, err = cache.refresh_local_cache(server, download_url, cfg.upload_user, cfg.upload_password)
if not ok then
return nil, err
else
Please sign in to comment.
Something went wrong with that request. Please try again.