diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/admin_remove.lua | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/luarocks/admin_remove.lua b/src/luarocks/admin_remove.lua new file mode 100644 index 00000000..1b1f845c --- /dev/null +++ b/src/luarocks/admin_remove.lua | |||
@@ -0,0 +1,99 @@ | |||
1 | |||
2 | --- Module implementing the luarocks-admin "remove" command. | ||
3 | -- Removes a rock or rockspec from a rocks server. | ||
4 | module("luarocks.admin_remove", package.seeall) | ||
5 | |||
6 | local cfg = require("luarocks.cfg") | ||
7 | local util = require("luarocks.util") | ||
8 | local fetch = require("luarocks.fetch") | ||
9 | local dir = require("luarocks.dir") | ||
10 | local manif = require("luarocks.manif") | ||
11 | local index = require("luarocks.index") | ||
12 | local fs = require("luarocks.fs") | ||
13 | local cache = require("luarocks.cache") | ||
14 | |||
15 | help_summary = "Remove a rock or rockspec from a rocks server." | ||
16 | help_arguments = "[--from=<server>] [--no-refresh] {<rockspec>|<rock>...}" | ||
17 | help = [[ | ||
18 | Arguments are local files, which may be rockspecs or rocks. | ||
19 | The flag --from indicates which server to use. | ||
20 | If not given, the default server set in the upload_server variable | ||
21 | from the configuration file is used instead. | ||
22 | The flag --no-refresh indicates the local cache should not be refreshed | ||
23 | prior to generation of the updated manifest. | ||
24 | ]] | ||
25 | |||
26 | local function remove_files_from_server(refresh, rockfiles, server, upload_server) | ||
27 | assert(type(refresh) == "boolean" or not refresh) | ||
28 | assert(type(rockfiles) == "table") | ||
29 | assert(type(server) == "string") | ||
30 | assert(type(upload_server) == "table" or not upload_server) | ||
31 | |||
32 | local download_url = 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() | ||
42 | |||
43 | local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url | ||
44 | 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 | ||
46 | return nil, protocol | ||
47 | end | ||
48 | if protocol ~= "rsync" then | ||
49 | return nil, "This command requires 'rsync', check your configuration." | ||
50 | end | ||
51 | |||
52 | fs.change_dir(at) | ||
53 | |||
54 | local nr_files = 0 | ||
55 | for i, rockfile in ipairs(rockfiles) do | ||
56 | local basename = dir.base_name(rockfile) | ||
57 | local file = dir.path(local_cache, basename) | ||
58 | print("Removing file "..file.."...") | ||
59 | if fs.delete(file) then | ||
60 | nr_files = nr_files + 1 | ||
61 | else | ||
62 | print("Failed removing "..file) | ||
63 | end | ||
64 | end | ||
65 | if nr_files == 0 then | ||
66 | return nil, "No files removed." | ||
67 | end | ||
68 | |||
69 | fs.change_dir(local_cache) | ||
70 | |||
71 | print("Updating manifest...") | ||
72 | manif.make_manifest(local_cache) | ||
73 | print("Updating index.html...") | ||
74 | index.make_index(local_cache) | ||
75 | |||
76 | local srv, path = server_path:match("([^/]+)(/.+)") | ||
77 | local cmd = "rsync -Oavz -e ssh "..local_cache.."/ "..user.."@"..srv..":"..path.."/" | ||
78 | |||
79 | print(cmd) | ||
80 | fs.execute(cmd) | ||
81 | |||
82 | return true | ||
83 | end | ||
84 | |||
85 | function run(...) | ||
86 | local files = { util.parse_flags(...) } | ||
87 | local flags = table.remove(files, 1) | ||
88 | if #files < 1 then | ||
89 | return nil, "Argument missing, see help." | ||
90 | end | ||
91 | local server = flags["from"] | ||
92 | if not server then server = cfg.upload_server end | ||
93 | if not server then | ||
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 | ||
99 | |||