|
|
@ -25,21 +25,40 @@ Rename `libdistant_lua.so` or `libdistant_lua.dylib` to `distant_lua.so`
|
|
|
|
```lua
|
|
|
|
```lua
|
|
|
|
local distant = require("distant_lua")
|
|
|
|
local distant = require("distant_lua")
|
|
|
|
|
|
|
|
|
|
|
|
-- Distant functions are async by design and need to be wrapped in a coroutine
|
|
|
|
-- The majority of the distant lua module provides async and sync variants
|
|
|
|
-- in order to be used
|
|
|
|
-- of methods; however, launching a session is currently only synchronous
|
|
|
|
local thread = coroutine.wrap(distant.launch)
|
|
|
|
local session = distant.session.launch({ host = "127.0.0.1" })
|
|
|
|
|
|
|
|
|
|
|
|
-- Initialize the thread
|
|
|
|
-- Sync methods are executed in a blocking fashion, returning the result of
|
|
|
|
thread({ host = "127.0.0.1" })
|
|
|
|
-- the operation if successful or throwing an error if failing. Use `pcall`
|
|
|
|
|
|
|
|
-- if you want to capture the error
|
|
|
|
-- Continually check if launch has completed
|
|
|
|
local success, result = pcall(session.read_dir, session, { path = "path/to/dir" })
|
|
|
|
local res
|
|
|
|
if success then
|
|
|
|
while true do
|
|
|
|
for _, entry in ipairs(result.entries) do
|
|
|
|
res = thread()
|
|
|
|
print("Entry", entry.file_type, entry.path, entry.depth)
|
|
|
|
if res ~= distant.PENDING then
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
print(result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Async methods have _async as a suffix and need to be polled from
|
|
|
|
|
|
|
|
-- Lua in some manner; the `wrap_async` function provides a convience
|
|
|
|
|
|
|
|
-- to do so taking an async distant function and a scheduling function
|
|
|
|
|
|
|
|
local schedule_fn = function(cb) end
|
|
|
|
|
|
|
|
local read_dir = distant.utils.wrap_async(session.read_dir_async, schedule_fn)
|
|
|
|
|
|
|
|
read_dir(session, { path = "path/to/dir" }, function(success, result)
|
|
|
|
|
|
|
|
-- success: Returns true if ok and false if err
|
|
|
|
|
|
|
|
-- result: If success is true, then is the resulting value,
|
|
|
|
|
|
|
|
-- otherwise is the error
|
|
|
|
|
|
|
|
print("Success", success)
|
|
|
|
|
|
|
|
if success then
|
|
|
|
|
|
|
|
for _, entry in ipairs(result.entries) do
|
|
|
|
|
|
|
|
print("Entry", entry.file_type, entry.path, entry.depth)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
## Tests
|
|
|
|