-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathutils.lua
More file actions
136 lines (121 loc) · 4.08 KB
/
utils.lua
File metadata and controls
136 lines (121 loc) · 4.08 KB
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
local log = require "remote-sshfs.log"
local M = {}
M.setup_sshfs = function(config)
local sshfs_folder = config.mounts.base_dir
if not vim.loop.fs_stat(sshfs_folder) then
local ok, err = vim.loop.fs_mkdir(sshfs_folder, tonumber("700", 8))
if not ok then
vim.notify("Error creating mount base dir (" .. sshfs_folder .. "): " .. tostring(err), vim.log.levels.ERROR)
end
end
end
M.file_exists = function(path)
local _, error = vim.loop.fs_stat(path)
return error == nil
end
M.setup_mount_dir = function(mount_dir, callback)
log.line("util", "Setting up mount directory " .. mount_dir)
if not M.file_exists(mount_dir) then
log.line("util", "Creating mount directory " .. mount_dir)
local ok, err = vim.loop.fs_mkdir(mount_dir, tonumber("700", 8))
if not ok then
vim.notify("Error creating mount directory (" .. mount_dir .. "): " .. tostring(err), vim.log.levels.ERROR)
return
end
end
callback()
end
M.cleanup_mount_dir = function(mount_dir, callback)
log.line("util", "Cleaning up mount directory " .. mount_dir)
if M.file_exists(mount_dir) then
log.line("util", "Removing mount directory " .. mount_dir)
local success = vim.loop.fs_rmdir(mount_dir)
if not success then
vim.notify("Error cleaning up mount directory (" .. mount_dir .. ").")
else
callback()
end
else
callback()
end
end
M.parse_hosts_from_configs = function(ssh_configs)
local hosts = {}
local current_hosts = {}
-- Iterate through all ssh config files in config
for _, path in ipairs(ssh_configs) do
-- Open the SSH config file
local current_config = vim.fn.expand(path)
if vim.fn.filereadable(current_config) == 1 then
for line in io.lines(current_config) do
-- Ignore comments and empty lines
if line:sub(1, 1) ~= "#" and line:match "%S" then
-- Check if the line is a Host entry
local host_names = line:match "^%s*Host%s+(.+)$"
if host_names then
current_hosts = {}
for host_name in host_names:gmatch "%S+" do
table.insert(current_hosts, host_name)
hosts[host_name] = { ["Config"] = path, ["Name"] = host_name }
end
else
-- If the line is not a Host entry, but there are current hosts, add the line to their attributes
if #current_hosts > 0 then
local key, value = line:match "^%s*(%S+)%s+(.+)$"
if key and value then
for _, host in ipairs(current_hosts) do
hosts[host][key] = value
end
end
end
end
end
end
end
end
-- Resolve configured hostnames via `ssh -G`, to honor Include/Match/HostName directives
if vim.fn.executable "ssh" == 1 then
for alias, host in pairs(hosts) do
-- Use ssh to dump effective config for this host
local lines = vim.fn.systemlist { "ssh", "-G", alias }
if vim.v.shell_error == 0 then
for _, line in ipairs(lines) do
local key, value = line:match "^%s*(%S+)%s+(.*)$"
if key and value then
local k = key:lower()
if k == "hostname" then
host["HostName"] = value
elseif k == "user" then
host["User"] = value
elseif k == "port" then
host["Port"] = value
elseif k == "identityfile" then
host["IdentityFile"] = value
end
end
end
end
end
end
return hosts
end
M.parse_host_from_command = function(command)
local host = {}
local port = command:match "%-p (%d+)"
host["Port"] = port
command = command:gsub("%s*%-p %d+%s*", "")
local user, hostname, path = command:match "^([^@]+)@([^:]+):?(.*)$"
if not user then
hostname, path = command:match "^([^:]+):?(.*)$"
end
host["Name"] = hostname
host["User"] = user
host["Path"] = path
return host
end
M.change_directory = function(path)
-- Change the working directory of the Vim instance
vim.fn.execute("cd " .. path)
vim.notify("Directory changed to " .. path)
end
return M