aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/luarocks/upload/api.lua25
-rw-r--r--src/luarocks/util.lua21
2 files changed, 23 insertions, 23 deletions
diff --git a/src/luarocks/upload/api.lua b/src/luarocks/upload/api.lua
index 30857158..bf83faf6 100644
--- a/src/luarocks/upload/api.lua
+++ b/src/luarocks/upload/api.lua
@@ -109,27 +109,6 @@ local function encode_query_string(t, sep)
109 return table.concat(buf) 109 return table.concat(buf)
110end 110end
111 111
112-- An ode to the multitude of JSON libraries out there...
113local function require_json()
114 local list = { "cjson", "dkjson", "json" }
115 for _, lib in ipairs(list) do
116 local json_ok, json = pcall(require, lib)
117 if json_ok then
118 pcall(json.use_lpeg) -- optional feature in dkjson
119 return json_ok, json
120 end
121 end
122 local errmsg = "Failed loading "
123 for i, name in ipairs(list) do
124 if i == #list then
125 errmsg = errmsg .."and '"..name.."'. Use 'luarocks search <partial-name>' to search for a library and 'luarocks install <name>' to install one."
126 else
127 errmsg = errmsg .."'"..name.."', "
128 end
129 end
130 return nil, errmsg
131end
132
133local function redact_api_url(url) 112local function redact_api_url(url)
134 url = tostring(url) 113 url = tostring(url)
135 return (url:gsub(".*/api/[^/]+/[^/]+", "")) or "" 114 return (url:gsub(".*/api/[^/]+/[^/]+", "")) or ""
@@ -140,7 +119,7 @@ if not ltn12_ok then -- If not using LuaSocket and/or LuaSec...
140 119
141function Api:request(url, params, post_params) 120function Api:request(url, params, post_params)
142 local vars = cfg.variables 121 local vars = cfg.variables
143 local json_ok, json = require_json() 122 local json_ok, json = util.require_json()
144 if not json_ok then return nil, "A JSON library is required for this command. "..json end 123 if not json_ok then return nil, "A JSON library is required for this command. "..json end
145 124
146 if fs.which_tool("downloader") == "wget" then 125 if fs.which_tool("downloader") == "wget" then
@@ -204,7 +183,7 @@ else -- use LuaSocket and LuaSec
204local warned_luasec = false 183local warned_luasec = false
205 184
206function Api:request(url, params, post_params) 185function Api:request(url, params, post_params)
207 local json_ok, json = require_json() 186 local json_ok, json = util.require_json()
208 if not json_ok then return nil, "A JSON library is required for this command. "..json end 187 if not json_ok then return nil, "A JSON library is required for this command. "..json end
209 local server = tostring(self.config.server) 188 local server = tostring(self.config.server)
210 local http_ok, http 189 local http_ok, http
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua
index 2d44c189..7d7ce921 100644
--- a/src/luarocks/util.lua
+++ b/src/luarocks/util.lua
@@ -527,4 +527,25 @@ function util.deep_copy(tbl)
527 return copy 527 return copy
528end 528end
529 529
530-- An ode to the multitude of JSON libraries out there...
531function util.require_json()
532 local list = { "cjson", "dkjson", "json" }
533 for _, lib in ipairs(list) do
534 local json_ok, json = pcall(require, lib)
535 if json_ok then
536 pcall(json.use_lpeg) -- optional feature in dkjson
537 return json_ok, json
538 end
539 end
540 local errmsg = "Failed loading "
541 for i, name in ipairs(list) do
542 if i == #list then
543 errmsg = errmsg .."and '"..name.."'. Use 'luarocks search <partial-name>' to search for a library and 'luarocks install <name>' to install one."
544 else
545 errmsg = errmsg .."'"..name.."', "
546 end
547 end
548 return nil, errmsg
549end
550
530return util 551return util