diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2014-05-26 19:40:58 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2014-05-26 19:40:58 -0300 |
| commit | 1788d0bb8c08e1ff7e671f20a023b3dcc4f80206 (patch) | |
| tree | 2a83a24fa8bd4d4b7ea682b275a402b32ba4220a /src | |
| parent | 0d9bc9eb541cdac6ae55c5c313a29be275cdd812 (diff) | |
| download | luarocks-1788d0bb8c08e1ff7e671f20a023b3dcc4f80206.tar.gz luarocks-1788d0bb8c08e1ff7e671f20a023b3dcc4f80206.tar.bz2 luarocks-1788d0bb8c08e1ff7e671f20a023b3dcc4f80206.zip | |
Fix chmod argument order.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/upload/api.lua | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/src/luarocks/upload/api.lua b/src/luarocks/upload/api.lua new file mode 100644 index 00000000..818d293e --- /dev/null +++ b/src/luarocks/upload/api.lua | |||
| @@ -0,0 +1,182 @@ | |||
| 1 | |||
| 2 | local api = {} | ||
| 3 | |||
| 4 | local cfg = require("luarocks.cfg") | ||
| 5 | local fs = require("luarocks.fs") | ||
| 6 | local util = require("luarocks.util") | ||
| 7 | local persist = require("luarocks.persist") | ||
| 8 | |||
| 9 | local Api = {} | ||
| 10 | |||
| 11 | local function upload_config_file() | ||
| 12 | local _, _, home_conf, home_ok = cfg.which_config() | ||
| 13 | if not home_conf then | ||
| 14 | return nil | ||
| 15 | end | ||
| 16 | return (home_conf:gsub("/[^/]+$", "/upload_config.lua")) | ||
| 17 | end | ||
| 18 | |||
| 19 | function Api:load_config() | ||
| 20 | local upload_conf = upload_config_file() | ||
| 21 | print(upload_conf) | ||
| 22 | if not upload_conf then return nil end | ||
| 23 | local cfg, err = persist.load_into_table(upload_conf) | ||
| 24 | return cfg | ||
| 25 | end | ||
| 26 | |||
| 27 | function Api:save_config() | ||
| 28 | -- Test configuration before saving it. | ||
| 29 | local res, err = self:raw_method("status") | ||
| 30 | if not res then | ||
| 31 | return nil, err | ||
| 32 | end | ||
| 33 | if res.errors then | ||
| 34 | util.printerr("Server says: " .. tostring(res.errors[1])) | ||
| 35 | return | ||
| 36 | end | ||
| 37 | local upload_conf = upload_config_file() | ||
| 38 | if not upload_conf then return nil end | ||
| 39 | persist.save_from_table(upload_conf, self.config) | ||
| 40 | fs.chmod(upload_conf, "0600") | ||
| 41 | end | ||
| 42 | |||
| 43 | function Api:check_version() | ||
| 44 | if not self._server_tool_version then | ||
| 45 | local tool_version = cfg.upload.tool_version | ||
| 46 | local res, err = self:request("http://" .. tostring(self.config.server) .. "/api/tool_version", { | ||
| 47 | current = tool_version | ||
| 48 | }) | ||
| 49 | if not res then | ||
| 50 | return nil, err | ||
| 51 | end | ||
| 52 | if not res.version then | ||
| 53 | return nil, "failed to fetch tool version" | ||
| 54 | end | ||
| 55 | self._server_tool_version = res.version | ||
| 56 | if res.force_update then | ||
| 57 | return nil, "Your upload client is too out of date to continue, please upgrade LuaRocks." | ||
| 58 | end | ||
| 59 | if res.version ~= tool_version then | ||
| 60 | util.printerr("Warning: Your LuaRocks is out of date, consider upgrading.") | ||
| 61 | end | ||
| 62 | end | ||
| 63 | return true | ||
| 64 | end | ||
| 65 | |||
| 66 | function Api:method(...) | ||
| 67 | local res, err = self:raw_method(...) | ||
| 68 | if not res then | ||
| 69 | return nil, err | ||
| 70 | end | ||
| 71 | if res.errors then | ||
| 72 | if res.errors[1] == "Invalid key" then | ||
| 73 | return nil, res.errors[1] .. " (use the --api-key flag to change)" | ||
| 74 | end | ||
| 75 | local msg = table.concat(res.errors, ", ") | ||
| 76 | return nil, "API Failed: " .. msg | ||
| 77 | end | ||
| 78 | return res | ||
| 79 | end | ||
| 80 | |||
| 81 | function Api:raw_method(path, ...) | ||
| 82 | self:check_version() | ||
| 83 | local url = "http://" .. tostring(self.config.server) .. "/api/" .. tostring(cfg.upload.api_version) .. "/" .. tostring(self.config.key) .. "/" .. tostring(path) | ||
| 84 | return self:request(url, ...) | ||
| 85 | end | ||
| 86 | |||
| 87 | local function encode_query_string(t, sep) | ||
| 88 | local url = require("socket.url") | ||
| 89 | if sep == nil then | ||
| 90 | sep = "&" | ||
| 91 | end | ||
| 92 | local i = 0 | ||
| 93 | local buf = { } | ||
| 94 | for k, v in pairs(t) do | ||
| 95 | if type(k) == "number" and type(v) == "table" then | ||
| 96 | k, v = v[1], v[2] | ||
| 97 | end | ||
| 98 | buf[i + 1] = url.escape(k) | ||
| 99 | buf[i + 2] = "=" | ||
| 100 | buf[i + 3] = url.escape(v) | ||
| 101 | buf[i + 4] = sep | ||
| 102 | i = i + 4 | ||
| 103 | end | ||
| 104 | buf[i] = nil | ||
| 105 | return table.concat(buf) | ||
| 106 | end | ||
| 107 | |||
| 108 | -- An ode to the multitude of JSON libraries out there... | ||
| 109 | local function require_json() | ||
| 110 | for _, lib in ipairs({ "cjson", "dkjson", "json" }) do | ||
| 111 | local json_ok, json = pcall(require, lib) | ||
| 112 | if json_ok then | ||
| 113 | return json_ok, json | ||
| 114 | end | ||
| 115 | end | ||
| 116 | return nil | ||
| 117 | end | ||
| 118 | |||
| 119 | function Api:request(url, params, post_params) | ||
| 120 | local http_ok, http = pcall(require, "socket.http") | ||
| 121 | local ltn12_ok, ltn12 = pcall(require, "ltn12") | ||
| 122 | local json_ok, json = require_json() | ||
| 123 | if not http_ok then return nil, "LuaSocket is required for this command." end | ||
| 124 | if not json_ok then return nil, "A JSON library is required for this command." end | ||
| 125 | |||
| 126 | if not self.config.key then | ||
| 127 | return nil, "Must have API key before performing any actions." | ||
| 128 | end | ||
| 129 | local body | ||
| 130 | local headers = {} | ||
| 131 | if params and next(params) then | ||
| 132 | url = url .. ("?" .. encode_query_string(params)) | ||
| 133 | end | ||
| 134 | if post_params then | ||
| 135 | local multipart = require("luarocks.upload.multipart") | ||
| 136 | local boundary | ||
| 137 | body, boundary = multipart.encode(post_params) | ||
| 138 | headers["Content-length"] = #body | ||
| 139 | headers["Content-type"] = "multipart/form-data; boundary=" .. tostring(boundary) | ||
| 140 | end | ||
| 141 | local method = post_params and "POST" or "GET" | ||
| 142 | if self.debug then | ||
| 143 | util.printout("[" .. tostring(method) .. "] " .. tostring(url) .. " ... ") | ||
| 144 | end | ||
| 145 | local out = {} | ||
| 146 | local _, status = http.request({ | ||
| 147 | url = url, | ||
| 148 | headers = headers, | ||
| 149 | method = method, | ||
| 150 | sink = ltn12.sink.table(out), | ||
| 151 | source = body and ltn12.source.string(body) | ||
| 152 | }) | ||
| 153 | if self.debug then | ||
| 154 | util.printout(tostring(status)) | ||
| 155 | end | ||
| 156 | if status ~= 200 then | ||
| 157 | return nil, "API returned " .. tostring(status) .. " - " .. tostring(url) | ||
| 158 | end | ||
| 159 | return json.decode(table.concat(out)) | ||
| 160 | end | ||
| 161 | |||
| 162 | function api.new(flags, name) | ||
| 163 | local self = {} | ||
| 164 | setmetatable(self, { __index = Api }) | ||
| 165 | self.config = self:load_config() or {} | ||
| 166 | self.config.server = flags["server"] or self.config.server or cfg.upload.server | ||
| 167 | self.config.version = self.config.version or cfg.upload.version | ||
| 168 | self.config.key = flags["api-key"] or self.config.key | ||
| 169 | self.debug = flags["debug"] | ||
| 170 | if not self.config.key then | ||
| 171 | return nil, "You need an API key to upload rocks.\n" .. | ||
| 172 | "Navigate to http://"..self.config.server.."/settings to get a key\n" .. | ||
| 173 | "and then pass it through the --api-key=<key> flag." | ||
| 174 | end | ||
| 175 | if flags["api-key"] then | ||
| 176 | self:save_config() | ||
| 177 | end | ||
| 178 | return self | ||
| 179 | end | ||
| 180 | |||
| 181 | return api | ||
| 182 | |||
