diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2019-03-14 22:13:18 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2019-03-14 23:19:39 -0300 |
commit | 9064b9f1ff7bbdd7a25ac6e8e02ceb0fe9603e93 (patch) | |
tree | 38a715a671ab436595945ef7f336b63aef1d395e /src | |
parent | 83dceb3606505c226789e58ec7eb467da28ee204 (diff) | |
download | luarocks-9064b9f1ff7bbdd7a25ac6e8e02ceb0fe9603e93.tar.gz luarocks-9064b9f1ff7bbdd7a25ac6e8e02ceb0fe9603e93.tar.bz2 luarocks-9064b9f1ff7bbdd7a25ac6e8e02ceb0fe9603e93.zip |
refactor: move require_json from luarocks.upload.api to luarocks.util
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/upload/api.lua | 25 | ||||
-rw-r--r-- | src/luarocks/util.lua | 21 |
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) |
110 | end | 110 | end |
111 | 111 | ||
112 | -- An ode to the multitude of JSON libraries out there... | ||
113 | local 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 | ||
131 | end | ||
132 | |||
133 | local function redact_api_url(url) | 112 | local 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 | ||
141 | function Api:request(url, params, post_params) | 120 | function 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 | |||
204 | local warned_luasec = false | 183 | local warned_luasec = false |
205 | 184 | ||
206 | function Api:request(url, params, post_params) | 185 | function 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 |
528 | end | 528 | end |
529 | 529 | ||
530 | -- An ode to the multitude of JSON libraries out there... | ||
531 | function 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 | ||
549 | end | ||
550 | |||
530 | return util | 551 | return util |