-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconnections.lua
176 lines (148 loc) · 3.97 KB
/
connections.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
local utils = require "remote-sshfs.utils"
local ui = require "remote-sshfs.ui"
local handler = require "remote-sshfs.handler"
local config = {}
local hosts = {}
local ssh_configs = {}
local sshfs_args = {}
-- Current connection
local sshfs_job_id = nil
local mount_point = nil
local current_host = nil
local M = {}
M.setup = function(opts)
config = opts
utils.setup_sshfs(config)
ssh_configs = config.connections.ssh_configs
sshfs_args = config.connections.sshfs_args
hosts = utils.parse_hosts_from_configs(ssh_configs)
end
M.is_connected = function()
if sshfs_job_id and mount_point and current_host then
return true
end
return false
end
M.list_hosts = function()
return hosts
end
M.list_ssh_configs = function()
return ssh_configs
end
M.get_current_host = function()
return current_host
end
M.get_current_mount_point = function()
return mount_point
end
M.reload = function()
hosts = utils.parse_hosts_from_configs(ssh_configs)
vim.notify "Reloaded!"
end
M.connect = function(host)
-- Initialize host variables
local remote_host = host["Name"]
if config.ui.confirm.connect then
local prompt = "Connect to remote host (" .. remote_host .. ")?"
ui.prompt_yes_no(prompt, function(item_short)
ui.clear_prompt()
if item_short == "y" then
M.init_host(host)
end
end)
else
M.init_host(host)
end
end
M.init_host = function(host, ask_pass)
-- If already connected, disconnect
if sshfs_job_id then
-- Kill the SSHFS process
vim.fn.jobstop(sshfs_job_id)
end
-- Create/confirm mount directory
local remote_host = host["Name"]
local mount_dir = config.mounts.base_dir .. remote_host
if not ask_pass then
utils.setup_mount_dir(mount_dir, function()
M.mount_host(host, mount_dir, ask_pass)
end)
else
M.mount_host(host, mount_dir, ask_pass)
end
end
M.mount_host = function(host, mount_dir, ask_pass)
-- Setup new connection
local remote_host = host["Name"]
-- Construct the SSHFS command
local sshfs_cmd = "sshfs -o LOGLEVEL=VERBOSE "
-- Add custom SSHFS args from config
for _, value in ipairs(sshfs_args) do
sshfs_cmd = sshfs_cmd .. value .. " "
end
if config.mounts.unmount_on_exit then
sshfs_cmd = sshfs_cmd .. "-f "
end
if host["Port"] then
sshfs_cmd = sshfs_cmd .. "-p " .. host["Port"] .. " "
end
local user = nil
if host["User"] then
user = host["User"]
end
if user then
sshfs_cmd = sshfs_cmd .. user .. "@"
end
sshfs_cmd = sshfs_cmd .. remote_host
if host["Path"] then
sshfs_cmd = sshfs_cmd .. ":" .. host["Path"] .. " "
else
sshfs_cmd = sshfs_cmd .. ": "
end
sshfs_cmd = sshfs_cmd .. mount_dir
local function start_job()
local sshfs_cmd_local = sshfs_cmd
-- If password required
if ask_pass then
local password = vim.fn.inputsecret "Enter password for host: "
sshfs_cmd_local = "echo " .. password .. " | " .. sshfs_cmd .. " -o password_stdin"
end
vim.notify("Connecting to host (" .. remote_host .. ")...")
local skip_clean = false
mount_point = mount_dir .. "/"
current_host = host
sshfs_job_id = vim.fn.jobstart(sshfs_cmd_local, {
on_stdout = function(_, data)
handler.sshfs_wrapper(data, mount_dir, function(event)
if event == "ask_pass" then
skip_clean = true
M.init_host(host, true)
end
end)
end,
on_stderr = function(_, data)
handler.sshfs_wrapper(data, mount_dir, function(event)
if event == "ask_pass" then
skip_clean = true
M.init_host(host, true)
end
end)
end,
on_exit = function(_, _, data)
handler.on_exit_handler(data, mount_dir, skip_clean, function()
sshfs_job_id = nil
mount_point = nil
current_host = nil
end)
end,
})
end
start_job()
end
M.unmount_host = function()
if sshfs_job_id then
-- Kill the SSHFS process
vim.fn.jobstop(sshfs_job_id)
end
end
return M