From be344afda0cf2cadcee7cfec0e8c7512da5b9ee0 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sun, 14 Aug 2011 14:44:42 -0300 Subject: Fix refresh_cache and remove code redundancy --- src/luarocks/add.lua | 26 +++++--------------------- src/luarocks/admin_remove.lua | 22 +++++----------------- src/luarocks/cache.lua | 24 ++++++++++++++++++++++++ src/luarocks/refresh_cache.lua | 14 +++++--------- 4 files changed, 39 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/luarocks/add.lua b/src/luarocks/add.lua index 0dcb74bb..92b5ac4b 100644 --- a/src/luarocks/add.lua +++ b/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 diff --git a/src/luarocks/admin_remove.lua b/src/luarocks/admin_remove.lua index 95213ecc..1b897e7d 100644 --- a/src/luarocks/admin_remove.lua +++ b/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 diff --git a/src/luarocks/cache.lua b/src/luarocks/cache.lua index d3fbdf5b..692d3346 100644 --- a/src/luarocks/cache.lua +++ b/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 diff --git a/src/luarocks/refresh_cache.lua b/src/luarocks/refresh_cache.lua index 92852f2a..23f86a13 100644 --- a/src/luarocks/refresh_cache.lua +++ b/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 -- cgit v1.2.3-55-g6feb