Skip to content

Commit

Permalink
fix(provider): Fix hostname parsing in connection options (#64)
Browse files Browse the repository at this point in the history
We were not parsing connection options correctly for hostname which needs to be removed from connection string. We have now implemented a more robust mechanism for handling this instead of relying on regular expressions.

Fixes #63.
  • Loading branch information
amitds1997 committed Nov 2, 2023
1 parent 4e65b1b commit e57a2f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 12 additions & 8 deletions lua/remote-nvim/providers/ssh/ssh_provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ end
---@param conn_opts string
---@return string cleaned_conn_opts Cleaned up SSH options
function SSHProvider:_cleanup_conn_options(conn_opts)
local host_expression = self.host:gsub("([^%w])", "%%%1")
return vim.trim(
conn_opts
:gsub("^%s*ssh%s*", "") -- Remove "ssh" prefix if it exists
:gsub("%s+", " ") -- Replace multiple whitespaces by a single one
:gsub(host_expression .. " ", " ") -- Remove hostname from connection string
:gsub("%-N", "") -- "-N" restrics command execution so we do not do it
)
local filtered_conn_opts = vim.tbl_filter(function(elem)
-- We filter following keywords and patterns
-- Any empty string, "-N" and hostname as a keyword in the connection options
return elem ~= self.host and elem ~= "-N" and elem ~= ""
end, vim.split(conn_opts, "%s", { trimempty = true }))

-- If the connection options begin with "ssh", remove "ssh"
if #filtered_conn_opts > 0 and filtered_conn_opts[1] == require("remote-nvim").config.ssh_config.ssh_binary then
table.remove(filtered_conn_opts, 1)
end

return table.concat(filtered_conn_opts, " ")
end

return SSHProvider
8 changes: 7 additions & 1 deletion tests/remote-nvim/providers/ssh/ssh_provider_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ describe("SSH Provider", function()
local ssh_provider = SSHProvider("localhost", "localhost -p 3011")
assert.equals(ssh_provider.conn_opts, "-p 3011")

ssh_provider = SSHProvider("localhost", " localhost ")
ssh_provider = SSHProvider("localhost", " localhost")
assert.equals(ssh_provider.conn_opts, "")

ssh_provider = SSHProvider("localhost", "localhost ")
assert.equals(ssh_provider.conn_opts, "")

ssh_provider = SSHProvider("user@localhost", "user@localhost")
assert.equals("", ssh_provider.conn_opts)
end)

it("should remove '-N' ssh option from connection options (if present)", function()
Expand Down

0 comments on commit e57a2f8

Please sign in to comment.