fix: actions.terminal supports ssh adapter (#152)

This commit is contained in:
Steven Arcangeli 2023-08-12 12:07:04 -07:00
parent 0e5fca35cd
commit 0ccf95ae5d
4 changed files with 83 additions and 31 deletions

View file

@ -22,8 +22,9 @@ local FIELD_META = constants.FIELD_META
---@param oil_url string
---@return oil.sshUrl
local function parse_url(oil_url)
M.parse_url = function(oil_url)
local scheme, url = util.parse_url(oil_url)
assert(scheme and url, string.format("Malformed input url '%s'", oil_url))
local ret = { scheme = scheme }
local username, rem = url:match("^([^@%s]+)@(.*)$")
ret.user = username
@ -80,11 +81,19 @@ local function url_to_scp(url)
return table.concat(pieces, "")
end
---@param url1 oil.sshUrl
---@param url2 oil.sshUrl
---@return boolean
local function url_hosts_equal(url1, url2)
return url1.host == url2.host and url1.port == url2.port and url1.user == url2.user
end
local _connections = {}
---@param url string
---@param allow_retry nil|boolean
---@return oil.sshFs
local function get_connection(url, allow_retry)
local res = parse_url(url)
local res = M.parse_url(url)
res.scheme = config.adapter_to_scheme.ssh
res.path = ""
local key = url_to_str(res)
@ -124,7 +133,7 @@ ssh_columns.permissions = {
end,
perform_action = function(action, callback)
local res = parse_url(action.url)
local res = M.parse_url(action.url)
local conn = get_connection(action.url)
conn:chmod(action.value, res.path, callback)
end,
@ -168,7 +177,7 @@ end
---@param bufname string
---@return string
M.get_parent = function(bufname)
local res = parse_url(bufname)
local res = M.parse_url(bufname)
res.path = pathutil.parent(res.path)
return url_to_str(res)
end
@ -176,7 +185,7 @@ end
---@param url string
---@param callback fun(url: string)
M.normalize_url = function(url, callback)
local res = parse_url(url)
local res = M.parse_url(url)
local conn = get_connection(url, true)
local path = res.path
@ -199,7 +208,7 @@ end
---@param column_defs string[]
---@param callback fun(err: nil|string, entries: nil|oil.InternalEntry[])
M.list = function(url, column_defs, callback)
local res = parse_url(url)
local res = M.parse_url(url)
cache.begin_update_url(url)
local conn = get_connection(url)
@ -267,7 +276,7 @@ end
---@param cb fun(err: nil|string)
M.perform_action = function(action, cb)
if action.type == "create" then
local res = parse_url(action.url)
local res = M.parse_url(action.url)
local conn = get_connection(action.url)
if action.entry_type == "directory" then
conn:mkdir(res.path, cb)
@ -277,15 +286,15 @@ M.perform_action = function(action, cb)
conn:touch(res.path, cb)
end
elseif action.type == "delete" then
local res = parse_url(action.url)
local res = M.parse_url(action.url)
local conn = get_connection(action.url)
conn:rm(res.path, cb)
elseif action.type == "move" then
local src_adapter = config.get_adapter_by_scheme(action.src_url)
local dest_adapter = config.get_adapter_by_scheme(action.dest_url)
if src_adapter == M and dest_adapter == M then
local src_res = parse_url(action.src_url)
local dest_res = parse_url(action.dest_url)
local src_res = M.parse_url(action.src_url)
local dest_res = M.parse_url(action.dest_url)
local src_conn = get_connection(action.src_url)
local dest_conn = get_connection(action.dest_url)
if src_conn ~= dest_conn then
@ -305,25 +314,25 @@ M.perform_action = function(action, cb)
local src_adapter = config.get_adapter_by_scheme(action.src_url)
local dest_adapter = config.get_adapter_by_scheme(action.dest_url)
if src_adapter == M and dest_adapter == M then
local src_res = parse_url(action.src_url)
local dest_res = parse_url(action.dest_url)
local src_conn = get_connection(action.src_url)
local dest_conn = get_connection(action.dest_url)
if src_conn.host ~= dest_conn.host then
local src_res = M.parse_url(action.src_url)
local dest_res = M.parse_url(action.dest_url)
if not url_hosts_equal(src_res, dest_res) then
shell.run({ "scp", "-C", "-r", url_to_scp(src_res), url_to_scp(dest_res) }, cb)
else
local src_conn = get_connection(action.src_url)
src_conn:cp(src_res.path, dest_res.path, cb)
end
src_conn:cp(src_res.path, dest_res.path, cb)
else
local src_arg
local dest_arg
if src_adapter == M then
src_arg = url_to_scp(parse_url(action.src_url))
src_arg = url_to_scp(M.parse_url(action.src_url))
local _, path = util.parse_url(action.dest_url)
dest_arg = fs.posix_to_os_path(path)
else
local _, path = util.parse_url(action.src_url)
src_arg = fs.posix_to_os_path(path)
dest_arg = url_to_scp(parse_url(action.dest_url))
dest_arg = url_to_scp(M.parse_url(action.dest_url))
end
shell.run({ "scp", "-C", "-r", src_arg, dest_arg }, cb)
end
@ -338,7 +347,7 @@ M.supports_xfer = { files = true }
M.read_file = function(bufnr)
loading.set_loading(bufnr, true)
local bufname = vim.api.nvim_buf_get_name(bufnr)
local url = parse_url(bufname)
local url = M.parse_url(bufname)
local scp_url = url_to_scp(url)
local basename = pathutil.basename(bufname)
local tmpdir = fs.join(vim.fn.stdpath("cache"), "oil")
@ -372,7 +381,7 @@ end
M.write_file = function(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr)
vim.bo[bufnr].modifiable = false
local url = parse_url(bufname)
local url = M.parse_url(bufname)
local scp_url = url_to_scp(url)
local tmpdir = fs.join(vim.fn.stdpath("cache"), "oil")
local fd, tmpfile = vim.loop.fs_mkstemp(fs.join(tmpdir, "ssh_XXXXXXXX"))