diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2011-08-14 14:44:42 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2011-08-14 14:44:42 -0300 |
commit | be344afda0cf2cadcee7cfec0e8c7512da5b9ee0 (patch) | |
tree | 49275a90335bdd76e3d2d8b1f7f656c0421b0308 | |
parent | 0cdc12f51e164c08962b05b830628b2ed867d2db (diff) | |
download | luarocks-be344afda0cf2cadcee7cfec0e8c7512da5b9ee0.tar.gz luarocks-be344afda0cf2cadcee7cfec0e8c7512da5b9ee0.tar.bz2 luarocks-be344afda0cf2cadcee7cfec0e8c7512da5b9ee0.zip |
Fix refresh_cache and remove code redundancy
-rw-r--r-- | src/luarocks/add.lua | 26 | ||||
-rw-r--r-- | src/luarocks/admin_remove.lua | 22 | ||||
-rw-r--r-- | src/luarocks/cache.lua | 24 | ||||
-rw-r--r-- | src/luarocks/refresh_cache.lua | 14 |
4 files changed, 39 insertions, 47 deletions
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) | |||
28 | assert(type(rockfiles) == "table") | 28 | assert(type(rockfiles) == "table") |
29 | assert(type(server) == "string") | 29 | assert(type(server) == "string") |
30 | assert(type(upload_server) == "table" or not upload_server) | 30 | assert(type(upload_server) == "table" or not upload_server) |
31 | |||
32 | local download_url = server | ||
33 | local login_url = nil | ||
34 | if upload_server then | ||
35 | if upload_server.rsync then download_url = "rsync://"..upload_server.rsync | ||
36 | elseif upload_server.http then download_url = "http://"..upload_server.http | ||
37 | elseif upload_server.ftp then download_url = "ftp://"..upload_server.ftp | ||
38 | end | ||
39 | |||
40 | if upload_server.ftp then login_url = "ftp://"..upload_server.ftp | ||
41 | elseif upload_server.sftp then login_url = "sftp://"..upload_server.sftp | ||
42 | end | ||
43 | end | ||
44 | 31 | ||
32 | local download_url, login_url = cache.get_server_urls(server, upload_server) | ||
45 | local at = fs.current_dir() | 33 | local at = fs.current_dir() |
46 | |||
47 | local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url | 34 | local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url |
35 | |||
48 | local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password) | 36 | local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password) |
49 | if not local_cache then | 37 | if not local_cache then |
50 | return nil, protocol | 38 | return nil, protocol |
@@ -113,12 +101,8 @@ function run(...) | |||
113 | if #files < 1 then | 101 | if #files < 1 then |
114 | return nil, "Argument missing, see help." | 102 | return nil, "Argument missing, see help." |
115 | end | 103 | end |
116 | local server = flags["to"] | 104 | local server, server_table = cache.get_upload_server(flags["to"]) |
117 | if not server then server = cfg.upload_server end | 105 | if not server then return nil, server_table end |
118 | if not server then | 106 | return add_files_to_server(not flags["no-refresh"], files, server, server_table) |
119 | return nil, "No server specified with --to and no default configured with upload_server." | ||
120 | end | ||
121 | |||
122 | return add_files_to_server(not flags["no-refresh"], files, server, cfg.upload_servers and cfg.upload_servers[server]) | ||
123 | end | 107 | end |
124 | 108 | ||
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 | |||
29 | assert(type(server) == "string") | 29 | assert(type(server) == "string") |
30 | assert(type(upload_server) == "table" or not upload_server) | 30 | assert(type(upload_server) == "table" or not upload_server) |
31 | 31 | ||
32 | local download_url = server | 32 | local download_url, login_url = cache.get_server_urls(server, upload_server) |
33 | if upload_server then | ||
34 | if upload_server.rsync then | ||
35 | download_url = "rsync://"..upload_server.rsync | ||
36 | else | ||
37 | return nil, "This command requires 'rsync', check your configuration." | ||
38 | end | ||
39 | end | ||
40 | |||
41 | local at = fs.current_dir() | 33 | local at = fs.current_dir() |
42 | |||
43 | local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url | 34 | local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url |
35 | |||
44 | local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password) | 36 | local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password) |
45 | if not local_cache then | 37 | if not local_cache then |
46 | return nil, protocol | 38 | return nil, protocol |
@@ -88,12 +80,8 @@ function run(...) | |||
88 | if #files < 1 then | 80 | if #files < 1 then |
89 | return nil, "Argument missing, see help." | 81 | return nil, "Argument missing, see help." |
90 | end | 82 | end |
91 | local server = flags["from"] | 83 | local server, server_table = cache.get_upload_server(flags["from"]) |
92 | if not server then server = cfg.upload_server end | 84 | if not server then return nil, server_table end |
93 | if not server then | 85 | return remove_files_from_server(not flags["no-refresh"], files, server, server_table) |
94 | return nil, "No server specified with --to and no default configured with upload_server." | ||
95 | end | ||
96 | |||
97 | return remove_files_from_server(not flags["no-refresh"], files, server, cfg.upload_servers and cfg.upload_servers[server]) | ||
98 | end | 86 | end |
99 | 87 | ||
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") | |||
8 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
9 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
10 | 10 | ||
11 | function get_upload_server(server) | ||
12 | if not server then server = cfg.upload_server end | ||
13 | if not server then | ||
14 | return nil, "No server specified and no default configured with upload_server." | ||
15 | end | ||
16 | return server, cfg.upload_servers and cfg.upload_servers[server] | ||
17 | end | ||
18 | |||
19 | function get_server_urls(server, upload_server) | ||
20 | local download_url = server | ||
21 | local login_url = nil | ||
22 | if upload_server then | ||
23 | if upload_server.rsync then download_url = "rsync://"..upload_server.rsync | ||
24 | elseif upload_server.http then download_url = "http://"..upload_server.http | ||
25 | elseif upload_server.ftp then download_url = "ftp://"..upload_server.ftp | ||
26 | end | ||
27 | |||
28 | if upload_server.ftp then login_url = "ftp://"..upload_server.ftp | ||
29 | elseif upload_server.sftp then login_url = "sftp://"..upload_server.sftp | ||
30 | end | ||
31 | end | ||
32 | return download_url, login_url | ||
33 | end | ||
34 | |||
11 | function split_server_url(server, url, user, password) | 35 | function split_server_url(server, url, user, password) |
12 | local protocol, server_path = dir.split_url(url) | 36 | local protocol, server_path = dir.split_url(url) |
13 | if server_path:match("@") then | 37 | 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. | |||
15 | 15 | ||
16 | function run(...) | 16 | function run(...) |
17 | local flags = util.parse_flags(...) | 17 | local flags = util.parse_flags(...) |
18 | local server = flags["from"] | 18 | local server, upload_server = cache.get_upload_server(flags["from"]) |
19 | if not server then server = cfg.upload_server end | 19 | if not server then return nil, upload_server end |
20 | if not server then | 20 | local download_url = cache.get_server_urls(server, upload_server) |
21 | return nil, "No server specified with --from and no default configured with upload_server." | 21 | |
22 | end | 22 | local ok, err = cache.refresh_local_cache(server, download_url, cfg.upload_user, cfg.upload_password) |
23 | if cfg.upload_servers and cfg.upload_servers[server] and cfg.upload_servers[server].http then | ||
24 | server = "http://"..cfg.upload_servers[server].http | ||
25 | end | ||
26 | local ok, err = cache.refresh_local_cache(server, cfg.upload_user, cfg.upload_password) | ||
27 | if not ok then | 23 | if not ok then |
28 | return nil, err | 24 | return nil, err |
29 | else | 25 | else |