-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.lua
More file actions
58 lines (55 loc) · 2.03 KB
/
utils.lua
File metadata and controls
58 lines (55 loc) · 2.03 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
-- Utility functions for elevated privileges operations
local M = {}
---Execute a command with sudo privileges
---@param cmd string The command to execute
---@param print_output boolean Whether to print the command output
---@return boolean success Whether the command executed successfully
M.sudo_exec = function(cmd, print_output)
-- Prompt for sudo password securely
local password = vim.fn.inputsecret("Password: ")
-- Execute command with sudo, using -S to read password from stdin
local out = vim.fn.system(string.format("/usr/bin/sudo -p '' -S %s", cmd), password)
-- Check for execution errors
if vim.v.shell_error ~= 0 then
print("\r\n")
print(out)
return false
end
-- Optionally display command output
if print_output then
print("\r\n", out)
end
return true
end
---Write the current buffer to a file with sudo privileges
---@param tmpfile? string Optional temporary file path
---@param filepath? string Optional target file path (defaults to current buffer's file)
M.sudo_write = function(tmpfile, filepath)
-- Use system temp file if none provided
if not tmpfile then
tmpfile = vim.fn.tempname()
end
-- Default to current buffer's file path
if not filepath then
filepath = vim.fn.expand("%")
end
-- Validate we have a target file path
if not filepath or #filepath == 0 then
print("E32: No file name")
return
end
-- `bs=1048576` is equivalent to `bs=1M` for GNU dd or `bs=1m` for BSD dd
-- Both `bs=1M` and `bs=1m` are non-POSIX
-- Use dd command to copy with elevated privileges
local cmd = string.format("dd if=%s of=%s bs=1048576", vim.fn.shellescape(tmpfile), vim.fn.shellescape(filepath))
-- Write current buffer to temporary file
vim.api.nvim_exec2(string.format("write! %s", tmpfile), { output = true })
-- Execute dd command with sudo and handle result
if M.sudo_exec(cmd, false) then
print(string.format('\r\n"%s" written', filepath))
vim.cmd("e!") -- Reload the buffer to reflect changes
end
-- Clean up temporary file
vim.fn.delete(tmpfile)
end
return M