diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-08-02 18:47:49 +0300 |
---|---|---|
committer | V1K1NGbg <victor@ilchev.com> | 2024-08-05 20:51:31 +0300 |
commit | 5b189eea169bd69faa6705f6df68b8b132d2b0dc (patch) | |
tree | 0a34f4aa1371ac4bc24d5d0afd7dd9804d360704 | |
parent | 2fe9dd208a3f2f3f1b308102809c4f2cbdd826f8 (diff) | |
download | luarocks-5b189eea169bd69faa6705f6df68b8b132d2b0dc.tar.gz luarocks-5b189eea169bd69faa6705f6df68b8b132d2b0dc.tar.bz2 luarocks-5b189eea169bd69faa6705f6df68b8b132d2b0dc.zip |
working on api
-rw-r--r-- | src/luarocks/core/cfg.d.tl | 4 | ||||
-rw-r--r-- | src/luarocks/core/util.tl | 2 | ||||
-rw-r--r-- | src/luarocks/fs.d.tl | 1 | ||||
-rw-r--r-- | src/luarocks/persist.tl | 2 | ||||
-rw-r--r-- | src/luarocks/upload/api.tl | 64 | ||||
-rw-r--r-- | src/luarocks/upload/multipart.tl | 4 |
6 files changed, 45 insertions, 32 deletions
diff --git a/src/luarocks/core/cfg.d.tl b/src/luarocks/core/cfg.d.tl index 73e946dd..5ad95b6d 100644 --- a/src/luarocks/core/cfg.d.tl +++ b/src/luarocks/core/cfg.d.tl | |||
@@ -83,6 +83,10 @@ local record cfg | |||
83 | -- api | 83 | -- api |
84 | user_agent: string | 84 | user_agent: string |
85 | connection_timeout: number | 85 | connection_timeout: number |
86 | record upload | ||
87 | server: string | ||
88 | version: string | ||
89 | end | ||
86 | end | 90 | end |
87 | 91 | ||
88 | return cfg \ No newline at end of file | 92 | return cfg \ No newline at end of file |
diff --git a/src/luarocks/core/util.tl b/src/luarocks/core/util.tl index d23d1b59..76d7b42e 100644 --- a/src/luarocks/core/util.tl +++ b/src/luarocks/core/util.tl | |||
@@ -191,7 +191,7 @@ function util.split_string(str: string, delim: string, maxNb?: number): {string} | |||
191 | for part, pos in string.gmatch(str, pat) do | 191 | for part, pos in string.gmatch(str, pat) do |
192 | nb = nb + 1 | 192 | nb = nb + 1 |
193 | result[nb] = part | 193 | result[nb] = part |
194 | lastPos = tonumber(pos) as integer --? cast because math.tointeger returns an integer? | 194 | lastPos = tonumber(pos) as integer |
195 | if nb == maxNb then break end | 195 | if nb == maxNb then break end |
196 | end | 196 | end |
197 | -- Handle the last field | 197 | -- Handle the last field |
diff --git a/src/luarocks/fs.d.tl b/src/luarocks/fs.d.tl index 076eba06..17cf0517 100644 --- a/src/luarocks/fs.d.tl +++ b/src/luarocks/fs.d.tl | |||
@@ -26,6 +26,7 @@ local record fs | |||
26 | execute_string: function(string): boolean | 26 | execute_string: function(string): boolean |
27 | Q: function(string): string | 27 | Q: function(string): string |
28 | download: function(string, string): boolean, string | 28 | download: function(string, string): boolean, string |
29 | set_permissions: function(string, string, string) | ||
29 | end | 30 | end |
30 | 31 | ||
31 | return fs | 32 | return fs |
diff --git a/src/luarocks/persist.tl b/src/luarocks/persist.tl index 64b76639..5c6b47c3 100644 --- a/src/luarocks/persist.tl +++ b/src/luarocks/persist.tl | |||
@@ -201,7 +201,7 @@ end | |||
201 | -- @param sort_by table: an optional array indicating the order of top-level fields. | 201 | -- @param sort_by table: an optional array indicating the order of top-level fields. |
202 | -- @return boolean or (nil, string): true if successful, or nil and a | 202 | -- @return boolean or (nil, string): true if successful, or nil and a |
203 | -- message in case of errors. | 203 | -- message in case of errors. |
204 | function persist.save_from_table(filename: string, tbl: PersistableTable, sort_by: util.SortBy<number | string>): boolean, string | 204 | function persist.save_from_table(filename: string, tbl: PersistableTable, sort_by?: util.SortBy<number | string>): boolean, string |
205 | local prefix = dir.dir_name(filename) | 205 | local prefix = dir.dir_name(filename) |
206 | fs.make_dir(prefix) | 206 | fs.make_dir(prefix) |
207 | local out = io.open(filename, "w") | 207 | local out = io.open(filename, "w") |
diff --git a/src/luarocks/upload/api.tl b/src/luarocks/upload/api.tl index 0c94a6ee..48c53ca2 100644 --- a/src/luarocks/upload/api.tl +++ b/src/luarocks/upload/api.tl | |||
@@ -1,5 +1,16 @@ | |||
1 | local record api | 1 | local record api |
2 | record Api | ||
3 | config: PersistableTable | ||
4 | debug: boolean | ||
5 | _server_tool_version: string | ||
6 | end | ||
2 | 7 | ||
8 | record Args | ||
9 | server: string | ||
10 | temp_key: string | ||
11 | api_key: string | ||
12 | debug: boolean | ||
13 | end | ||
3 | end | 14 | end |
4 | 15 | ||
5 | local cfg = require("luarocks.core.cfg") | 16 | local cfg = require("luarocks.core.cfg") |
@@ -11,16 +22,10 @@ local multipart = require("luarocks.upload.multipart") | |||
11 | local json = require("luarocks.vendor.dkjson") | 22 | local json = require("luarocks.vendor.dkjson") |
12 | local dir_sep = package.config:sub(1, 1) | 23 | local dir_sep = package.config:sub(1, 1) |
13 | 24 | ||
14 | local record Api | ||
15 | raw_method: function(Api, string) | ||
16 | record config | ||
17 | server: any --! why to string | ||
18 | key: string | ||
19 | end | ||
20 | debug: boolean | ||
21 | end | ||
22 | |||
23 | local type Parameters = multipart.Parameters | 25 | local type Parameters = multipart.Parameters |
26 | local type Api = api.Api | ||
27 | local type Args = api.Args | ||
28 | local type PersistableTable = persist.PersistableTable | ||
24 | 29 | ||
25 | local function upload_config_file(): string | 30 | local function upload_config_file(): string |
26 | if not cfg.config_files.user.file then | 31 | if not cfg.config_files.user.file then |
@@ -29,14 +34,14 @@ local function upload_config_file(): string | |||
29 | return (cfg.config_files.user.file:gsub("[\\/][^\\/]+$", dir_sep .. "upload_config.lua")) | 34 | return (cfg.config_files.user.file:gsub("[\\/][^\\/]+$", dir_sep .. "upload_config.lua")) |
30 | end | 35 | end |
31 | 36 | ||
32 | function Api:load_config(): {any: any} --? tighter bound? | 37 | function api.Api:load_config(): {any: any} |
33 | local upload_conf = upload_config_file() | 38 | local upload_conf = upload_config_file() |
34 | if not upload_conf then return nil end | 39 | if not upload_conf then return nil end |
35 | local config = persist.load_into_table(upload_conf) | 40 | local config = persist.load_into_table(upload_conf) |
36 | return config | 41 | return config |
37 | end | 42 | end |
38 | 43 | ||
39 | function Api:save_config(): nil, string --! nil? | 44 | function api.Api:save_config(): nil, string --! nil? |
40 | -- Test configuration before saving it. | 45 | -- Test configuration before saving it. |
41 | local res, err = self:raw_method("status") | 46 | local res, err = self:raw_method("status") |
42 | if not res then | 47 | if not res then |
@@ -56,7 +61,7 @@ function Api:save_config(): nil, string --! nil? | |||
56 | fs.set_permissions(upload_conf, "read", "user") | 61 | fs.set_permissions(upload_conf, "read", "user") |
57 | end | 62 | end |
58 | 63 | ||
59 | function Api:check_version() | 64 | function api.Api:check_version(): boolean, string |
60 | if not self._server_tool_version then | 65 | if not self._server_tool_version then |
61 | local tool_version = cfg.upload.tool_version | 66 | local tool_version = cfg.upload.tool_version |
62 | local res, err = self:request(tostring(self.config.server) .. "/api/tool_version", { | 67 | local res, err = self:request(tostring(self.config.server) .. "/api/tool_version", { |
@@ -79,7 +84,7 @@ function Api:check_version() | |||
79 | return true | 84 | return true |
80 | end | 85 | end |
81 | 86 | ||
82 | function Api:method(...) | 87 | function api.Api:method(...): {string : any}, string |
83 | local res, err = self:raw_method(...) | 88 | local res, err = self:raw_method(...) |
84 | if not res then | 89 | if not res then |
85 | return nil, err | 90 | return nil, err |
@@ -94,7 +99,7 @@ function Api:method(...) | |||
94 | return res | 99 | return res |
95 | end | 100 | end |
96 | 101 | ||
97 | function Api:raw_method(path: string, ...) --! path, ... type | 102 | function api.Api:raw_method(path: string, ...: Parameters): {string : any}, string |
98 | self:check_version() | 103 | self:check_version() |
99 | local url = tostring(self.config.server) .. "/api/" .. tostring(cfg.upload.api_version) .. "/" .. tostring(self.config.key) .. "/" .. path | 104 | local url = tostring(self.config.server) .. "/api/" .. tostring(cfg.upload.api_version) .. "/" .. tostring(self.config.key) .. "/" .. path |
100 | return self:request(url, ...) | 105 | return self:request(url, ...) |
@@ -107,12 +112,15 @@ local function encode_query_string(t: Parameters, sep?: string): string | |||
107 | local i = 0 | 112 | local i = 0 |
108 | local buf: {string} = { } | 113 | local buf: {string} = { } |
109 | for k, v in pairs(t.map) do --! pairs problem for Parameters | 114 | for k, v in pairs(t.map) do --! pairs problem for Parameters |
110 | if k is number and v is {string} then | 115 | local ks, vs: string, string |
111 | k, v = v[1], v[2] | 116 | if k is number and v is table then |
117 | ks, vs = v[1], v[2] | ||
118 | else | ||
119 | ks, vs = k, v | ||
112 | end | 120 | end |
113 | buf[i + 1] = multipart.url_escape(k as string) --! cast | 121 | buf[i + 1] = multipart.url_escape(ks) |
114 | buf[i + 2] = "=" | 122 | buf[i + 2] = "=" |
115 | buf[i + 3] = multipart.url_escape(v as string) --! cast | 123 | buf[i + 3] = multipart.url_escape(vs) |
116 | buf[i + 4] = sep | 124 | buf[i + 4] = sep |
117 | i = i + 4 | 125 | i = i + 4 |
118 | end | 126 | end |
@@ -121,14 +129,14 @@ local function encode_query_string(t: Parameters, sep?: string): string | |||
121 | end | 129 | end |
122 | 130 | ||
123 | local function redact_api_url(url: any): string | 131 | local function redact_api_url(url: any): string |
124 | url = tostring(url) | 132 | local urls = tostring(url) |
125 | return ((url as string):gsub(".*/api/[^/]+/[^/]+", "")) or "" --! cast | 133 | return (urls:gsub(".*/api/[^/]+/[^/]+", "")) or "" |
126 | end | 134 | end |
127 | 135 | ||
128 | local ltn12_ok, ltn12 = pcall(require, "ltn12") | 136 | local ltn12_ok, ltn12 = pcall(require, "ltn12") |
129 | if not ltn12_ok then -- If not using LuaSocket and/or LuaSec... | 137 | if not ltn12_ok then -- If not using LuaSocket and/or LuaSec... |
130 | 138 | ||
131 | function Api:request(url: string, params?: Parameters, post_params?: Parameters): {string : any}, string | 139 | function api.Api:request(url: string, params?: Parameters, post_params?: Parameters): {string : any}, string |
132 | local vars = cfg.variables | 140 | local vars = cfg.variables |
133 | 141 | ||
134 | if fs.which_tool("downloader") == "wget" then | 142 | if fs.which_tool("downloader") == "wget" then |
@@ -152,10 +160,10 @@ if not ltn12_ok then -- If not using LuaSocket and/or LuaSec... | |||
152 | local curl_cmd = vars.CURL.." "..vars.CURLNOCERTFLAG.." -f -L --silent --user-agent \""..cfg.user_agent.." via curl\" " | 160 | local curl_cmd = vars.CURL.." "..vars.CURLNOCERTFLAG.." -f -L --silent --user-agent \""..cfg.user_agent.." via curl\" " |
153 | for k,v in pairs(post_params) do | 161 | for k,v in pairs(post_params) do |
154 | local var = v | 162 | local var = v |
155 | if v is {string: string} then | 163 | if v is {string: (string | File)} then --! |
156 | var = "@"..v.fname | 164 | var = "@"..v.fname |
157 | end | 165 | end |
158 | curl_cmd = curl_cmd .. "--form \""..k.."="..var as string.."\" " --! cast | 166 | curl_cmd = curl_cmd .. "--form \""..k.."="..var.."\" " |
159 | end | 167 | end |
160 | if cfg.connection_timeout and cfg.connection_timeout > 0 then | 168 | if cfg.connection_timeout and cfg.connection_timeout > 0 then |
161 | curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." " | 169 | curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." " |
@@ -191,9 +199,9 @@ else -- use LuaSocket and LuaSec | |||
191 | 199 | ||
192 | local warned_luasec = false | 200 | local warned_luasec = false |
193 | 201 | ||
194 | function Api:request(url: string, params?: Parameters, post_params?: Parameters): {string : any}, string | 202 | function api.Api:request(url: string, params?: Parameters, post_params?: Parameters): {string : any}, string |
195 | local server = tostring(self.config.server) | 203 | local server = tostring(self.config.server) |
196 | local http_ok, http: boolean, any | 204 | local http_ok, http: boolean |
197 | local via = "luasocket" | 205 | local via = "luasocket" |
198 | if server:match("^https://") then | 206 | if server:match("^https://") then |
199 | http_ok, http = pcall(require, "ssl.https") --! a lot of new files in the src dir | 207 | http_ok, http = pcall(require, "ssl.https") --! a lot of new files in the src dir |
@@ -244,7 +252,7 @@ else -- use LuaSocket and LuaSec | |||
244 | if self.debug then | 252 | if self.debug then |
245 | util.printout(tostring(status)) | 253 | util.printout(tostring(status)) |
246 | end | 254 | end |
247 | local pok, ret, err = pcall(json.decode, table.concat(out)) | 255 | local pok, ret = pcall(json.decode, table.concat(out)) |
248 | if pok and ret then | 256 | if pok and ret then |
249 | return ret | 257 | return ret |
250 | end | 258 | end |
@@ -253,8 +261,8 @@ else -- use LuaSocket and LuaSec | |||
253 | 261 | ||
254 | end | 262 | end |
255 | 263 | ||
256 | function api.new(args): Api | 264 | function api.new(args: Args): Api, string |
257 | local self = {} | 265 | local self: Api = {} |
258 | setmetatable(self, { __index = Api }) | 266 | setmetatable(self, { __index = Api }) |
259 | self.config = self:load_config() or {} | 267 | self.config = self:load_config() or {} |
260 | self.config.server = args.server or self.config.server or cfg.upload.server | 268 | self.config.server = args.server or self.config.server or cfg.upload.server |
diff --git a/src/luarocks/upload/multipart.tl b/src/luarocks/upload/multipart.tl index 2be9c45f..5bcbc502 100644 --- a/src/luarocks/upload/multipart.tl +++ b/src/luarocks/upload/multipart.tl | |||
@@ -1,8 +1,8 @@ | |||
1 | 1 | ||
2 | local record multipart | 2 | local record multipart |
3 | record Parameters | 3 | record Parameters |
4 | {{string, string | File}} | 4 | {{string, (string | File)}} |
5 | map: {string: string | File} | 5 | map: {string: (string | File)} |
6 | end | 6 | end |
7 | end | 7 | end |
8 | 8 | ||